#include "LocalDataMoveEngine.h"


LocalDataMoveEngine::LocalDataMoveEngine() {}
LocalDataMoveEngine::~LocalDataMoveEngine() {}

APP_ERROR LocalDataMoveEngine::Init()
{
    strPort0_ = engineName_ + "_" + std::to_string(engineId_) + "_0";
    mapUseDataSouceCfg_ = MyYaml::GetIns()->GetUseDataSourceConfig();
    strResultPath_ = MyYaml::GetIns()->GetPathValue("gc_result_path");
    bUploadFlag_ = (MyYaml::GetIns()->GetIntValue("gc_ftp_open") == 1 || MyYaml::GetIns()->GetIntValue("gc_minio_open") == 1);
    iPushDirection_ = MyYaml::GetIns()->GetIntValue("gc_push_direction");

    InitParam();
    LogInfo << "LocalDataMoveEngine Init ok";
    return APP_ERR_OK;
}

APP_ERROR LocalDataMoveEngine::DeInit()
{
    LogInfo << "LocalDataMoveEngine DeInit ok";
    return APP_ERR_OK;
}

/**
* 初始化参数信息
* inParam : N/A
* outParam: N/A
* return  : N/A
*/
void LocalDataMoveEngine::InitParam()
{
    iDirection_ = DIRECTION_UNKNOWN;
    mapFtpFrameInfo_.clear();
}

APP_ERROR LocalDataMoveEngine::Process()
{
    int iRet = APP_ERR_OK;
    while (!isStop_)
    {
        std::shared_ptr<void> pVoidData0 = nullptr;
        inputQueMap_[strPort0_]->pop(pVoidData0);
        if (nullptr == pVoidData0)
        {
            usleep(1000); // 1ms
            continue;
        }

        std::shared_ptr<ProcessData> pProcessData = std::static_pointer_cast<ProcessData>(pVoidData0);

        //1.日期或时间不相等(停车导致的)，则复制图片到新过车文件中
        if (pProcessData->strOrigTrainDate != pProcessData->strTrainDate || pProcessData->strOrigTrainName != pProcessData->strTrainName)
        {
            char szCameraNo[5] = {0};
            sprintf(szCameraNo, "%03d/", pProcessData->iDataSource + 1);
            std::string strOldPath = strResultPath_ + pProcessData->strOrigTrainDate + "/" +
                                     pProcessData->strOrigTrainName + "/" + szCameraNo;
            std::string strNewPath = strResultPath_ + pProcessData->strTrainDate + "/" +
                                     pProcessData->strTrainName + "/" + szCameraNo;
            MyUtils::getins()->CreateDirPath(strNewPath);

            std::string strOldTxtFile = strOldPath + std::to_string(pProcessData->iOrigFrameId) + ".txt";
            std::string strOldImgFile = strOldPath + std::to_string(pProcessData->iOrigFrameId) + ".jpg";
            std::string strNewTxtFile = strNewPath + std::to_string(pProcessData->iFrameId) + ".txt";
            std::string strNewImgFile = strNewPath + std::to_string(pProcessData->iFrameId) + ".jpg";
            if (mapUseDataSouceCfg_[pProcessData->iDataSource].iRotate != 0)
            {
                std::string strOldRotateImgFile = strOldPath + std::to_string(pProcessData->iOrigFrameId) + "_rotate.jpg";
                std::string strNewRotateImgFile = strNewPath + std::to_string(pProcessData->iFrameId) + "_rotate.jpg";
                MyUtils::getins()->copyFile(strOldRotateImgFile, strNewRotateImgFile);
            }

            MyUtils::getins()->copyFile(strOldImgFile, strNewImgFile);

            Json::Value jvFrameInfo;
            MyUtils::getins()->ReadJsonInfo(jvFrameInfo, strOldTxtFile);
            jvFrameInfo["from"] = strOldTxtFile;
            MyUtils::getins()->WriteJsonInfo(jvFrameInfo, strNewTxtFile);
        }

        //2.组织数据上传ftp
        if (bUploadFlag_)
        {
            if (iDirection_ == DIRECTION_UNKNOWN)
            {
                std::string strFilePath = strResultPath_ + pProcessData->strTrainDate + "/" + pProcessData->strTrainName + "/" + "direction.txt";
                Json::Value jvDirectionInfo;
                if (MyUtils::getins()->ReadJsonInfo(jvDirectionInfo, strFilePath))
                {
                    iDirection_ = jvDirectionInfo["direction"].asInt();
                }
            }
            if (iDirection_ == DIRECTION_UNKNOWN || iDirection_ == iPushDirection_ || iPushDirection_ == DIRECTION_UNKNOWN)
            {
                if (mapFtpFrameInfo_.find(pProcessData->iDataSource) == mapFtpFrameInfo_.end() ||
                    pProcessData->iFrameId > mapFtpFrameInfo_[pProcessData->iDataSource])
                {
                    mapFtpFrameInfo_[pProcessData->iDataSource] = pProcessData->iFrameId;

                    char szCameraNo[5] = {0};
                    sprintf(szCameraNo, "%03d/", pProcessData->iDataSource + 1);
                    std::string strImgPath = pProcessData->strTrainDate + "/" + pProcessData->strTrainName + "/" + szCameraNo;
                    std::string strImgName = std::to_string(pProcessData->iFrameId) + ".jpg";

                    std::shared_ptr<FtpData> pFtpData = std::make_shared<FtpData>();
                    pFtpData->strLocalFile = strResultPath_ + strImgPath + strImgName;
                    pFtpData->strFtpFilePath = strImgPath;
                    pFtpData->strFtpFileName = strImgName;
                    pFtpData->bIsEnd = pProcessData->bIsEnd;
                    iRet = outputQueMap_[strPort0_]->push(std::static_pointer_cast<void>(pFtpData), false);
                }
            }
        }

        if (pProcessData->bIsEnd)
        {
            InitParam();
        }
    }
    return APP_ERR_OK;
}
