/**
 * 数据处理二引擎
 **/

#ifndef DATADEALTWOENGINE_H
#define DATADEALTWOENGINE_H

#include "AppCommon.h"

#include "EngineBase.h"
#include "EngineFactory.h"
#include "MyYaml.h"
#include "myutils.h"
#include "json/json.h"

class DataDealTwoEngine : public ai_matrix::EngineBase
{
public:
    DataDealTwoEngine();
    ~DataDealTwoEngine();

    APP_ERROR Init() override;
    APP_ERROR DeInit() override;
    APP_ERROR Process() override;

private:
    //参数初始化(列车结束时需调用)
    void InitParam();

    //获取大框信息
    int GetPostData(std::shared_ptr<ProcessData> pProcessData, Json::Value &jvFrameInfo,
                    Json::Value &jvOneSplit);
    //获取大框信息
    void GetPostSubDatas(std::vector<PostSubData> &vecPostSubData, Json::Value &jvFrameInfo, Target tar,
                         Json::Value &jvOneSplit);

    //图片数据解码
    bool GetJpegdOut(std::shared_ptr<DecodedData> pDecodedData, RawData &rawData);
    //构造处理数据并push
    void MakeProcessData(std::shared_ptr<TrainRange> pTrainRange);

    void GetValidTypeAndSplit(Json::Value &jvOneSplit, Json::Value &jvMainSplit,
                              std::shared_ptr<ProcessData> pProcessData, Json::Value &jvFrameInfo,
                              std::shared_ptr<TrainRange> pTrainRange);
    void GetMainSplitInfo(Json::Value &jvMainSplit, std::shared_ptr<ProcessData> pProcessData,
                          Json::Value &jvFrameInfo, std::shared_ptr<TrainRange> pTrainRange);
    void GetSubSplitInfoByMain(Json::Value &jvOneSplit, std::shared_ptr<ProcessData> pProcessData, Json::Value &jvFrameInfo);

    void PushData(const std::string &strPort, const std::shared_ptr<ProcessData> &pProcessData);


private:
    std::string strPort0_;
    std::string strPort1_;
    std::string strPort2_;
    std::string strResultPath_;
    int iIntervalTime_;
    int iSplitSpanPX_;

    ai_matrix::DataSourceConfig dataSourceConfig_;
    std::map<int, ai_matrix::DataSourceConfig> mapUseDataSouceCfg_;
    std::map<int, std::vector<std::string>> mapSourcePushPort_;

    std::string strDataDir_;
    uint32_t iDataNO_;
    int iNoDataCnt_;

    std::map<std::string, std::shared_ptr<ProcessData>> mapProcessData_;//清晰度
    std::map<float, std::string> mapClear_;//清晰度

    int iTrainIndex_;       //车厢序号
    int iDirection_;        //行驶方向
    int iDirectionAndFirst_;

    int iLastSpaceX_;               //上次间隔位置
    uint32_t iLastSpaceFrameid_;    //上次间隔帧号
    int iSpaceType_;                //车厢间隔类型(0-未知; 1:车厢开始间隔; 2:车厢结束间隔)

};

ENGINE_REGIST(DataDealTwoEngine)

#endif
