//#include "stable.h"
#include "cmwmanuallaserkerf.h"
#include "ui_cmwmanuallaserkerf.h"
#include "macro_def.h"
#include "logic_compdef.h"
#include "logic_local_def.h"
//#include "cmwcamlight.h"
#include "qmymessagebox.h"
#include "cjig_driver.h"
#include "csystem_external.h"
#include "cworker.h"
#include "cworkerdriver.h"
#include "cprocess_laserkerf.h"
#include "ccam.h"
#include "ccamdriver.h"
#include "cakerfcheck_h.h"
CMWManualLaserKerf::CMWManualLaserKerf(QWidget *parent)
	: CMWidget(parent)
{
	ui = new Ui::CMWManualLaserKerf();
	ui->setupUi(this);
	m_iProcessId = PROCESS_LASERKERF;
}

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

void CMWManualLaserKerf::MInit()
{
	CMWidget::MInit();
}

void CMWManualLaserKerf::MTimeFresh()
{
	if( strCurFile!=_SysExt_Instance.GetCurFile()||
		m_iCurSideID!=_SysExt_Instance.m_iCurSideID||
		m_iCurLayer!= _SysExt_Instance.m_iCurLayer||
		m_iCurCamID!= _SysExt_Instance.m_iCurChID)
	{
		m_iCurCamID= _SysExt_Instance.m_iCurChID;
		strCurFile= _SysExt_Instance.GetCurFile();
		m_iCurSideID= _SysExt_Instance.m_iCurSideID;
		m_iCurLayer= _SysExt_Instance.m_iCurLayer;
		MFresh();
	}
}

void CMWManualLaserKerf::MFresh()
{
	if(_SysExt_Instance.m_iCurChID!=CAM6X)
	{
		showShieldWindow(true,
			QObject::trUtf8("Switch to cam.6X"));
		return;
	}

	CProcess_LaserKerf* pKerf=GET_PROCESS(m_iProcessId,
		SM_MAIN,CProcess_LaserKerf);

	QMutexLocker locker1(&pKerf->m_vKerfParam.mutex);
	KERFPARAM * pKerfParam=pKerf->GetKerfParam();
	if(!pKerfParam)
	{
		showShieldWindow(true,
			QObject::trUtf8("Cur channel do not have kerf param."));
		return;
	}
	showShieldWindow(false);
	ui->lineEdit_RangeMax->
		setText(QString::number(pKerfParam->dRange,'f',3));
	ui->lineEdit_RangeMin->setText(
		QString::number(pKerfParam->dRangeMin,'f',3));
	ui->lineEdit_OffsetMax->
		setText(QString::number(pKerfParam->dOffsetMax,'f',4));
	ui->lineEdit_OffsetMin->
		setText(QString::number(pKerfParam->dOffsetMin,'f',4));

	ui->lineEdit_WidthMax->
		setText(QString::number(pKerfParam->dWidthMax,'f',3));
	ui->lineEdit_WidthMin->
		setText(QString::number(pKerfParam->dWidthMin,'f',3));

	//ui->comboBox_KerfPos->setCurrentIndex(pKerfParam->ikerfPos);
	//TODO:
	if (_SysExt_Instance.m_pfnSetLightData) {
		_SysExt_Instance.m_pfnSetLightData(pKerfParam->iLight1, pKerfParam->iLight2, CAM6X);
	}
	if (_SysExt_Instance.m_pfnSetCamInfo) {
		_SysExt_Instance.m_pfnSetCamInfo(pKerfParam->iExposure, pKerfParam->iGain, CAM6X);
	}

	ui->label_Threshold->setNum(pKerfParam->iPixelThres);

	/*ui->lineEdit_PosOffset->
	setText(QString::number(pKerfParam->dSpecKerfPos,'f',3));*/

	ui->label_Threshold_binary->setNum(pKerfParam->iBinaryThres);
	//ui->checkBox_AutoThres->setChecked(pKerf->m_Variants.get_VData(Var_Process_Kerf_EnableAutoThres_ID)
		//->toBool());

	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();

	CWorker* pWorker=GET_CHDIRECT(M_WORKER,CWorker);
	if(_SysExt_Instance.IsSMAutoRun(SM_MAIN)
		&& pWorker->m_workLevel<LEVEL_ADMINISTRATOR)
	{
		setEnabled(false);
	}
	else{
		setEnabled(true);
	}

	if(pWorker->m_workLevel>=LEVEL_ENGINEER){
		ui->btnChangeLine->setVisible(true);
	}else{
		ui->btnChangeLine->setVisible(false);
	}

	if (pWorker->m_workLevel >= LEVEL_FACTORAY) {
		ui->chkBoxEnableCut->setVisible(true);
	}
	else {
		ui->chkBoxEnableCut->setVisible(false);
	}


	ui->label_19->setVisible(false);
	ui->label_20->setVisible(false);
}

void CMWManualLaserKerf::MApply()
{
    CProcess_LaserKerf* pKerf=GET_PROCESS(m_iProcessId,
                                     SM_MAIN,CProcess_LaserKerf);

    QMutexLocker locker1(&pKerf->m_vKerfParam.mutex);
    KERFPARAM * pKerfParam=pKerf->GetKerfParam();
    Q_ASSERT(pKerfParam);
	pKerfParam->iLineCount = 1;
    pKerfParam->dRange=ui->lineEdit_RangeMax->text().toDouble();
	pKerfParam->dRangeMin = ui->lineEdit_RangeMin->text().toDouble();
    pKerfParam->dOffsetMax=ui->lineEdit_OffsetMax->text().toDouble();
    pKerfParam->dOffsetMin=ui->lineEdit_OffsetMin->text().toDouble();
    pKerfParam->dWidthMax=ui->lineEdit_WidthMax->text().toDouble();
    pKerfParam->dWidthMin=ui->lineEdit_WidthMin->text().toDouble();
    //pKerfParam->ikerfPos=ui->comboBox_KerfPos->currentIndex();
   if (_SysExt_Instance.m_pfnGetLightData) {
	   _SysExt_Instance.m_pfnGetLightData(pKerfParam->iLight1, pKerfParam->iLight2, CAM6X);
   }
   if (_SysExt_Instance.m_pfnGetCamInfo) {
	   _SysExt_Instance.m_pfnGetCamInfo(pKerfParam->iExposure, pKerfParam->iGain, CAM6X);
   }
    pKerfParam->iPixelThres=ui->label_Threshold->text().toDouble();
    pKerfParam->iBinaryThres=ui->label_Threshold_binary->text().toDouble();
	//pKerfParam->dSpecKerfPos=ui->lineEdit_PosOffset->text().toDouble();

    pKerf->SetKerfParam();

	
    //pKerf->m_Variants.get_VData(Var_Process_Kerf_EnableAutoThres_ID)->setValue(ui->checkBox_AutoThres->isChecked());
    //pKerfParam->dSpecKerfPos=ui->lineEdit_SpecPos->text().toDouble();
	pKerfParam->dSpecKerfPos = 0;
}


void CMWManualLaserKerf::SetProcess(int iProcessID)
{
	m_iProcessId = iProcessID;
	if (m_iProcessId == PROCESS_LASERKERF_D_WIDE)
	{
		ui->label_10->setText("D_Wide");
		m_iLaserPath = LASERPATH_D_WIDE;
	}
	else
	{
		ui->label_10->setText("L_Narrow");
		m_iLaserPath = LASERPATH_S_NARROW;
	}
}

void CMWManualLaserKerf::on_pushButton_CheckPos_clicked()
{

	V_MsgQueue msgqueue;
	msgqueue.pushback_Msg(VarMsg_Process_ID,
		QObject::trUtf8("Move Laser Check Pos Sig"),
		QVariantList()
		<< m_iProcessId
		<<CProcess_LaserKerf::MOVEKERFPOS ,0);
	_SysExt_Instance.start_manual(SM_MAIN, msgqueue,true);

}


void CMWManualLaserKerf::on_pushButton_ManualSet_clicked()
{
	V_MsgQueue msgqueue;
	msgqueue.pushback_Msg(VarMsg_Process_ID,
		QObject::trUtf8("Set Laser Kerf Pos Manual Sig"),
		QVariantList()
		<< m_iProcessId
		<<CProcess_LaserKerf::SETKERFPOSMANUAL <<m_iCurSideID
		<<m_iCurLayer,0);

	if(true == _SysExt_Instance.start_manual(SM_MAIN, msgqueue,true)){
		_SysExt_Instance.abort_state(SM_MAIN,
			VarState_Kerf_LaserCheck_Process_ID,
			QVariantList()<< _SysExt_Instance.m_iCurSideID, true);
	}

}

void CMWManualLaserKerf::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()
		<< m_iProcessId
		<<CProcess_LaserKerf::MEASURE_THICKNESS
		<<curLineIdx-1<<bCut, 0);

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

void CMWManualLaserKerf::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 CMWManualLaserKerf::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()
		<<m_iProcessId
		<<CProcess_LaserKerf::REPLACE_MARKPANEL,0);

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

void CMWManualLaserKerf::on_btnKerfCut_clicked()
{
	V_MsgQueue msgqueue;
	/*msgqueue.pushback_Msg(VarMsg_Process_ID,QObject::trUtf8("Cut Kerf"),
	QVariantList()
	<<PROCESS_CUT<<CProcess_Cut::CUT_KERF
	<<m_gSystem.m_iCurSideID<<m_iLineSelected<<0);*/

	msgqueue.pushback_Msg(VarMsg_Process_ID,
		QObject::trUtf8("Cut laser offset check line Sig"),
		QVariantList()
		<<m_iProcessId
		<<CProcess_LaserKerf::CUTKERFLINE
		<< m_iLaserPath, 0);

	msgqueue.pushback_Msg(VarMsg_Process_ID,
		QObject::trUtf8("Move Kerf Pos"),
		QVariantList()
		<<m_iProcessId
		<<CProcess_LaserKerf::MOVEKERFPOS,0);

	msgqueue.pushback_Msg(VarMsg_Process_ID,
		QObject::trUtf8("Laser Cali"),
		QVariantList()
		<<m_iProcessId
		<<CProcess_LaserKerf::KERF
		<< _SysExt_Instance.m_iCurSideID<<0,0);
    _SysExt_Instance.start_manual(SM_MAIN, msgqueue,true);
}

void CMWManualLaserKerf::on_btnTestCur_clicked()
{
	int  iLayer = std::max(m_iCurLayer, 0);
	V_MsgQueue msgqueue;
	msgqueue.pushback_Msg(VarMsg_Process_ID,
		QObject::trUtf8("Search Laser Kerf Line Sig"),
		QVariantList()
		<<m_iProcessId
		<<CProcess_LaserKerf::KERF
		<<m_iCurSideID<<iLayer<<true,0);

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



void CMWManualLaserKerf::on_pushButton_N5_clicked()
{
	TestKerf(-5);
}

void CMWManualLaserKerf::on_pushButton_N1_clicked()
{
	TestKerf(-1);
}

void CMWManualLaserKerf::on_pushButton_P1_clicked()
{
	TestKerf(1);
}

void CMWManualLaserKerf::on_pushButton_P5_clicked()
{
	TestKerf(5);
}

void CMWManualLaserKerf::on_pushButton_Test_clicked()
{
	TestKerf(0);
}

void CMWManualLaserKerf::TestKerf(int offset)
{
	int ivalue= ui->label_Threshold->text().toInt()+offset;
#undef min
	ivalue=std::min(255,ivalue);
	ivalue= std::max(0,ivalue);
	ui->label_Threshold->setNum(ivalue);
	MApply();

	V_MsgQueue msgqueue;
	msgqueue.pushback_Msg(VarMsg_Process_ID,
		QObject::trUtf8("test Laser Kerf Sig"),
		QVariantList()
		<<m_iProcessId
		<<CProcess_LaserKerf::TESTKERF <<m_iCurSideID
		<<m_iCurLayer,0);

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

void CMWManualLaserKerf::TestBinary(int offset)
{
	int ivalue= ui->label_Threshold_binary->text().toInt()+offset;
#undef min
	ivalue=std::min(255,ivalue);
	ivalue= std::max(0,ivalue);

	CProcess_LaserKerf* pKerf=GET_PROCESS(PROCESS_LASERKERF,
		SM_MAIN,CProcess_LaserKerf);
	Q_ASSERT(pKerf);
	CCam* pCam=GET_CHDIRECT(pKerf->m_iKerfCamID,CCam);
	Q_ASSERT(pCam);
	CAKerfCheck_H* pAKerfCheck =
		(CAKerfCheck_H*)pCam->Find_Algorithm(M_KERF_LASER);
	Q_ASSERT(pAKerfCheck);
	CAMorphology* pMorphology=
		pAKerfCheck->GetMorphology(CAMorphology::IMG_BINARY);
	if(!pMorphology)
	{
		_SysExt_Instance.m_SysMsg.SetMsg(tr("did not find binary Morphology"));
		return;
	}

	pMorphology->m_Variants.get_VData(Var_Morphology_Thres_ID)->setValue((double)ivalue);
	ui->label_Threshold_binary->setNum(ivalue);
	try
	{
		CCamDriver* pCamDriver=GET_CARD(CAM_DRIVER,CCamDriver);
		pCamDriver->image_to_container(pKerf->m_iKerfCamID);
		pAKerfCheck->exe_morphology(&pCam->m_imageContainer.m_Image);
	}
	catch(QString& s)
	{
		_SysExt_Instance.m_SysMsg.SetMsg(s);
	}
}

void CMWManualLaserKerf::on_pushButton_N5_binary_clicked()
{
	TestBinary(-5);
}

void CMWManualLaserKerf::on_pushButton_N1_binary_clicked()
{
	TestBinary(-1);
}

void CMWManualLaserKerf::on_pushButton_Test_binary_clicked()
{
	TestBinary(0);
}

void CMWManualLaserKerf::on_pushButton_P1_binary_clicked()
{
	TestBinary(1);
}

void CMWManualLaserKerf::on_pushButton_P5_binary_clicked()
{
	TestBinary(5);
}
