#include "vst_work_coat.h"
#include "logic.h"
#include "vsm_thread.h"
VST_Work_Coat::VST_Work_Coat(int iID, const QString &sName,
                             VSM_Thread *pParent,
                             VS_Thread *pPreState,
                             VS_Thread *pNextState)
    :VST_Work_Base(iID,sName,pParent,pPreState,pNextState)
{

}

//仅有涂敷的过程 
void VST_Work_Coat::do_Msg(const V_Msg &msg)
{
    switch(msg.m_iID)
    {
    case VarMsg_CoatClean_BeforeAll:
    {
        if(!m_pNextState)
        {
            m_pNextState= new VST_Work_Coat(VarState_Coat_Step,
                                            QObject::trUtf8("Coat Prepare"),
                                            m_pParent,
                                            this,0);
            int iActionType= msg.m_vlInput[0].toInt();
			//实际只使用了i=0，i>0的消息都直接忽略了 PROCESS_COAT
            for(int i=0;i<200;i++)
            {
                ((VST_Work_Coat*)m_pNextState)->m_VarMsgProcess.
                        pushback_Msg(VarMsg_Process_ID,
                                     QObject::trUtf8("Prepare Coat Sig"),
                                     QVariantList()<<PROCESS_COAT
                                     <<CProcess_Coat::ALL_ACTION_BEFORE
                                     <<iActionType<<i);
            }
            ((VST_Work_Coat*)m_pNextState)->LoadMsgProcess();
        }
        m_pParent->SetState(m_pNextState);

        break;
    }
    case VarMsg_CoatClean_Step:
        if(!m_pNextState)
        {
            m_pNextState= new VST_Work_Coat(VarState_Coat_Step,
                                            QObject::trUtf8("Coat Step"),
                                            m_pParent,
                                            this,0);
            CProcess_Coat* pCoat=GET_PROCESS(PROCESS_COAT,
                                             SM_COAT,CProcess_Coat);
            Q_ASSERT(pCoat);
            Q_ASSERT(msg.m_vlInput.size()>=5);
            int iActionType= msg.m_vlInput[0].toInt();
            int iType= msg.m_vlInput[1].toInt();
            double dTime= msg.m_vlInput[2].toDouble();
            int iRPM= msg.m_vlInput[3].toInt();
			//Coat控制
			double dSpayNum= msg.m_vlInput[5].toDouble();
			double dSpaySpd= msg.m_vlInput[6].toDouble();
			double dCenterSpeed= msg.m_vlInput[7].toDouble();
			double dEageSpeed= msg.m_vlInput[8].toDouble();
			double dCenterWaitTime= msg.m_vlInput[9].toDouble();
			double dCoatPreSpay = msg.m_vlInput[10].toDouble();
			double dCoatPreSpayWaitTime = msg.m_vlInput[11].toDouble();
			double dBumpNum = msg.m_vlInput[12].toDouble();
			double dPreSpay = msg.m_vlInput[13].toDouble();

            pCoat->m_iActionType=iActionType;
            pCoat->m_iCurStep=msg.m_vlInput[4].toInt();

			//单步before
            QString name=pCoat->GetActionName(iType);
            for(int i=0;i<200;i++)
            {
                ((VST_Work_Coat*)m_pNextState)->m_VarMsgProcess.
                        pushback_Msg(VarMsg_Process_ID,
                                     QObject::trUtf8("Before %1 Sig").arg(name),
                                     QVariantList()<<PROCESS_COAT
                                     <<CProcess_Coat::SINGLE_ACTION_BEFORE
                                     <<iActionType<<iType<<dTime<<iRPM<<i<<dSpayNum
									 <<dSpaySpd<<dCenterSpeed<<dEageSpeed<<dCenterWaitTime
									 <<dCoatPreSpay<<dCoatPreSpayWaitTime
									 <<dBumpNum<<dPreSpay);
            }
			//单步运行
            ((VST_Work_Coat*)m_pNextState)->m_VarMsgProcess.
                    pushback_Msg(VarMsg_CoatClean_Wait,
                                 QObject::trUtf8("%1 Sig").arg(name),
                                 QVariantList()
                                 <<iActionType<<iType<<dTime<<iRPM<<0);
			//单步after
            for(int i=0;i<200;i++)
            {
                ((VST_Work_Coat*)m_pNextState)->m_VarMsgProcess.
                        pushback_Msg(VarMsg_Process_ID,
                                     QObject::trUtf8("After %1 Sig").arg(name),
                                     QVariantList()<<PROCESS_COAT
                                     <<CProcess_Coat::SINGLE_ACTION_AFTER
                                     <<iActionType<<iType<<dTime<<iRPM<<i);
            }
            ((VST_Work_Coat*)m_pNextState)->LoadMsgProcess();
        }
        m_pParent->SetState(m_pNextState);

        break;
    case VarMsg_CoatClean_Wait:
    {
		//运行涂敷的单步操作
        CProcess_Coat* pCoat=GET_PROCESS(PROCESS_COAT,
                                         SM_COAT,CProcess_Coat);
        Q_ASSERT(pCoat);
        const QVariantList& vlInput=msg.m_vlInput;
        pCoat->single_action_wait(vlInput[0].toInt(),vlInput[1].toInt(),
                vlInput[2].toDouble(), vlInput[3].toInt(),vlInput[4].toInt());
        if(!pCoat->m_COAT_CLEAN_TIME.m_bstart)
        {
            MQT_THROW_CODESTR(ALARM_COAT_LOGIC_PARM, QObject::trUtf8("Coat Single process not start"));
        }
        if(pCoat->m_COAT_CLEAN_TIME.GetTime()>vlInput[2].toInt() )
        {
            return ;
        }
        else
        {
            MSleep(10);
            pushfront_Msg(msg);
        }
        break;
    }
    case VarMsg_CoatClean_AfterAll:
    {
		//涂胶结束
        if(!m_pNextState)
        {
            m_pNextState= new VST_Work_Coat(VarState_Coat_Step,
                                            QObject::trUtf8("Coat After"),
                                            m_pParent,
                                            this,0);
            int iActionType= msg.m_vlInput[0].toInt();
            for(int i=0;i<200;i++)
            {
                ((VST_Work_Coat*)m_pNextState)->m_VarMsgProcess.
                        pushback_Msg(VarMsg_Process_ID,
                                     QObject::trUtf8("After Coat Sig"),
                                     QVariantList()<<PROCESS_COAT
                                     <<CProcess_Coat::ALL_ACTION_AFTER
                                     <<iActionType<<i);
            }
            ((VST_Work_Coat*)m_pNextState)->LoadMsgProcess();
        }
        m_pParent->SetState(m_pNextState);

        break;
    }

    }
    VST_Work_Base::do_Msg(msg);
}

VST_Work_Coat *VST_Work_Coat::GetStepState()
{
    VS_Thread * pWorkState=this;
    while(pWorkState&&pWorkState->m_iID!=VarState_Work_CoatClean)
    {
            pWorkState=pWorkState->m_pPreState;
    }
    Q_ASSERT(pWorkState);
    return (VST_Work_Coat*)pWorkState;
}

