#include "vst_semi_main.h"
#include "logic.h"
#include "vsm_thread.h"
#include "vst_work_main.h"
VST_Semi_Main::VST_Semi_Main(int iID, const QString &sName,
                             VSM_Thread *pParent)
    :VST_Semi(  iID,  sName, pParent)
{
	m_iCurOperator = -1;
}

//半自动划片，跟自动划片一致
void VST_Semi_Main::do_Msg(const V_Msg &msg)
{
    switch(msg.m_iID)
    {
    case VarMsg_StartSemi_ID:
    {
		Q_ASSERT(msg.m_vlInput.size()>=1);
		m_iCurOperator = msg.m_vlInput[0].toInt();
		//检查安全门关闭
		try{
			m_gSystem.SafeDoorCheck();
		}catch(const QString& ex){
			m_gSystem.CloseSafeDoor();

			throw ex;
		}

		if(m_iCurOperator == VarMsg_StartSemi_ID)
		{
#if 1
			if(!m_pNextState)
			{
				CMProduct* pProduct=GET_CHDIRECT(PRODUCT,CMProduct);
				bool bfirstCutB= pProduct->m_Variants.get_VData(Var_MProduct_FirstCutb_ID)->toBool();
				int iFirstSide=bfirstCutB?SIDE_B:SIDE_A;
				int iSecondSide=!bfirstCutB?SIDE_B:SIDE_A;

				m_pNextState= new VST_Work_Main(VarState_Work_Main,
												QObject::trUtf8("Single Cell Cut"),
												m_pParent,this,0);

				((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
					pushback_Msg(VarMsg_Resume_Process_Main_ID,
					QObject::trUtf8("Cut_Process"), QVariantList());

                ((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
                    pushback_Msg(VarMsg_Driver_ID,
                                 QObject::trUtf8("Check lens protect flow volume "), QVariantList() << LASERPATH_DRIVER
                                 << CLaserPathDriver::CHECKLESNPROTECT << LASERPATH_D_WIDE);

				//初始化卡抓
				((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
					pushback_Msg(VarMsg_Driver_ID,
						QObject::trUtf8("Move Path init angle"), QVariantList() << LASERPATH_DRIVER
						<< CLaserPathDriver::MOVEPATHINITANGLE << LASERPATH_D_WIDE);

				((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
						pushback_Msg(VarMsg_Locate_Process_ID,
									 QObject::trUtf8("Locate_Process"));

				//测试在Location完成后暂停
				if(m_gSystem.m_bPausedAfterLocation)
				{
					((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
						pushback_Msg(VarMsg_Locate_Process_PauseAfter_ID,
						QObject::trUtf8("Cut_Process"), QVariantList());

					//恢复工作
					((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
						pushback_Msg(VarMsg_Resume_Process_Main_ID,
						QObject::trUtf8("Cut_Process"), QVariantList());
				}

				bool bEnableFirst=true;
				bool bEnableSecond=true;
				if(false == bfirstCutB){
					if(m_gSystem.m_bOnlyASide){
						bEnableSecond = false;
					}
					if(m_gSystem.m_bOnlyBSide){
						bEnableFirst = false;
					}
				}else{
					if(m_gSystem.m_bOnlyASide){
						bEnableFirst = false;
					}
					if(m_gSystem.m_bOnlyBSide){
						bEnableSecond = false;
					}
				}

				//仅定位 
				if(!m_gSystem.m_bOnlyLocation)
				{
					//调平第一面
					if(bEnableFirst){
						if(true == m_gSystem.m_bSimulatorLeveling)
						{
							//模拟时，直接生成切割线
							((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
								pushback_Msg(VarMsg_Debug_Align_Process_ID,
								QObject::trUtf8("Align_Process"),
								QVariantList()<<iFirstSide);
						}else{

							((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
									pushback_Msg(VarMsg_Align_Process_ID,
												 QObject::trUtf8("Align_Process"),
												 QVariantList()<<iFirstSide);
						}
					}
					//调平第二面
					if(bEnableSecond){
						if(true == m_gSystem.m_bSimulatorLeveling)
						{
							//模拟时，直接生成切割线
							((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
								pushback_Msg(VarMsg_Debug_Align_Process_ID,
								QObject::trUtf8("Align_Process"),
								QVariantList()<<iSecondSide);
						}else{

							((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
								pushback_Msg(VarMsg_Align_Process_ID,
								QObject::trUtf8("Align_Process"),
								QVariantList()<<iSecondSide);
						}
					}

					//仅调平
					if(!m_gSystem.m_bOnlyLeveling)
					{
						//切割前暂停
						if(m_gSystem.m_bPausedAfterLeveling)
						{
							//pushback_Msg(VarMsg_Pause_ID, QObject::trUtf8("Pause Sig"));
							((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
								pushback_Msg(VarMsg_Align_Process_PauseAfter_ID,
								QObject::trUtf8("Cut_Process"), QVariantList());

							//恢复工作
							((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
								pushback_Msg(VarMsg_Resume_Process_Main_ID,
								QObject::trUtf8("Cut_Process"), QVariantList());

							//pushback_Msg(VarMsg_Pause_ID, QObject::trUtf8("Pause Sig"));
						}
						if(bEnableFirst){
							((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
								pushback_Msg(VarMsg_Cut_Process_Before_ID, QObject::trUtf8("Before cut record data"), QVariantList()<<iFirstSide);
						}
						if(bEnableSecond){
							((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
								pushback_Msg(VarMsg_Cut_Process_Before_ID, QObject::trUtf8("Before cut record data"), QVariantList()<<iSecondSide);
						}


						//值为0时不生效
						CProcess_Level* pLevel=GET_PROCESS(PROCESS_LEVELING, SM_MAIN,CProcess_Level);
						bool bOk;
						int iStartA = pLevel->m_Variants.get_VData(Var_Process_ProductSide_CutStartA)->toInt(&bOk);
						int iEndA = pLevel->m_Variants.get_VData(Var_Process_ProductSide_CutEndA)->toInt(&bOk);
						int iStartB = pLevel->m_Variants.get_VData(Var_Process_ProductSide_CutStartB)->toInt(&bOk);
						int iEndB = pLevel->m_Variants.get_VData(Var_Process_ProductSide_CutEndB)->toInt(&bOk);
						int iCutFirstStart = iStartA;
						int iCutFirstEnd = iEndA;
						int iCutSecondStart = iStartB;
						int iCutSencondEnd =iEndB;
						if(bfirstCutB){
							iCutFirstStart = iStartB;
							iCutFirstEnd = iEndB;
							iCutSecondStart = iStartA;
							iCutSencondEnd =iEndA;
						}

						/*LOG(INFO)<<"Semi Cut Line:First: "<<iCutFirstStart+1<<" To "<<iCutFirstEnd+1<<" Second:"<<iCutSecondStart+1<<" To " <<iCutSencondEnd+1;*/
						if(bEnableFirst){

							((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
									pushback_Msg(VarMsg_Cut_Process_ID,
												 QObject::trUtf8("Cut_Process"),
												 QVariantList()<<iFirstSide<<iCutFirstStart-1<<iCutFirstEnd-1<<true<<0<<0);
						 }
						if(bEnableSecond){
							((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
									pushback_Msg(VarMsg_Cut_Process_ID,
												 QObject::trUtf8("Cut_Process"),
												 QVariantList()<<iSecondSide<<iCutSecondStart-1<<iCutSencondEnd-1<<true<<0<<0);
							

						}

					}
				}

				 ((VST_Work_Main*)m_pNextState)->LoadMsgProcess();

			}
			pushback_Msg(VarMsg_Cut_Finish_ID,
								 QObject::trUtf8("Cut Finish"));
			m_pParent->SetState(m_pNextState);
			//调平前，重置切割道
			m_gSystem.Reset_All_Side();

			/////开始计时
			m_gSystem.m_cellTime.ClearTime();
			m_gSystem.m_cellTime.StartTime();
			///加工开始数据库记录
			CProductSide* pSideA=GET_CHDIRECT(SIDE_A,CProductSide);
			double dStepA=pSideA->m_Variants.get_VData(Var_ProductSide_SideStep)
					->toDouble();
			CProductSide* pSideB=GET_CHDIRECT(SIDE_B,CProductSide);
			double dStepB=pSideB->m_Variants.get_VData(Var_ProductSide_SideStep)
					->toDouble();
			QString str=QObject::trUtf8("DNo:%1,INDEX1:%3,INDEX2:%4")
					.arg(m_gSystem.GetCurFile())
					.arg(dStepA*1000)
					.arg(dStepB*1000);
			m_gSystem.EvtLog(QString::fromUtf8("PRO_SEMI"),str);
			///////////////加工开始数据库记录 结束
#endif
		}else if(m_iCurOperator == VarMsg_Manual_StartCut
			|| m_iCurOperator == VarMsg_Recover_StartCutCH1
			|| m_iCurOperator == VarMsg_Recover_StartCutCH2)
		{
			Q_ASSERT(msg.m_vlInput.size()==7);
			int iSide = msg.m_vlInput[1].toInt();
			int iStartLine = msg.m_vlInput[2].toInt();
			int iEndLine = msg.m_vlInput[3].toInt();
			int bKerfCheck = msg.m_vlInput[4].toBool();
			int iLayerId = msg.m_vlInput[5].toInt();
			int iTimes = msg.m_vlInput[6].toInt();
			if(!m_pNextState)
			{
				CMProduct* pProduct=GET_CHDIRECT(PRODUCT,CMProduct);
				m_pNextState= new VST_Work_Main(VarState_Work_Main,
					QObject::trUtf8("Manual Cut Finished"),
					m_pParent,this,0);

				
				((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
					pushback_Msg(VarMsg_Cut_Process_ID,
					QObject::trUtf8("Cut_Process"),
					QVariantList()<<iSide<<iStartLine<<iEndLine<<bKerfCheck<<iLayerId<<iTimes);
			}

			((VST_Work_Main*)m_pNextState)->LoadMsgProcess();

			//手动切割
			if(m_iCurOperator == VarMsg_Recover_StartCutCH1){
				pushback_Msg(VarMsg_Cut_Finish_ID, QObject::trUtf8("Recover Cut CH1 Finish"));
			}
			else if(m_iCurOperator == VarMsg_Recover_StartCutCH2){
				pushback_Msg(VarMsg_Cut_Finish_ID, QObject::trUtf8("Recover Cut CH2 Finish"));
			}
			else{
				pushback_Msg(VarMsg_Cut_Finish_ID, QObject::trUtf8("Manual Cut Finish"));
			}
			m_pParent->SetState(m_pNextState);

			/////开始计时
			m_gSystem.m_cellTime.ClearTime();
			m_gSystem.m_cellTime.StartTime();
			///加工开始数据库记录
			CProductSide* pSideA=GET_CHDIRECT(SIDE_A,CProductSide);
			double dStepA=pSideA->m_Variants.get_VData(Var_ProductSide_SideStep)
				->toDouble();
			CProductSide* pSideB=GET_CHDIRECT(SIDE_B,CProductSide);
			double dStepB=pSideB->m_Variants.get_VData(Var_ProductSide_SideStep)
				->toDouble();
			if(SIDE_A == iSide){
				QString str=QObject::trUtf8("DNo:%1,INDEX1:%3,Start:%4,End:%5")
					.arg(m_gSystem.GetCurFile())
					.arg(dStepA*1000)
					.arg(iStartLine)
					.arg(iEndLine);
				m_gSystem.EvtLog(QString::fromUtf8("PRO_SEMI"),str);
			}else {
				QString str=QObject::trUtf8("DNo:%1,INDEX2:%3,Start:%4,End:%5")
					.arg(m_gSystem.GetCurFile())
					.arg(dStepB*1000)
					.arg(iStartLine)
					.arg(iEndLine);;
				m_gSystem.EvtLog(QString::fromUtf8("PRO_SEMI"),str);
			}
			///////////////加工开始数据库记录 结束
		}

        break;
    }
    case VarMsg_Cut_Finish_ID:
    {
        /////加工结束计时
        m_gSystem.m_cellTime.StopTime();

        CMWorkTableDriver* pWorkTableDriver=GET_CARD(WORKTABLE_DRIVER,
                                                     CMWorkTableDriver);
        CProcess_Level* pLevel=GET_PROCESS(PROCESS_LEVELING,
                                           SM_MAIN,
                                           CProcess_Level);
        Q_ASSERT(pWorkTableDriver);
        CMPointd Axis;
        pWorkTableDriver->WtToAxis(CAM6X,pLevel->m_AlignPtBase.Pt,Axis);
        m_gSystem.absmove_xy(Axis.tX,Axis.tY);
        m_gSystem.changeChannel(CAM6X,false);

        ///加工结束数据库记录
        QString str=QObject::trUtf8("DNo:%1,TM:%2")
                .arg(m_gSystem.GetCurFile())
                .arg(m_gSystem.m_cellTime.FormatTime()) ;
        m_gSystem.EvtLog(QString::fromUtf8("PRO_SEMI"),str);

		

		//如果循环跑的话就给自己再发送一个自动消息
        if(!m_gSystem.m_bEnableCycle)
        {
			if(m_iCurOperator == VarMsg_Manual_StartCut)
				m_gSystem.m_SysMsg.SetMsg(QObject::trUtf8("Manual Cut Finished"));
			else if(m_iCurOperator == VarMsg_Recover_StartCutCH1){
				m_gSystem.m_SysMsg.SetMsg(QObject::trUtf8("Recover Cut CH1 Finish"));
			}else if(m_iCurOperator == VarMsg_Recover_StartCutCH2){
				m_gSystem.m_SysMsg.SetMsg(QObject::trUtf8("Recover Cut CH2 Finish"));
			}
			else
				m_gSystem.m_SysMsg.SetMsg(QObject::trUtf8("Single Cut Finished"));

            m_pParent->pushfront_Msg(VarMsg_StopSemi_ID,QObject::trUtf8("Stop Single Cut Sig"));
        }
        else
        {
            m_pParent->clear_Msg();
            clear_Msg();
            pushfront_Msg(VarMsg_StartSemi_ID,
                          QObject::trUtf8("Single Cut Sig"),
						  QVariantList()<<VarMsg_StartSemi_ID);
        }
		m_iCurOperator = -1;
        break;
    }
    }
    VST_Semi::do_Msg(msg);
}

