#include "vst_idle_main.h"
#include "logic.h"
#include "vst_work_main.h"
#include "cprocess_laserpowercali.h"
VST_Idle_Main::VST_Idle_Main(int iID, const QString& sName,
                             VSM_Thread* pParent)
    :VST_Idle_Base(iID, sName, pParent)
{

}

void VST_Idle_Main::do_Msg(const V_Msg& msg)
{
    //    if(msg.m_iID!=0){
    //        int id = msg.m_iID;
    //    }
    switch(msg.m_iID) {
        case VarMsg_Driver_ID:
        {
            V_Msg msg1 = msg;
            Q_ASSERT(msg1.m_vlInput.size() >= 2);
            int iDriverID = msg1.m_vlInput.at(0).toInt();
            int iDriverMsgID = msg1.m_vlInput.at(1).toInt();
            msg1.m_vlInput.pop_front();
            msg1.m_vlInput.pop_front();
            CDriverCard* pDriverCard = GET_CARD(iDriverID, CDriverCard);
            Q_ASSERT(pDriverCard);
            pDriverCard->do_msg(iDriverMsgID, msg1.m_vlInput, msg1.m_pvlOutput);
            break;
        }
        case VarMsg_Calibration:
        {
            //TODO:定位操作
    //        m_gSystem.absmove_xy_msg();
    //        pushback_Msg(VarMsg_Driver_ID, );
    //        pushback_Msg();
            //测量功率
            if(!m_pNextState) {
                m_pNextState = new VST_Work_Main(VarState_Calibration_ID,
                                                 QObject::trUtf8("Camera Calibration"),
                                                 m_pParent, this, 0);


                ((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
                    pushback_Msg(VarMsg_Process_ID,
                                 QObject::trUtf8("Camera Calibration"),
                                 QVariantList() << PROCESS_CUT
                                 << CProcess_Cut::CALIBRATION
                                 << msg.m_vlInput[0],
                                 msg.m_pvlOutput);

                ((VST_Work_Main*)m_pNextState)->LoadMsgProcess();
            }
            m_pParent->SetState(m_pNextState, false);
            break;
        }
		case VarMsg_PowerCali_BeamRange_ID:
		{
			//测量功率
			int iProcessID = msg.m_vlInput[0].toInt();
			CProcess_LaserPowerCali* pProcess_PowerCali = (CProcess_LaserPowerCali*)m_pParent->FindProcess(iProcessID);
			Q_ASSERT(pProcess_PowerCali);
			if (!m_pNextState)
			{
				m_pNextState = new VST_Work_Main(VarState_PowerCali_ID,
					QObject::trUtf8("Laser Cali Power Range"),
					m_pParent, this, 0);
				double dStep = pProcess_PowerCali->m_Variants.get_VData(Var_Process_LaserPowerCali_Step)->toDouble();
				if (DEQUAL0(dStep))
				{
					MQT_THROW_CODESTR(ALARM_POWERCALI_PARAMETER, QObject::trUtf8("Power Cali Step is Zero"));

				}
				int iTotalStep = 1000.0 / dStep;
				//int iTotalStep = (int)dStep;
				((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
					pushback_Msg(VarMsg_Process_ID,
						QObject::trUtf8("Laser Cali Power Range Before"),
						QVariantList() << iProcessID
						<< CProcess_LaserPowerCali::CALIPOWER_BEAM_BEFORE
						<< msg.m_vlInput[1] << msg.m_vlInput[2] << msg.m_vlInput[3] << msg.m_vlInput[4],
						msg.m_pvlOutput);
				for (int i = iTotalStep; i >= 0; i--)
				{
					((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
						pushback_Msg(VarMsg_Process_ID,
							QObject::trUtf8("Laser Cali Power Range"),
							QVariantList() << iProcessID
							<< CProcess_LaserPowerCali::CALIPOWER_BEAM
							<< msg.m_vlInput[1] << msg.m_vlInput[2] << msg.m_vlInput[3] << i << msg.m_vlInput[4],
							msg.m_pvlOutput);

				}
				((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
					pushback_Msg(VarMsg_Process_ID,
						QObject::trUtf8("Laser Cali Power Range After"),
						QVariantList() << iProcessID
						<< CProcess_LaserPowerCali::CALIPOWER_BEAM_AFTER
						<< msg.m_vlInput[1] << msg.m_vlInput[2] << msg.m_vlInput[3] << msg.m_vlInput[4],
						msg.m_pvlOutput);

				((VST_Work_Main*)m_pNextState)->LoadMsgProcess();
			}
			m_pParent->SetState(m_pNextState, false);
		}
		break;
        case VarMsg_PowerCali_PowerRange_ID:
        {
            //测量功率
            int iProcessID = msg.m_vlInput[0].toInt();
            CProcess_LaserPowerCali* pProcess_PowerCali = (CProcess_LaserPowerCali*)m_pParent->FindProcess(iProcessID);
            Q_ASSERT(pProcess_PowerCali);
            if(!m_pNextState) {
                m_pNextState = new VST_Work_Main(VarState_PowerCali_ID,
                                                 QObject::trUtf8("Laser Cali Power Range"),
                                                 m_pParent, this, 0);
                double dStep = pProcess_PowerCali->m_Variants.get_VData(Var_Process_LaserPowerCali_Step)->toDouble();
                if(DEQUAL0(dStep)) {
                    MQT_THROW_CODESTR(ALARM_POWERCALI_PARAMETER, QObject::trUtf8("Power Cali Step is Zero"));

                }

				((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
					pushback_Msg(VarMsg_Driver_ID,
						QObject::trUtf8("Check Power Meter"),
						QVariantList() << POWERMETERDRIVER
						<< CPowerMeterDriver_lp::kPowerMeterCheck);
                //TODO:功率测量，总共测量多少次,根据测量步长进行计算
#ifndef CALIBLASERPOWER_PICK_G2
                int iTotalStep = 100.0 / dStep;
                //int iTotalStep = (int)dStep;
                ((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
                    pushback_Msg(VarMsg_Process_ID,
                                 QObject::trUtf8("Laser Cali Power Range Before"),
                                 QVariantList() << iProcessID
                                 << CProcess_LaserPowerCali::CALIPOWER_BEFORE
                                 << msg.m_vlInput[1] << msg.m_vlInput[2] << msg.m_vlInput[3],
                                 msg.m_pvlOutput);
                for(int i = 0; i < iTotalStep; i += dStep) {
                    ((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
                        pushback_Msg(VarMsg_Process_ID,
                                     QObject::trUtf8("Laser Cali Power Range"),
                                     QVariantList() << iProcessID
                                     << CProcess_LaserPowerCali::CALIPOWER
                                     << msg.m_vlInput[1] << msg.m_vlInput[2] << msg.m_vlInput[3] << i,
                                     msg.m_pvlOutput);

                }
#else
				int iTotalStep = 45 / dStep;
				//int iTotalStep = (int)dStep;
				((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
					pushback_Msg(VarMsg_Process_ID,
						QObject::trUtf8("Laser Cali Power Range Before"),
						QVariantList() << iProcessID
						<< CProcess_LaserPowerCali::CALIPOWER_BEFORE
						<< msg.m_vlInput[1] << msg.m_vlInput[2] << msg.m_vlInput[3],
						msg.m_pvlOutput);
				for (int i = 0; i < iTotalStep; i++)
				{
					((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
						pushback_Msg(VarMsg_Process_ID,
							QObject::trUtf8("Laser Cali Power Range"),
							QVariantList() << iProcessID
							<< CProcess_LaserPowerCali::CALIPOWER
							<< msg.m_vlInput[1] << msg.m_vlInput[2] << msg.m_vlInput[3] << i,
							msg.m_pvlOutput);

				}
#endif

                ((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
                    pushback_Msg(VarMsg_Process_ID,
                                 QObject::trUtf8("Laser Cali Power Range After"),
                                 QVariantList() << iProcessID
                                 << CProcess_LaserPowerCali::CALIPOWER_AFTER
                                 << msg.m_vlInput[1] << msg.m_vlInput[2] << msg.m_vlInput[3],
                                 msg.m_pvlOutput);

                ((VST_Work_Main*)m_pNextState)->LoadMsgProcess();
            }
            m_pParent->SetState(m_pNextState, false);
            break;
        }
        case VarMsg_PowerCali_PowerPChange_ID:
        {
            //PCHANGE 校正
            int iProcessID = msg.m_vlInput[0].toInt();
            CProcess_LaserPowerCali* pProcess_PowerCali =
                (CProcess_LaserPowerCali*)m_pParent->FindProcess(iProcessID);
            Q_ASSERT(pProcess_PowerCali);
            if(!m_pNextState) {

                m_pNextState = new VST_Work_Main(VarState_PowerCali_ID,
                                                 QObject::trUtf8("Laser Cali Power Range"),
                                                 m_pParent, this, 0);
                double dStep = pProcess_PowerCali->m_Variants.
                    get_VData(Var_Process_LaserPowerCali_Step)
                    ->toDouble();
                if(DEQUAL0(dStep)) {
                    MQT_THROW_CODESTR(ALARM_POWERCALI_PARAMETER, QObject::trUtf8("Power Cali Step is Zero"));

                }
                //int iTotalStep=90/dStep;
                int iTotalStep = (int)dStep;
                ((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
                    pushback_Msg(VarMsg_Process_ID,
                                 QObject::trUtf8("Laser Cali Power Range Before"),
                                 QVariantList() << iProcessID
                                 << CProcess_LaserPowerCali::CALIPCHANGEANGLE_BEFORE
                                 << msg.m_vlInput[1] << msg.m_vlInput[2],
                                 msg.m_pvlOutput);
                for(int i = 0; i < iTotalStep; i++) {
                    ((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
                        pushback_Msg(VarMsg_Process_ID,
                                     QObject::trUtf8("Laser Cali Power Range"),
                                     QVariantList() << iProcessID
                                     << CProcess_LaserPowerCali::CALIPCHANGEANGLE
                                     << msg.m_vlInput[1] << msg.m_vlInput[2] << i, msg.m_pvlOutput);
                }
                ((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
                    pushback_Msg(VarMsg_Process_ID,
                                 QObject::trUtf8("Laser Cali Power Range After"),
                                 QVariantList() << iProcessID
                                 << CProcess_LaserPowerCali::CALIPCHANGEANGLE_AFTER
                                 << msg.m_vlInput[1] << msg.m_vlInput[2],
                                 msg.m_pvlOutput);
            }
            m_pParent->SetState(m_pNextState);

            break;
        }
        case VarMsg_PowerCali_PowerMax_ID:
        {
            //校正最大功率
            int iProcessID = msg.m_vlInput[0].toInt();
            CProcess_LaserPowerCali* pProcess_PowerCali =
                (CProcess_LaserPowerCali*)m_pParent->FindProcess(iProcessID);
            Q_ASSERT(pProcess_PowerCali);

            if(!m_pNextState) {
                m_pNextState = new VST_Work_Main(VarState_PowerCali_ID,
                                                 QObject::trUtf8("Laser Cali Power Range"),
                                                 m_pParent, this, 0);
                double dStep = pProcess_PowerCali->m_Variants.
                    get_VData(Var_Process_LaserPowerCali_Step)
                    ->toDouble();
                if(DEQUAL0(dStep)) {
                    MQT_THROW_CODESTR(ALARM_POWERCALI_PARAMETER, QObject::trUtf8("Power Cali Step is Zero"));

                }
                //TODO:测量次数，不用偏移90度
                int iTotalStep = 50 / dStep;
                //int iTotalStep = (int)dStep;
                ((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
                    pushback_Msg(VarMsg_Process_ID,
                                 QObject::trUtf8("Laser Cali Max Power Before"),
                                 QVariantList() << iProcessID
                                 << CProcess_LaserPowerCali::CALIMAXPOWERANGLE_BEFORE
                                 << msg.m_vlInput[1] << msg.m_vlInput[2],
                                 msg.m_pvlOutput);
                for(int i = 0; i < iTotalStep; i++) {
                    ((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
                        pushback_Msg(VarMsg_Process_ID,
                                     QObject::trUtf8("Laser Cali Max Power "),
                                     QVariantList() << iProcessID
                                     << CProcess_LaserPowerCali::CALIMAXPOWERANGLE
                                     << msg.m_vlInput[1] << msg.m_vlInput[2] << i,
                                     msg.m_pvlOutput);
                }
                ((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
                    pushback_Msg(VarMsg_Process_ID,
                                 QObject::trUtf8("Laser Cali Max Power After"),
                                 QVariantList() << iProcessID
                                 << CProcess_LaserPowerCali::CALIMAXPOWERANGLE_AFTER
                                 << msg.m_vlInput[1] << msg.m_vlInput[2],
                                 msg.m_pvlOutput);


            }
            m_pParent->SetState(m_pNextState);

            break;
        }
        case VarMsg_System_ChangeCh:
            m_gSystem.changeChannel(msg.m_vlInput[0].toInt(),
                                    msg.m_vlInput[1].toBool());
            break;

        case VarMsg_System_ChangeSide:
            m_gSystem.ChangeSide(msg.m_vlInput[0].toInt());
            break;
        case VarMsg_StartAuto_ID:
        {
            m_gSystem.SetIndicatorLight(Qt::red, false);
            m_gSystem.SetIndicatorLight(Qt::yellow, false);
            m_gSystem.SetIndicatorLight(Qt::green, true);
            //如果是auto就恢复启动
            if(m_pParent->m_pHistoryState_Auto) {
                m_gSystem.EvtLog(QString("START"), QString(QObject::trUtf8("Resume Auto Work") + QString(",CUT")));
            } else {
                m_gSystem.EvtLog(QString("FAS"), QObject::trUtf8("Full Auto Start"));
            }
            break;
        }
        case VarMsg_PowerCheck_ID:
        {
            if (!m_pNextState) 
            {
                m_pNextState = new VST_Work_Main(VarState_PowerCheck_ID,
                    QObject::trUtf8("State Power Check"),
                    m_pParent, this, 0);

                ((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
                    pushback_Msg(VarMsg_PowerCheck_ALL_ID,
                        QObject::trUtf8("Power Check All"), QVariantList() << true << 2 << SIDE_A
                         << SIDE_B); 

                ((VST_Work_Main*)m_pNextState)->LoadMsgProcess();
            }
            m_pParent->SetState(m_pNextState);
            break;
        }
        case VarMsg_PlatformCheck_ID:
        {
            //平台检测
            if(!m_pNextState) {
                m_pNextState = new VST_Work_Main(VarState_PlatformCheck_ID,
                                                 QObject::trUtf8("State Platform Check"),
                                                 m_pParent, this, 0);

                //移动到A面
                ((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
                    pushback_Msg(VarMsg_Process_ID,
                                 QObject::trUtf8("Before cali distance data"),
                                 QVariantList() << PROCESS_UTILS
                                 << CProcess_Utils::BEFORE_CALI_DISTANCEDATA << SIDE_A);

                for(int i = 0; i < 600; i++) {
                    ((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
                        pushback_Msg(VarMsg_Process_ID,
                                     QObject::trUtf8("Cali distance step"),
                                     QVariantList() << PROCESS_UTILS
                                     << CProcess_Utils::CALIDISTANCESTEP << i);
                }

                ((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
                    pushback_Msg(VarMsg_Process_ID,
                                 QObject::trUtf8("After cali distance data"),
                                 QVariantList() << PROCESS_UTILS
                                 << CProcess_Utils::AFTER_CALI_DISTANCEDATA << SIDE_A);

                ((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
                    pushback_Msg(VarMsg_Process_ID,
                                 QObject::trUtf8("Before cali distance data"),
                                 QVariantList() << PROCESS_UTILS
                                 << CProcess_Utils::BEFORE_CALI_DISTANCEDATA << SIDE_B);

                for(int i = 0; i < 600; i++) {
                    ((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
                        pushback_Msg(VarMsg_Process_ID,
                                     QObject::trUtf8("Cali distance step"),
                                     QVariantList() << PROCESS_UTILS
                                     << CProcess_Utils::CALIDISTANCESTEP << i);
                }

				((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
					pushback_Msg(VarMsg_Process_ID,
						QObject::trUtf8("After cali distance data"),
						QVariantList() << PROCESS_UTILS
						<< CProcess_Utils::AFTER_CALI_DISTANCEDATA << SIDE_B);

                ((VST_Work_Main*)m_pNextState)->m_VarMsgProcess.
                    pushback_Msg(VarMsg_Process_ID,
                                 QObject::trUtf8("Message After All Cali"),
                                 QVariantList() << PROCESS_UTILS
                                 << CProcess_Utils::AFTER_ALL_CALI);

                //移动到B面
                ((VST_Work_Main*)m_pNextState)->LoadMsgProcess();
            }
            
            m_pParent->SetState(m_pNextState);

            break;
        }
        case VarMsg_StartSemi_ID:
        {
            break;
        }
    }
    VST_Idle_Base::do_Msg(msg);
}

void VST_Idle_Main::Enter()
{
    try {
        m_gSystem.SetIndicatorLight(Qt::red, false);
        m_gSystem.SetIndicatorLight(Qt::yellow, true);
        m_gSystem.SetIndicatorLight(Qt::green, false);
    } catch(QString& s) {
        MqDebug() << s;
    }
}


