#include "Step1MergeEngine.h"

using namespace ai_matrix;

namespace
{
    //按照x坐标排列
    bool CompareX(const SingleData &a, const SingleData &b)
    {
        return a.fLTX < b.fLTX;
    }
}

Step1MergeEngine::Step1MergeEngine() {}

Step1MergeEngine::~Step1MergeEngine() {}

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

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

    this->multiTypeQueue_ = new ai_matrix::MultiTypeQueue(2);
    LogInfo << "MergeEngine Init ok";
    return APP_ERR_OK;
}

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

APP_ERROR Step1MergeEngine::Process()
{
    int iRet = APP_ERR_OK;
    while (!isStop_)
    {
        std::shared_ptr<void> pVoidData0 = nullptr;
        inputQueMap_[strPort0_]->pop(pVoidData0);

        std::shared_ptr<void> pVoidData1 = nullptr;
        inputQueMap_[strPort1_]->pop(pVoidData1);

        if (nullptr == pVoidData0 && nullptr == pVoidData1)
        {
            usleep(1000); //1ms
            continue;
        }

        if (pVoidData0)
        {
            this->multiTypeQueue_->PushData(0, pVoidData0);
        }
        if (pVoidData1)
        {
            this->multiTypeQueue_->PushData(1, pVoidData1);
        }

        if (!this->multiTypeQueue_->PopAllData(pVoidData0, pVoidData1))
        {
            usleep(1000); //1ms
            continue;
        }

        std::shared_ptr<InferenceResultData> pInferenceResultData =
                std::static_pointer_cast<InferenceResultData>(pVoidData0);
        std::shared_ptr<InferenceResultData> pInferenceResultData_container =
                std::static_pointer_cast<InferenceResultData>(pVoidData1);

        pInferenceResultData->vecSingleData = pInferenceResultData_container->vecSingleData;

        std::sort(pInferenceResultData->vecSingleData.begin(),
                  pInferenceResultData->vecSingleData.end(),
                  CompareX);
        if (pInferenceResultData->singleData.fScore > 0.0f)
        {
            // 箱号
            LogDebug << " 帧:" << pInferenceResultData->iFrameId
                     << " 数据源:" << pInferenceResultData->iDataSource
                     << " --iClassId:" << pInferenceResultData->singleData.iClassId
                     << " confidence=" << pInferenceResultData->singleData.fScore
                     << " lx=" << pInferenceResultData->singleData.fLTX
                     << " ly=" << pInferenceResultData->singleData.fLTY
                     << " rx=" << pInferenceResultData->singleData.fRBX
                     << " ry=" << pInferenceResultData->singleData.fRBY
                     << " clear:" << pInferenceResultData->singleData.fClear;
        }

        // 箱角
        for (const auto & it_result : pInferenceResultData->vecSingleData)
        {
            LogDebug << " 帧:" << pInferenceResultData->iFrameId
                     << " 数据源:" << pInferenceResultData->iDataSource
                     << " --iClassId:" << it_result.iClassId
                     << " confidence=" << it_result.fScore
                     << " lx=" << it_result.fLTX
                     << " ly=" << it_result.fLTY
                     << " rx=" << it_result.fRBX
                     << " ry=" << it_result.fRBY;
        }

        outputQueMap_[strPort0_]->push(std::static_pointer_cast<void>(pInferenceResultData), true);
    }
    return APP_ERR_OK;
}




