#include "cmwcalimultilaserkerf.h"
#include "ui_cmwcalimultilaserkerf.h"
#include "cprocess_laserkerf.h"
#include "callsystem.h"
#include "logic_local_def.h"
#include "cjig_driver.h"
#include "logic_compdef.h"
#include "csystem_external.h"
#include "qmymessagebox.h"
#include "cmotordriver.h"
#include <QTimer>

CMWCaliMultiLaserKerf::CMWCaliMultiLaserKerf(QWidget* parent)
    : CMWidget(parent)
{
    ui = new Ui::CMWCaliMultiLaserKerf();
    ui->setupUi(this);
}

CMWCaliMultiLaserKerf::~CMWCaliMultiLaserKerf()
{
    delete ui;
}


void CMWCaliMultiLaserKerf::MInit()
{
    CProcess_LaserKerf* pKerf = GET_PROCESS(PROCESS_LASERKERF,
                                            SM_MAIN, CProcess_LaserKerf);
    Q_ASSERT(pKerf);
    Variant* pparam[30] = { 0 };
    int index = 0;
    CJigDriver* pJigDriver = GET_CARD(JigDriver, CJigDriver);
    //中心位置
    pparam[index++] = pJigDriver->m_Variants.get_Variant(Var_Process_LaserKerf_AreaCenterX);
    pparam[index++] = pJigDriver->m_Variants.get_Variant(Var_Process_LaserKerf_AreaCenterY);
    pparam[index++] = pJigDriver->m_Variants.get_Variant(Var_Process_LaserKerf_ZFocusThickness);
    //划线的参数
    pparam[index++] = pJigDriver->m_Variants.get_Variant(Var_Process_LaserKerf_WorkTableYOffset);
    pparam[index++] = pJigDriver->m_Variants.get_Variant(Var_Process_LaserKerf_LineLength);
    
    //Focus的光源
    pparam[index++] = pJigDriver->m_Variants.get_Variant(Var_Process_LaserKerf_FocusLightDirect);
    pparam[index++] = pJigDriver->m_Variants.get_Variant(Var_Process_LaserKerf_FocusLightCircle);
    pparam[index++] = pJigDriver->m_Variants.get_Variant(Var_Process_LaserKerf_FocusExposure);
    pparam[index++] = pJigDriver->m_Variants.get_Variant(Var_Process_LaserKerf_FocusGain);

    //区域的参数
    pparam[index++] = pJigDriver->m_Variants.get_Variant(Var_Process_LaserKerf_AreaCellDiameter);
    pparam[index++] = pJigDriver->m_Variants.get_Variant(Var_Process_LaserKerf_AreaValidWidth);
    pparam[index++] = pJigDriver->m_Variants.get_Variant(Var_Process_LaserKerf_LineStep);
    pparam[index++] = pJigDriver->m_Variants.get_Variant(Var_Process_LaserKerf_AreaType);
    pparam[index++] = pJigDriver->m_Variants.get_Variant(Var_Process_LaserKerf_OuterErosionDistance);
    pparam[index++] = pJigDriver->m_Variants.get_Variant(Var_Process_LaserKerf_InnerErosionDistance);

    int iID[30] = { 0 };
    iID[0] = MOTORX | (CMotorDriver::ABSMOVE_TYPE << 16);
    iID[1] = MOTORY | (CMotorDriver::ABSMOVE_TYPE << 16);

    ui->widget->MParamInit_list(pparam, index, iID);

    InitProcessId(PROCESS_LASERKERF, ui->widget_narrow);
    InitProcessId(PROCESS_LASERKERF_D_WIDE, ui->widget_wide);

    mchildwidgets[intptr_t(ui->widget)] = ui->widget;
    mchildwidgets[intptr_t(ui->widget_narrow)] = ui->widget_narrow;
    mchildwidgets[intptr_t(ui->widget_wide)] = ui->widget_wide;
}

void CMWCaliMultiLaserKerf::InitProcessId(int nProcessId, CCtrlParamList* widget)
{
    CProcess_LaserKerf* pKerf = GET_PROCESS(nProcessId,
                                            SM_MAIN, CProcess_LaserKerf);
    Q_ASSERT(pKerf);
    Variant* pparam[30] = { 0 };
    int index = 0;
    pparam[index++] = pKerf->m_Variants.get_Variant(Var_Process_Kerf_BeforeSearchSleep_ID);
    pparam[index++] = pKerf->m_Variants.get_Variant(Var_Process_Kerf_AfterSearchSleep_ID);
    pparam[index++] = pKerf->m_Variants.get_Variant(Var_Process_Kerf_AutoThresRange_ID);
    pparam[index++] = pKerf->m_Variants.get_Variant(Var_Process_Kerf_AutoThresStep_ID);
    pparam[index++] = pKerf->m_Variants.get_Variant(Var_Process_Kerf_EnableAutoThres_ID);


    pparam[index++] = pKerf->m_Variants.get_Variant(Var_Process_LaserKerf_CheckPower);
    pparam[index++] = pKerf->m_Variants.get_Variant(Var_Process_LaserKerf_CheckFreq);
    pparam[index++] = pKerf->m_Variants.get_Variant(Var_Process_LaserKerf_CheckEpulse);
    pparam[index++] = pKerf->m_Variants.get_Variant(Var_Process_LaserKerf_CheckBeamWidth);
    pparam[index++] = pKerf->m_Variants.get_Variant(Var_Process_LaserKerf_Speed);
  

    pparam[index++] = pKerf->m_Variants.get_Variant(Var_Process_KerfParm_LightDirect);
    pparam[index++] = pKerf->m_Variants.get_Variant(Var_Process_KerfParm_LightCircle);
    pparam[index++] = pKerf->m_Variants.get_Variant(Var_Process_KerfParm_Exposure);
    pparam[index++] = pKerf->m_Variants.get_Variant(Var_Process_KerfParm_Gain);

    widget->MParamInit_list(pparam, index);
}


void CMWCaliMultiLaserKerf::MFresh()
{
	CJigDriver* pJigDriver = GET_CARD(JigDriver, CJigDriver);
	QReadLocker l(&pJigDriver->m_Lock);
	ui->txtCurLine->setText(QString::number(pJigDriver->m_iCurPosIdx + 1));
    ui->lblTotalSize->setText(QString::number(pJigDriver->m_MarkListTopLeft.size()));
	l.unlock();
}

void CMWCaliMultiLaserKerf::on_btnChangeLine_clicked()
{
	CJigDriver* pJigDriver = GET_CARD(JigDriver, CJigDriver);
	CProcess_LaserKerf* pKerf = GET_PROCESS(PROCESS_LASERKERF,
		SM_MAIN, CProcess_LaserKerf);
	bool b;
	int idx = ui->txtCurLine->text().toInt(&b);
	if (b)
	{
		pJigDriver->m_iCurPosIdx = idx - 1;
		QTimer::singleShot(500, this, [this]() {
			MFresh();
			});
	}


}

void CMWCaliMultiLaserKerf::on_btnReplaceMarkPanel_clicked()
{
	QMyMessageBox msgBox(QMessageBox::Question, tr("Question"), tr("Install laser calibration panel finish?"), QMessageBox::Yes | QMessageBox::No);
	msgBox.SetMySize(300, 250);
	msgBox.setDefaultButton(QMessageBox::No);
	msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
	msgBox.setButtonText(QMessageBox::Yes, tr("Yes"));
	msgBox.setButtonText(QMessageBox::No, tr("No"));
	int ret = msgBox.exec();
	if (ret != QMessageBox::Yes)
		return;

	V_MsgQueue msgqueue;
	msgqueue.pushback_Msg(VarMsg_Process_ID,
		QObject::trUtf8("Replace Laser mark panel Sig"),
		QVariantList()
		<< PROCESS_LASERKERF
		<< CProcess_LaserKerf::REPLACE_MARKPANEL, 0);

	_SysExt_Instance.start_manual(SM_MAIN, msgqueue, true);
}

void CMWCaliMultiLaserKerf::on_btnMeasureThick_clicked()
{
	int curLineIdx = ui->txtCurLine->text().toInt();
	bool bCut = ui->chkBoxEnableCut->isChecked();
	V_MsgQueue msgqueue;
	msgqueue.pushback_Msg(VarMsg_Process_ID,
		QObject::trUtf8("Laser Kerf measure focus offset"),
		QVariantList()
		<< PROCESS_LASERKERF
		<< CProcess_LaserKerf::MEASURE_THICKNESS
		<< curLineIdx - 1 << bCut, 0);

	_SysExt_Instance.start_manual(SM_MAIN, msgqueue, true);
}