#include "cmwcammatch_calibration.h"
#include "ui_cmwcammatch_calibration.h"
#include "logic.h"
#include <QFileDialog>
#include "cmakepath.h"
#include "cmwcammorphology_h.h"
CMWCamMatch_Calibration::CMWCamMatch_Calibration(QWidget *parent,int32_t iCamID) :
    CMWCamAlgorithm(parent,iCamID),
    ui(new Ui::CMWCamMatch_Calibration)
{
    ui->setupUi(this);
}

CMWCamMatch_Calibration::~CMWCamMatch_Calibration()
{
    delete ui;
}
void CMWCamMatch_Calibration::MInit()
{
    CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
    Q_ASSERT(pCam);
    CAMatchNCC_H* pMatch=(CAMatchNCC_H*)pCam->Find_Algorithm(M_CALIBRATION_NCC);
    Q_ASSERT(pMatch);
    Variant* pparam[50]={0};
    pparam[0]=pMatch->m_Variants.get_Variant(Var_Match_AngleExtent_ID);
    pparam[1]=pMatch->m_Variants.get_Variant(Var_Match_AngleStart_ID);
    pparam[2]=pMatch->m_Variants.get_Variant(Var_Match_AngleStep_ID);
    pparam[3]=pMatch->m_Variants.get_Variant(Var_Match_Greediness_ID);
    pparam[4]=pMatch->m_Variants.get_Variant(Var_Match_MaxOverlap_ID);
    pparam[5]=pMatch->m_Variants.get_Variant(Var_MatchNcc_Metric_ID);
    pparam[6]=pMatch->m_Variants.get_Variant(Var_Match_MiniScore_ID);
    pparam[7]=pMatch->m_Variants.get_Variant(Var_Match_ThresScore_ID);
    pparam[8]=pMatch->m_Variants.get_Variant(Var_Match_NumLevels_ID);
    pparam[9]=pMatch->m_Variants.get_Variant(Var_Match_NumMatches_ID);
    pparam[10]=pMatch->m_Variants.get_Variant(Var_Match_SubPixel_ID);
    pparam[11]=pMatch->m_Variants.get_Variant(Var_Algorithm_EnableROI_ID);

	CProcess* pProcessMain = GET_PROCESS(PROCESS_CUT, SM_MAIN, CProcess);
    pparam[12]=pProcessMain->m_Variants.get_Variant(Var_Calibration_POSX);
    pparam[13]=pProcessMain->m_Variants.get_Variant(Var_Calibration_POSY);
    pparam[14]=pProcessMain->m_Variants.get_Variant(Var_Calibration_POSZ);
    pparam[15]=pProcessMain->m_Variants.get_Variant(Var_Calibration_OffsetX);
    pparam[16]=pProcessMain->m_Variants.get_Variant(Var_Calibration_OffsetY);
    //pparam[17]=pProcessMain->m_Variants.get_Variant(Var_Calibration_OffsetY);
    pparam[17]=pProcessMain->m_Variants.get_Variant(Var_Calibration_Angle);
    pparam[18]=pProcessMain->m_Variants.get_Variant(Var_Calibration_Adjust_Threshold);

    ui->widget->MParamInit_list(pparam,18);
    mchildwidgets[(uintptr_t)ui->widget]=ui->widget;

    ui->widget_Model->MInit();


    CMWCamMorphology_h* pMWMorphology=new CMWCamMorphology_h(this);
    pMWMorphology->m_pAlgorithm=pMatch;
    pMWMorphology->m_iCamID=m_iCamID;
    ui->horizontalLayout_Morphology->addWidget(pMWMorphology);
    mchildwidgets[uintptr_t(pMWMorphology)]=pMWMorphology;

}

void CMWCamMatch_Calibration::MFresh()
{
    CMWidget::MFresh();
    FreshModel();
}
void CMWCamMatch_Calibration::MApply()
{
    CMWidget::MApply();
}

void CMWCamMatch_Calibration::FreshModel()
{
    CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
    Q_ASSERT(pCam);
    CAMatchNCC_H* pMatch=(CAMatchNCC_H*)pCam->Find_Algorithm(M_CALIBRATION_NCC);
    Q_ASSERT(pMatch);
    if(pMatch->modelData.iModelID!=-1)
    {
        pMatch->showmodel_xld(ui->widget_Model->m_pContainer);
        ui->widget_Model->setImage(pMatch->modelData.ModelImage);

    }
    else
    {
        ui->widget_Model->setImage(pMatch->modelData.ModelImage);
    }
    ui->widget_Model->MFresh();
}

void CMWCamMatch_Calibration::on_pushButton_Tool_clicked()
{
    CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
    Q_ASSERT(pCam);
    CAMatchNCC_H* pMatch=(CAMatchNCC_H*)pCam->Find_Algorithm(M_CALIBRATION_NCC);
    Q_ASSERT(pMatch);
    if(!pMatch->m_bModelRectInited)
    {
        pMatch->m_bModelRectInited=true;
        int32_t width=pCam->m_Variants.get_VData(Var_Cam_Width_ID)->toInt();
        int32_t height=pCam->m_Variants.get_VData(Var_Cam_Height_ID)->toInt();
        pMatch->model_rect=CRect(width/4,height/4,CMPointd(width/2,height/2));
    }
    pMatch->showragion_active(&pCam->m_imageContainer);
}

void CMWCamMatch_Calibration::on_pushButton_Train_clicked()
{
    try
    {
        CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
        Q_ASSERT(pCam);
        CAMatchNCC_H* pMatch=(CAMatchNCC_H*)pCam->Find_Algorithm(M_CALIBRATION_NCC);
        Q_ASSERT(pMatch);
        pMatch->createModel(&pCam->m_imageContainer.m_Image,
                            &pMatch->modelData);
        FreshModel();
    }
    catch(QString& s)
    {
        m_gSystem.m_SysMsg.SetMsg(s);
    }
    //   pCamDriver->m_bTransImage=true;
}

void CMWCamMatch_Calibration::on_pushButton_Load_clicked()
{
    try
    {
        QString path= QFileDialog::getOpenFileName(this,QString( ),
                                                   CMakePath::make_pattern_path(""),
                                                   QString("*.mnm" )  );
        if(path.isEmpty()) return;
        CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
        Q_ASSERT(pCam);
        CAMatchNCC_H* pMatch=(CAMatchNCC_H*)pCam->Find_Algorithm(M_CALIBRATION_NCC);
        Q_ASSERT(pMatch);


        pMatch->loadModel(path,&pMatch->modelData);
        FreshModel();

    }
    catch(QString& s)
    {
        m_gSystem.m_SysMsg.SetMsg(s);
    }
}

void CMWCamMatch_Calibration::on_pushButton_Save_clicked()
{
    try
    {
        QString path= QFileDialog::getSaveFileName(this,QString( ),
                                                   CMakePath::make_pattern_path(""),\
                                                   QString(".mnm" ) );
        if(path.isEmpty()) return;

        QFileInfo info(path);
        path=info.absolutePath();
        path+='/'+info.baseName()+".mnm";

        CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
        Q_ASSERT(pCam);
        CAMatchNCC_H* pMatch=(CAMatchNCC_H*)pCam->Find_Algorithm(M_CALIBRATION_NCC);
        Q_ASSERT(pMatch);

        pMatch->saveModel(path,&pMatch->modelData);

    }
    catch(QString& s)
    {
        m_gSystem.m_SysMsg.SetMsg(s);
    }
}

void CMWCamMatch_Calibration::on_pushButton_Exe_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,
                          QObject::trUtf8("Match Ncc Sig"),
                          QVariantList()
                          <<CAM_DRIVER<<CCamDriver::EXE_ALGORITHM
                          <<m_iCamID<<M_CALIBRATION_NCC,0);
    m_gSystem.start_test(SM_MAIN, msgqueue,false);

}

//void CMWCamMatch_Calibration::on_checkBox_ROI_clicked()
//{

//}

//void CMWCamMatch_Calibration::on_checkBox_ROI_clicked(bool checked)
//{
//    CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
//    Q_ASSERT(pCam);
//    CAMatchNCC_H* pMatch=(CAMatchNCC_H*)pCam->Find_Algorithm(M_CALIBRATION_NCC);
//    Q_ASSERT(pMatch);
//    pMatch->m_Variants.set_VData(Var_Algorithm_EnableROI_ID,checked);
//    if(checked)
//    {
//        pCam->m_imageContainer.addShape(&pMatch->m_rect_ROI,
//                                        CImageContainer::ACTIVE_SHAPE);
//    }
//}

void CMWCamMatch_Calibration::on_pushButton_ROI_clicked()
{
    CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
    Q_ASSERT(pCam);
    CAMatchNCC_H* pMatch=(CAMatchNCC_H*)pCam->Find_Algorithm(M_CALIBRATION_NCC);
    Q_ASSERT(pMatch);
    pCam->m_imageContainer.addShape(&pMatch->m_rect_ROI,
                                    CImageContainer::ACTIVE_SHAPE);
}

void CMWCamMatch_Calibration::on_btnMoveToCali_clicked()
{
    CProcess* pProcessMain = GET_PROCESS(PROCESS_CUT, SM_MAIN, CProcess);
    double posX = pProcessMain->m_Variants.get_VData(Var_Calibration_POSX)->toDouble();
    double posY = pProcessMain->m_Variants.get_VData(Var_Calibration_POSY)->toDouble();
    double posZ = pProcessMain->m_Variants.get_VData(Var_Calibration_POSZ)->toDouble();

    V_MsgQueue msgqueue;
    m_gSystem.absmove_xy_msg(posX, posY, msgqueue);
    m_gSystem.absmove_msg(posZ, MOTORZ, msgqueue);
    m_gSystem.start_test(SM_MAIN, msgqueue,true);
}

void CMWCamMatch_Calibration::on_btnLoad_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Process_ID,
                          QObject::trUtf8("Load All Model Sig"),
                          QVariantList()<<PROCESS_LEVELING
                          <<CProcess_Level::LOADCALIBRATIONMODEL
                          <<"Calibration",0);
    m_gSystem.start_test(SM_MAIN, msgqueue,true);
}

void CMWCamMatch_Calibration::on_btnSave_clicked()
{
    //保存模板及相应的参数
    try
    {
        QString folderPath = CMakePath::make_pattern_path("Calibration");
        QString filePath = QString("%1/%2").arg(folderPath).arg("Cam6X.mnm");
        CCam* pCam=GET_CHDIRECT(m_iCamID,CCam);
        Q_ASSERT(pCam);
        CAMatchNCC_H* pMatch=(CAMatchNCC_H*)pCam->Find_Algorithm(M_CALIBRATION_NCC);
        Q_ASSERT(pMatch);
        pMatch->saveModel(filePath,&pMatch->modelData);

    }
    catch(QString& s)
    {
        m_gSystem.m_SysMsg.SetMsg(s);
    }
}

void CMWCamMatch_Calibration::on_btnCalibration_clicked()
{
    CProcess* pProcessMain = GET_PROCESS(PROCESS_CUT, SM_MAIN, CProcess);
    double posX = pProcessMain->m_Variants.get_VData(Var_Calibration_POSX)->toDouble();
    double posY = pProcessMain->m_Variants.get_VData(Var_Calibration_POSY)->toDouble();
    double posZ = pProcessMain->m_Variants.get_VData(Var_Calibration_POSZ)->toDouble();

    V_MsgQueue msgqueue;
    m_gSystem.absmove_xy_msg(posX, posY, msgqueue);
    m_gSystem.absmove_msg(posZ, MOTORZ, msgqueue);
    msgqueue.pushback_Msg(VarMsg_Calibration,QObject::trUtf8("Camera Calibration"),
                          QVariantList()
                          <<CAM6X,0);

    m_gSystem.start_test(SM_MAIN, msgqueue, true);
}
