#include "ImgDecodeEngine.h"

#include <algorithm>
#include <string>
#include <regex>
#include <sys/stat.h>
using namespace std;
using namespace cv;


using namespace ai_matrix;

ImgDecodeEngine::ImgDecodeEngine() {}

ImgDecodeEngine::~ImgDecodeEngine() {}

APP_ERROR ImgDecodeEngine::Init()
{
    bUseEngine_ = true;
    dataSourceConfig_ = MyYaml::GetIns()->GetDataSourceConfigById(engineId_); //获取摄像机参数
    if (MyYaml::GetIns()->GetStringValue("gc_data_source") != "images" || !dataSourceConfig_.bUse)
    {
        bUseEngine_ = false;
        LogWarn << "engineId_:" << engineId_ << " not use engine";
        return APP_ERR_OK;
    }

    strPort0_ = engineName_ + "_" + std::to_string(engineId_) + "_0";

    width_ = IMAGE_WIDTH, height_ = IMAGE_HEIGHT;

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

APP_ERROR ImgDecodeEngine::DeInit()
{
    if (!bUseEngine_)
    {
        LogWarn << "engineId_:" << engineId_ << " not use engine";
        return APP_ERR_OK;
    }

    LogInfo << "ImgDecodeEngine deinit ok";
    return APP_ERR_OK;
}

APP_ERROR ImgDecodeEngine::Process()
{
    if (!bUseEngine_)
    {
        LogWarn << "engineId_:" << engineId_ << " not use engine";
        return APP_ERR_OK;
    }
    // #ifdef SAVE_BGR2RGB_FILE
    // char rgb_stream[256] = {};
    // sprintf(rgb_stream, "bgr2rgb_stream_%dx%d.rgb", width_, height_);
    // FILE *rgb_stream_fp = fopen(rgb_stream, "ab+");
    // #endif

    uint64_t u64count_num = 0;
    int iRet = APP_ERR_OK;
    
    while (!isStop_)
    {
        std::shared_ptr<void> pVoidData0 = nullptr;
        inputQueMap_[strPort0_]->pop(pVoidData0);
        if (nullptr == pVoidData0)
        {
            usleep(1*1000); //n ms
            continue;
        }
        //透传下一个Engine
        outputQueMap_[strPort0_]->push(pVoidData0);
        
    //     // std::cout<<"Enter BGR2RGBEngine Thread "<<++u64count_num<<" Times!"<<std::endl;
    //     // std::cout<<"BGR2RGBEngine Thread ID: "<<std::this_thread::get_id()<<std::endl;

    //     //接收到BGR数据
    //     std::shared_ptr<FrameData> pBGRFrameData = std::static_pointer_cast<FrameData>(pVoidData0);

    //     //构造RGB数据
	// 	void* pRGBBuffer = nullptr;
	// 	unsigned int pRGBBuffer_Size = width_*height_*3;
	// 	pRGBBuffer = new uint8_t[pRGBBuffer_Size];
	// 	std::shared_ptr<FrameData> pRGBFrameData = std::make_shared<FrameData>();

    //     //像素格式转换
    //     cv::Mat BGRImage(height_, width_, CV_8UC3, static_cast<uint8_t *>(pBGRFrameData->pData.get()));   //BGR
    //     cv::Mat RGBImage(height_, width_, CV_8UC3, pRGBBuffer);   //RGB

    //     #ifdef OPENCV_CVTCOLOR_BGR2RGB_TIME_CONSUMING_TEST
    //     auto start = std::chrono::system_clock::now();  //计时开始
    //     cv::cvtColor(BGRImage, RGBImage, cv::COLOR_BGR2RGB);     //像素格式转换 BGR转RGB
    //     auto end = std::chrono::system_clock::now();  //计时结束
    //     std::cout << "frame width: "<<width_<<" frame height:"<<height_<<" opencv cvtColor BGR2RGB time: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << "ms" << std::endl;
    //     #else
    //     cv::cvtColor(BGRImage, RGBImage, cv::COLOR_BGR2RGB);     //像素格式转换 BGR转RGB
    //     #endif

    //     //压入像素格式转换后的数据
    //     //组织数据
    //     pRGBFrameData->iDataSource = engineId_;
    //     pRGBFrameData->iSize = pRGBBuffer_Size;
    //     pRGBFrameData->pData.reset(pRGBBuffer, [](void* data){if(data) {delete[] data; data = nullptr;}}); //智能指针管理内存
    //     // pFrameData->pData.reset(pRGBBuffer, Deleter); //智能指针管理内存
    //     pRGBFrameData->i64TimeStamp =  pBGRFrameData->i64TimeStamp;
        
    //     //开启此宏保存转换后的RGB数据
    //     #ifdef SAVE_BGR2RGB_FILE
    //     if (rgb_stream_fp)
    //     {
    //         fwrite(pRGBFrameData->pData.get(), 1, pRGBFrameData->iSize, rgb_stream_fp);
    //         fflush(rgb_stream_fp);
    //         fsync(fileno(rgb_stream_fp));
    //     }
    //     #endif

    //     std::cout<<"port0 push the rgb frame data!"<<std::endl;
    //     #if 1
    //     iRet = outputQueMap_[strPort0_]->push(std::static_pointer_cast<void>(pRGBFrameData));
    //     if (iRet != APP_ERR_OK){
	// 		LogError << "push the rgb frame data failed...";
    //         // std::cerr<<"push the rgb frame data failed..."<<std::endl;
	// 	}else{
    //          std::cout<<"push the rgb frame data success!"<<std::endl;
    //     }
    //     #endif
    }

    // #ifdef SAVE_BGR2RGB_FILE
    // fclose(rgb_stream_fp);
    // if(rgb_stream_fp){
    //     rgb_stream_fp = nullptr;
    // }
    // #endif
}
