#include "SaveResultCSVEngine.h"

using namespace ai_matrix;

SaveResultCSVEngine::SaveResultCSVEngine() {}

SaveResultCSVEngine::~SaveResultCSVEngine() {}

APP_ERROR SaveResultCSVEngine::Init()
{
    strPort0_ = engineName_ + "_" + std::to_string(engineId_) + "_0";
    strPort1_ = engineName_ + "_" + std::to_string(engineId_) + "_1";

    this->baseConfig_ = Config::getins()->getBaseConfig();

    LogInfo << "SaveResultCSVEngine Init ok";
    return APP_ERR_OK;
}

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

/**
* 保存车厢的最优结果到CSV中
* inParam : std::shared_ptr<Train> pTrain   :列车信息
* outParam:
* return  : true/false
*/
bool SaveResultCSVEngine::saveTrainCsv(std::shared_ptr<DetectResultData> pDetectResultData)
{
    //1. 创建保存路径 (固定路径/YYYY-MM-DD/hh-mm-ss/)
    if (!FileUtil::getins()->createDirPath(this->baseConfig_.strResultPath))
    {
        LogError << "日期:" << pDetectResultData->strDetectDate << " csv save err";
        return false;
    }

    if (pDetectResultData->strDetectDate.empty())
    {
        return true;
    }
    //2. 保存csv
    std::string strCsvName = pDetectResultData->strDetectDate + ".csv";
    std::string strCsvPath = this->baseConfig_.strResultPath + "/"
            + pDetectResultData->strDetectDate + "/"
            + strCsvName;

//    LogInfo << strCsvPath;

    bool bIsExsit = false;
    if (access(strCsvPath.c_str(), F_OK) != -1)
    {
        bIsExsit = true;
    }

    try
    {
        // 写文件
        std::ofstream outFile;
        outFile.open(strCsvPath, std::ios::app);

        if (!bIsExsit)
        {
            outFile << "Time" << ','
                    << "Container" << ','
                    << "Image"
                    << std::endl;
        }

        std::string strContainerNo;
        for (const auto & containerNo_it : pDetectResultData->vecContainerNO)
        {
            strContainerNo += "[";
            strContainerNo += containerNo_it;
            strContainerNo += "]";
        }
        std::string strImage;
        for (const auto & image_it : pDetectResultData->vecImage)
        {
            strImage += "[";
            strImage += image_it;
            strImage += "]";

            std::string strDebugImage = this->baseConfig_.strDebugResultPath + "/"
                                        + pDetectResultData->strDetectDate + "/"
                                        + StringUtil::getins()->replace_all_distinct(pDetectResultData->strDetectTime, ":", "-")
                                        + "/" + image_it;
            std::string strResultImage = this->baseConfig_.strResultPath + "/"
                                        + pDetectResultData->strDetectDate + "/"
                                        + StringUtil::getins()->replace_all_distinct(pDetectResultData->strDetectTime, ":", "-")
                                        + "/" + image_it;
            FileUtil::getins()->copyFile(strDebugImage, strResultImage);
        }

        outFile << pDetectResultData->strDetectTime << ','
                << strContainerNo << ','
                << strImage << ','
                << std::endl;

        outFile.close();


    }
    catch (const std::exception &)
    {
        LogError << "strCsvPath:" << strCsvPath << " train savecsv fail!";
        return false;
    }
    return true;
}


APP_ERROR SaveResultCSVEngine::Process()
{
    int iRet = APP_ERR_OK;
    while (!isStop_)
    {
        //pop端口0 车厢信息
        std::shared_ptr<void> pVoidData0 = nullptr;
        iRet = inputQueMap_[strPort0_]->pop(pVoidData0);
        if (nullptr == pVoidData0)
        {
            usleep(1000);
            continue;
        }

        std::shared_ptr<DetectResultData> pDetectResultData = std::static_pointer_cast<DetectResultData>(pVoidData0);

        this->saveTrainCsv(pDetectResultData);

    }
    return APP_ERR_OK;
}
