#include "stable.h"
#include "cmwmanualhalfcutkerf.h"
#include "ui_cmwmanualhalfcutkerf.h"
#include "cmwcamlight.h"

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

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

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

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

void CMWManualHalfCutKerf::MFresh()
{
	if(m_gSystem.m_iCurChID!=CAM6X)
	{
		showShieldWindow(true,
			QObject::trUtf8("Switch to cam.6X"));
		return;
	}
	if(m_gSystem.m_iCurSideID!=SIDE_A&&
		m_gSystem.m_iCurSideID!=SIDE_B)
	{
		showShieldWindow(true,
			QObject::trUtf8("Switch to any side."));
		return;
	}

	CProcess_HalfCutKerf* pKerf=GET_PROCESS(PROCESS_HALFKERF,
		SM_MAIN,CProcess_HalfCutKerf);

	int iKerfIndex = m_gSystem.m_iCurSideID==SIDE_A?0:1;
	QMutexLocker locker1(&pKerf->m_vKerfParam.mutex);
	KERFPARAM * pKerfParam=pKerf->m_vKerfParam.mget(iKerfIndex);
	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);


	if(m_gSystem.m_pMWCamLight6X)
	{
		CMWCamLight* pCamLight=(CMWCamLight*)m_gSystem.m_pMWCamLight6X;
		pCamLight->SetLight(pKerfParam->iLight1,pKerfParam->iLight2);
		pCamLight->SetCamInfo(pKerfParam->iExposure, pKerfParam->iGain);
	}

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

	ui->checkBox_AutoThres->setEnabled(false);
	ui->comboBox_KerfPos->setEnabled(false);

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

void CMWManualHalfCutKerf::MApply()
{
	Q_ASSERT(!(m_iCurSideID!=SIDE_A&&m_iCurSideID!=SIDE_B));
	int iKerfIndex= m_iCurSideID==SIDE_A?0:1;
	CProcess_HalfCutKerf* pKerf=GET_PROCESS(PROCESS_HALFKERF,
		SM_MAIN,CProcess_HalfCutKerf);

	QMutexLocker locker1(&pKerf->m_vKerfParam.mutex);
	KERFPARAM * pKerfParam=pKerf->m_vKerfParam.mget(iKerfIndex);
	Q_ASSERT(pKerfParam);

	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(m_gSystem.m_pMWCamLight6X)
	{
		CMWCamLight* pCamLight=(CMWCamLight*)m_gSystem.m_pMWCamLight6X;
		pCamLight->GetLightData(pKerfParam->iLight1,pKerfParam->iLight2);
		pCamLight->GetCamInfo(pKerfParam->iExposure, pKerfParam->iGain);
	}
	pKerfParam->iPixelThres=ui->label_Threshold->text().toDouble();
	pKerfParam->iBinaryThres=ui->label_Threshold_binary->text().toDouble();
	pKerfParam->dSpecKerfPos=ui->lineEdit_PosOffset->text().toDouble();

	pKerf->SetKerfParam(m_iCurSideID);
	pKerf->m_Variants.get_VData(Var_Process_Kerf_EnableAutoThres_ID)->setValue(ui->checkBox_AutoThres->isChecked());
}


void CMWManualHalfCutKerf::on_pushButton_CheckPos_clicked()
{

	V_MsgQueue msgqueue;
	msgqueue.pushback_Msg(VarMsg_Process_ID,
		QObject::trUtf8("Move Halfcut Check Pos Sig"),
		QVariantList()
		<<PROCESS_HALFKERF
		<<CProcess_HalfCutKerf::MOVEKERFPOS ,0);
	m_gSystem.start_manual(SM_MAIN, msgqueue,true);


}


void CMWManualHalfCutKerf::on_pushButton_ManualSet_clicked()
{

	V_MsgQueue msgqueue;
	msgqueue.pushback_Msg(VarMsg_Process_ID,
		QObject::trUtf8("Set Kerf Pos Manual Sig"),
		QVariantList()
		<<PROCESS_HALFKERF
		<<CProcess_HalfCutKerf::SETKERFPOSMANUAL <<m_iCurSideID
		<<m_gSystem.m_iCurLine, 0);

	if(m_gSystem.start_manual(SM_MAIN, msgqueue,true)){
		m_gSystem.abort_state(SM_MAIN,
			VarState_Kerf_HalfCut_Process_ID,
			QVariantList()<<m_gSystem.m_iCurSideID, true);

		m_gSystem.abort_state(SM_MAIN,
			VarState_Align_Check_ID,
			QVariantList()<<m_gSystem.m_iCurSideID, true);
	}

}

void CMWManualHalfCutKerf::on_btnTestCur_clicked()
{
	Q_ASSERT(!(m_iCurSideID!=SIDE_A&&m_iCurSideID!=SIDE_B));

	//int  iLayer = max(m_iCurLayer, 0);
	V_MsgQueue msgqueue;
	msgqueue.pushback_Msg(VarMsg_Process_ID,
		QObject::trUtf8("Search Halfcut Kerf Line Sig"),
		QVariantList()
		<<PROCESS_HALFKERF
		<<CProcess_HalfCutKerf::KERF
		<<m_iCurSideID<<m_gSystem.m_iCurLine,0);

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

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

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

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

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

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

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

	V_MsgQueue msgqueue;
	msgqueue.pushback_Msg(VarMsg_Process_ID,
		QObject::trUtf8("test Halfcut Kerf Sig"),
		QVariantList()
		<<PROCESS_HALFKERF
		<<CProcess_HalfCutKerf::TESTKERF <<m_iCurSideID
		<<m_iCurLayer,0);

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

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

	CProcess_Kerf* pKerf=GET_PROCESS(PROCESS_KERFCHECK,
		SM_MAIN,CProcess_Kerf);
	Q_ASSERT(pKerf);
	CCam* pCam=GET_CHDIRECT(pKerf->m_iKerfCamID,CCam);
	Q_ASSERT(pCam);
	CAKerfCheck_H* pAKerfCheck =
		(CAKerfCheck_H*)pCam->Find_Algorithm(M_KERFCHECK);
	Q_ASSERT(pAKerfCheck);
	CAMorphology* pMorphology=
		pAKerfCheck->GetMorphology(CAMorphology::IMG_BINARY);
	if(!pMorphology)
	{
		m_gSystem.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)
	{
		m_gSystem.m_SysMsg.SetMsg(s);
	}
}

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

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

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

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

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