#include "stable.h"
#include "cmwmanualrecover.h"
#include "ui_cmwmanualrecover.h"
#include "qmymessagebox.h"
#include "vst_semi_main.h"
CMWManualRecover::CMWManualRecover(QWidget *parent)
	: CMWidget(parent)
{
	ui = new Ui::CMWManualRecover();
	ui->setupUi(this);

	picOn = QPixmap(CMakePath::make_bin_path("../theme/led_blue.png"));
	picOff = QPixmap(CMakePath::make_bin_path("../theme/led_red.png"));

	ui->btnPauseCoat->setEnabled(true);
}

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

void CMWManualRecover::MTimeFresh()
{
	QMutexLocker l(&m_gSystem.FindSM(SM_MAIN)->m_SMMutex);
	VST_Semi_Main* pSemiState = (VST_Semi_Main*)(m_gSystem.FindSM(SM_MAIN)->m_pState_Semi);
	int iType = m_gSystem.FindSM(SM_MAIN)->m_pCurState->m_iStateType;
	bool bSemiRun = false;
	if(iType == VS_Thread::State_Semi || iType == VS_Thread::State_Work_Semi
		|| m_gSystem.FindSM(SM_MAIN)->m_pHistoryState_Semi)
	{
		//半自动运行中
		bSemiRun = true;
	}
	l.unlock();

	int iCurOperator = pSemiState->m_iCurOperator;
	if(bSemiRun){
		if(iCurOperator == VarMsg_Recover_StartCutCH1){
			//ui->btnManualCutCh1->setEnabled(true);
			ui->btnManualCutCh1->setChecked(true);
		}else{
			//ui->btnManualCutCh1->setEnabled(false);
			ui->btnManualCutCh1->setChecked(false);
		}
	}else{
		//ui->btnManualCutCh1->setEnabled(true);
		ui->btnManualCutCh1->setChecked(false);
	}

	if(bSemiRun){
		if(iCurOperator == VarMsg_Recover_StartCutCH2){
			//ui->btnManualCutCh2->setEnabled(true);
			ui->btnManualCutCh2->setChecked(true);
		}else{
			//ui->btnManualCutCh2->setEnabled(false);
			ui->btnManualCutCh2->setChecked(false);
		}
	}else{
		//ui->btnManualCutCh2->setEnabled(true);
		ui->btnManualCutCh2->setChecked(false);
	}
}

void CMWManualRecover::MFresh()
{
	CMWidget::MFresh();
	CWorker* pWorker=GET_CHDIRECT(M_WORKER,CWorker);
	if(pWorker->m_workLevel<LEVEL_ENGINEER)
	{
		ui->btnRedoCut->setEnabled(false);
		ui->btnRedoCoat->setEnabled(false);
	}else{
		ui->btnRedoCut->setEnabled(true);
		ui->btnRedoCoat->setEnabled(true);
	}

	if(pWorker->m_workLevel<LEVEL_ADMINISTRATOR){
		ui->btnPauseCoat->setVisible(false);
	}else{
		ui->btnPauseCoat->setVisible(true);
	}
}


void CMWManualRecover::on_btnReadRecoverData_clicked()
{
	if(!m_gSystem.IsAllSMStopped()){
		m_gSystem.m_SysMsg.SetMsg(tr("Stop before operation"));
		return;
	}

	CProcess_Trans* pTrans=GET_PROCESS(PROCESS_TRANS,
		SM_TRANS,CProcess_Trans);
	pTrans->ReadRecoveredData();

	CProductSide* pSideA =GET_CHDIRECT(SIDE_A,CProductSide);
	pSideA->ReadRecoveredData();

	CProductSide* pSideB =GET_CHDIRECT(SIDE_B,CProductSide);
	pSideB->ReadRecoveredData();

	CCassette* pCass=GET_CHDIRECT(CASSETTE,CCassette);
	Q_ASSERT(pCass);
	pCass->ReadRecoveredData();

	QString recipeName;
	QString slotNo;
	m_gSystem.ReadRecoverData(recipeName, slotNo);

	ui->lblRecipeName->setText(recipeName);
	ui->lblLotNo->setText(slotNo);

	//m_gSystem.Reset_All_Side(true);
	CProcess_Cut* pProcessCut=GET_PROCESS(PROCESS_CUT,SM_MAIN,CProcess_Cut);
	int iSideId, iLineId, iLayer, iTimes, iLineSize, iLayerSize, iTimesSize,iEndLine;
	pProcessCut->ReadRealtimeData((int)SIDE_A, iLineId, iLayer, iTimes, iLineSize, iLayerSize, iTimesSize,iEndLine);
	if((iLineId == iEndLine-1 || iLineId >= iLineSize-1) && iLayer == iLayerSize-1 && iTimes == iTimesSize-1){
		//已经处理完了
		CProductSideDriver* pSideDriver=GET_CARD(SIDE_DRIVER,
			CProductSideDriver);
		Q_ASSERT(pSideDriver);
		pSideDriver->ResetSide(SIDE_A, true);
		ui->lblSideAFinish->setPixmap(picOn);
		ui->btnManualCutCh1->setEnabled(false);
	}else{
		ui->lblSideAFinish->setPixmap(picOff);
		ui->btnManualCutCh1->setEnabled(true);
	}

	//如果当前层的最后一刀
	if(iTimes == iTimesSize-1){
		if(iLayer == iLayerSize-1){
			iLineId += 1;
			iLayer = 0;
			iTimes = 0;
		}else{
			iLayer+=1;
			iTimes = 0;
		}
	}else{
		//取下一次
		iTimes += 1;
	}
	ui->txtLine1->setText(QString::number(iLineId+1));
	ui->txtPass1->setText(QString::number(iLayer+1));
	ui->txtTimes1->setText(QString::number(iTimes+1));
	ui->lblEndLineA->setText(QString::number(iEndLine)); 

	pProcessCut->ReadRealtimeData((int)SIDE_B, iLineId, iLayer, iTimes, iLineSize, iLayerSize, iTimesSize,iEndLine);
	if((iLineId == iEndLine-1 || iLineId >= iLineSize-1) && iLayer == iLayerSize-1 && iTimes == iTimesSize-1){
		//已经处理完了
		CProductSideDriver* pSideDriver=GET_CARD(SIDE_DRIVER,
			CProductSideDriver);
		Q_ASSERT(pSideDriver);
		pSideDriver->ResetSide(SIDE_B, true);
		ui->lblSideBFinish->setPixmap(picOn);
		ui->btnManualCutCh2->setEnabled(false);
	}else{
		ui->lblSideBFinish->setPixmap(picOff);
		ui->btnManualCutCh2->setEnabled(true);
	}
	//如果当前层的最后一刀
	if(iTimes == iTimesSize-1){
		if(iLayer == iLayerSize-1){
			iLineId += 1;
			iLayer = 0;
			iTimes = 0;
		}else{
			iLayer+=1;
			iTimes = 0;
		}
	}else{
		//取下一次
		iTimes += 1;
	}

	ui->txtLine2->setText(QString::number(iLineId+1));
	ui->txtPass2->setText(QString::number(iLayer+1));
	ui->txtTimes2->setText(QString::number(iTimes+1));
	ui->lblEndLineB->setText(QString::number(iEndLine)); 
}

void CMWManualRecover::on_btnManualCutCh1_clicked()
{
	int iStart=ui->txtLine1->text().toInt()-1;
	int iPass = ui->txtPass1->text().toInt()-1;
	int iTimes = ui->txtTimes1->text().toInt()-1;
	int iEndLine = ui->lblEndLineA->text().toInt()-1;
	ManualCut(SIDE_A, iStart, iPass, iTimes, iEndLine);
}

void CMWManualRecover::on_btnManualCutCh2_clicked()
{
	int iStart=ui->txtLine2->text().toInt()-1;
	int iPass = ui->txtPass2->text().toInt()-1;
	int iTimes = ui->txtTimes2->text().toInt()-1;
	int iEndLine = ui->lblEndLineB->text().toInt()-1;
	ManualCut(SIDE_B, iStart, iPass, iTimes, iEndLine);
}

void CMWManualRecover::on_btnChangeCh1_clicked()
{
	V_MsgQueue msgqueue;
	msgqueue.pushback_Msg(VarMsg_System_ChangeSide,
		QObject::trUtf8("Change Side A"),
		QVariantList()<<SIDE_A,0);
	CWorkTable* pTable=(CWorkTable*)m_gSystem.FindChannel(WORKTABLE);
	Q_ASSERT(pTable);
	CCoordTrans* pCoord= pTable->FindCoord(CAM6X);
	Q_ASSERT(pCoord);
	m_gSystem.absmove_xy_msg(pCoord->m_dOffsetX, pCoord->m_dOffsetY, msgqueue);
	m_gSystem.changeChannel_msg(CAM6X,true,msgqueue);
	m_gSystem.start_manual(SM_MAIN, msgqueue,true);
}

void CMWManualRecover::on_btnChangeCh2_clicked()
{
	V_MsgQueue msgqueue;
	msgqueue.pushback_Msg(VarMsg_System_ChangeSide,
		QObject::trUtf8("Change Side B"),
		QVariantList()<<SIDE_B, 0);
	CWorkTable* pTable=(CWorkTable*)m_gSystem.FindChannel(WORKTABLE);
	Q_ASSERT(pTable);
	CCoordTrans* pCoord= pTable->FindCoord(CAM6X);
	Q_ASSERT(pCoord);
	m_gSystem.absmove_xy_msg(pCoord->m_dOffsetX, pCoord->m_dOffsetY, msgqueue);
	m_gSystem.changeChannel_msg(CAM6X,true,msgqueue);
	m_gSystem.start_manual(SM_MAIN, msgqueue,true);
}

void CMWManualRecover::ManualCut(int iSideId, int iStart, int iLayerId, int iTimes, int iEndLine)
{
	CProductSide* pSide=GET_CHDIRECT(iSideId,CProductSide);
	Q_ASSERT(pSide);
	QMutexLocker locker(&pSide->m_CutLineVector.mutex);
	int isizeLine=pSide->m_CutLineVector.size();
	locker.unlock();
	
	if(OUTOFRANGE(iStart,0,isizeLine))
	{
		m_gSystem.m_SysMsg.SetMsg(tr("line id is not valid"));
		return;
	}

	int operatorId = VarMsg_Recover_StartCutCH1;
	if(iSideId == SIDE_B)
		operatorId = VarMsg_Recover_StartCutCH2;

	V_MsgQueue msgqueue;
	msgqueue.pushback_Msg(VarMsg_StartSemi_ID,
		QObject::trUtf8("Single Cut Sig"),
		QVariantList()<<operatorId<<iSideId<<iStart<<iEndLine<<true<<iLayerId<<iTimes);

	int curOperateID = ((VST_Semi_Main*)(m_gSystem.FindSM(SM_MAIN)->m_pState_Semi))->m_iCurOperator;
	int ret = QMessageBox::Yes;
	if(curOperateID == operatorId){
		QMyMessageBox msgBoxAsk(QMessageBox::Question, tr("Confirm information"),tr("Operate not finish\n\nSelect what you want to do?"),
			QMessageBox::Yes | QMessageBox::Abort | QMessageBox::Cancel);
		//msgBoxAsk.setStandardButtons();
		msgBoxAsk.setDefaultButton(QMessageBox::Cancel);
		msgBoxAsk.setButtonText(QMessageBox::Yes, tr("Continue"));
		msgBoxAsk.setButtonText(QMessageBox::Abort, tr("Abort"));
		msgBoxAsk.setButtonText(QMessageBox::Cancel, tr("Cancel"));
		ret = msgBoxAsk.exec();
	}
	if(ret == QMessageBox::Abort){
		m_gSystem.StopSemi(SM_MAIN);
	}else if(ret == QMessageBox::Yes){
		if(curOperateID != -1 && curOperateID != operatorId)
		{
			QMessageBox::information(this, tr("Information"), tr("Another operation running, operation canceled"));
			return;
		}
		else{
			if(QMessageBox::Yes == QMessageBox::question(this,tr("Confirm information"),tr("Are you sure recover cut?\nPlease check line at first."), 
				QMessageBox::Yes|QMessageBox::No)){
				m_gSystem.start_manual(SM_MAIN, msgqueue, true);
			}
		}
	}else{
		return;
	}
}

void CMWManualRecover::on_btnRedoCut_clicked()
{
	VSMT_Base* pSM= (VSMT_Base*)m_gSystem.FindSM(SM_MAIN);
	
	if(m_gSystem.FindSM(SM_MAIN)->IsStop())
	{
		QMessageBox::information(this, QObject::tr("Information"), QObject::tr("Cut worktable not worked, can't do this operating"));
		return ;
	}

	QMyMessageBox msgBoxAsk(QMessageBox::Question, tr("Confirm information"),tr("Are you sure to redo cutting? Notice:The cutted street will be repeatedly cut"),
		QMessageBox::Yes | QMessageBox::No);
	//msgBoxAsk.setStandardButtons();
	msgBoxAsk.SetMySize(350, 250);
	msgBoxAsk.setDefaultButton(QMessageBox::No);
	msgBoxAsk.setButtonText(QMessageBox::Yes, tr("Yes"));
	msgBoxAsk.setButtonText(QMessageBox::No, tr("No"));
	int ret = msgBoxAsk.exec();
	if(ret == QMessageBox::Yes){
		pSM->pushfront_Msg(VarMsg_RestartAutoCut, QObject::trUtf8("Resume auto cut sig")); 
	}
}

void CMWManualRecover::on_btnRedoCoat_clicked()
{
	VSMT_Base* pSM= (VSMT_Base*)m_gSystem.FindSM(SM_COAT);
	if(m_gSystem.FindSM(SM_COAT)->IsStop())
	{
		QMessageBox::information(this, QObject::tr("Information"), QObject::tr("Coat worktable not worked, can't do this operating"));
		return ;
	}

	QMyMessageBox msgBoxAsk(QMessageBox::Question, tr("Confirm information"),tr("Are you sure to redo coating?"),
		QMessageBox::Yes | QMessageBox::No);
	//msgBoxAsk.setStandardButtons();
	msgBoxAsk.setDefaultButton(QMessageBox::No);
	msgBoxAsk.setButtonText(QMessageBox::Yes, tr("Yes"));
	msgBoxAsk.setButtonText(QMessageBox::No, tr("No"));
	int ret = msgBoxAsk.exec();
	if(ret == QMessageBox::Yes){
		pSM->pushfront_Msg(VarMsg_RestartAutoCoat, QObject::trUtf8("Resume auto coat sig")); 
	}
}

void CMWManualRecover::on_btnPauseCoat_clicked()
{
	VSMT_Base* pSM= (VSMT_Base*)m_gSystem.FindSM(SM_COAT);
	if(m_gSystem.FindSM(SM_COAT)->IsStop())
		return;

	pSM->pushfront_Msg(V_Msg(VarMsg_Pause_ID,QObject::trUtf8("Pause Coat Sig")));
}

void CMWManualRecover::on_btnPauseClean_clicked()
{
	VSMT_Base* pSM= (VSMT_Base*)m_gSystem.FindSM(SM_CLEAN);
	if(m_gSystem.FindSM(SM_CLEAN)->IsStop())
		return;

	pSM->pushfront_Msg(V_Msg(VarMsg_Pause_ID,QObject::trUtf8("Pause Clean Sig")));
}

void CMWManualRecover::on_btnPauseTrans_clicked()
{
	VSMT_Base* pSM= (VSMT_Base*)m_gSystem.FindSM(SM_TRANS);
	if(m_gSystem.FindSM(SM_TRANS)->IsStop())
		return;

	pSM->pushfront_Msg(V_Msg(VarMsg_Pause_ID,QObject::trUtf8("Pause Trans Sig")));
}

void CMWManualRecover::on_btnAbortCut_clicked()
{
	VSMT_Base* pSM= (VSMT_Base*)m_gSystem.FindSM(SM_MAIN);
	if(pSM->IsStop())
		return;

	if(QMessageBox::Yes != QMessageBox::question(this, QObject::tr("Confirm"), 
		QObject::tr("Are you sure abort cut process? Operating will cancel cutting,you must remove wafer and reset cut table")))
	{
		return;
	}
	pSM->pushfront_Msg(V_Msg(VarMSg_AbortAutoCut,QObject::trUtf8("Abort Auto Cut Sig")));
}

void CMWManualRecover::on_btnAbortCoat_clicked()
{
	 VSMT_Base* pSM= (VSMT_Base*)m_gSystem.FindSM(SM_COAT);
	 if(pSM->IsStop())
		 return;

	 if(QMessageBox::Yes != QMessageBox::question(this, QObject::tr("Confirm"), 
		 QObject::tr("Are you sure abort coat process? Operating will cancel coating,you must remove wafer and reset coat table")))
	 {
		 return;
	 }
	 pSM->pushfront_Msg(V_Msg(VarMsg_AbortAutoCoat,QObject::trUtf8("Abort Coat Sig")));
}

void CMWManualRecover::on_btnAbortClean_clicked()
{
	VSMT_Base* pSM= (VSMT_Base*)m_gSystem.FindSM(SM_CLEAN);
	if(pSM->IsStop())
		return;

	if(QMessageBox::Yes != QMessageBox::question(this, QObject::tr("Confirm"), 
		QObject::tr("Are you sure abort clean process? Operating will cancel cleaning,you must remove wafer and reset clean table")))
	{
		return;
	}
	pSM->pushfront_Msg(V_Msg(VarMsg_AbortAutoClean,QObject::trUtf8("Abort Clean Sig")));
}

void CMWManualRecover::on_btnAbortTrans_clicked()
{
	//暂停状态时才能取消
	//当在VST_Wait_Transfer状态时才能取消传片
	//当在VarState_Wait_Transfer状态时
	VSMT_Base* pSM= (VSMT_Base*)m_gSystem.FindSM(SM_TRANS);
	if(false == m_gSystem.IsSMAutoPaused(SM_TRANS)){
		QMessageBox::information(this, tr("Information"), tr("Must auto paused status can stop transfer."), QMessageBox::Ok);
		return ;
	}
	if(QMessageBox::Yes != QMessageBox::question(this, QObject::tr("Confirm"), 
		QObject::tr("Are you sure cancel current transfer process? Operating will cancel transfer,you must remove wafer.")))
	{
		return;
	}
	//取消掉Wait状态，转换到VST_Wait_Transfer状态
	m_gSystem.abort_state(SM_TRANS, VarState_Work_Transfer, QVariantList()<<m_gSystem.m_iCurSideID, true);
}

void CMWManualRecover::on_btnStartCut_clicked()
{
	VSMT_Base* pSM= (VSMT_Base*)m_gSystem.FindSM(SM_MAIN);
	if(QMessageBox::Yes != QMessageBox::question(this, QObject::tr("Confirm"), 
		QObject::tr("Are you sure start auto cut process immediately?")))
	{
		return;
	}

	pSM->pushfront_Msg(V_Msg(VarMsg_StartAuto_ID,QObject::trUtf8("Start Cut Sig")));
}

void CMWManualRecover::on_btnStartCoat_clicked()
{
	if(false == m_gSystem.IsSMAutoMode(SM_TRANS)){
		QMessageBox::information(this, tr("Information"), tr("Transfer not started"), QMessageBox::Ok);
		return;
	}

	VSMT_Base* pSM= (VSMT_Base*)m_gSystem.FindSM(SM_COAT);
	if(QMessageBox::Yes != QMessageBox::question(this, QObject::tr("Confirm"), 
		QObject::tr("Are you sure start auto coat process immediately?")))
	{
		return;
	}

	pSM->pushfront_Msg(V_Msg(VarMsg_StartAuto_ID,QObject::trUtf8("Start Coat Sig")));
}

void CMWManualRecover::on_btnStartClean_clicked()
{
	if(false == m_gSystem.IsSMAutoMode(SM_TRANS)){
		QMessageBox::information(this, tr("Information"), tr("Transfer not started"), QMessageBox::Ok);
		return;
	}
	VSMT_Base* pSM= (VSMT_Base*)m_gSystem.FindSM(SM_CLEAN);
	if(QMessageBox::Yes != QMessageBox::question(this, QObject::tr("Confirm"), 
		QObject::tr("Are you sure start auto clean process immediately?")))
	{
		return;
	}

	pSM->pushfront_Msg(V_Msg(VarMsg_StartAuto_ID,QObject::trUtf8("Start Clean Sig")));
}

void CMWManualRecover::on_btnStartTrans_clicked()
{
	VSMT_Base* pSM= (VSMT_Base*)m_gSystem.FindSM(SM_TRANS);
	if(QMessageBox::Yes != QMessageBox::question(this, QObject::tr("Confirm"), 
		QObject::tr("Are you sure start auto trans process immediately?")))
	{
		return;
	}
	pSM->pushfront_Msg(V_Msg(VarMsg_StartAuto_ID,QObject::trUtf8("Start Trans Sig")));
}

