#include "vst_work_clean.h"
#include "logic.h"
#include "vsmt_clean.h"
VST_Work_Clean::VST_Work_Clean(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_Clean::do_Msg(const V_Msg &msg)
{
    switch(msg.m_iID)
    {
    case VarMsg_CoatClean_BeforeAll:
    {
        if(!m_pNextState)
        {
            m_pNextState= new VST_Work_Clean(VarState_Clean_Step,
                                             QObject::trUtf8("Clean Prepare"),
                                             m_pParent, this,0);
            int iActionType= msg.m_vlInput[0].toInt();
            //实际只使用了i=0，i>0的消息都直接忽略了
            for(int i=0;i<200;i++)
            {
                ((VST_Work_Clean*)m_pNextState)->m_VarMsgProcess.
                        pushback_Msg(VarMsg_Process_ID,
                                     QObject::trUtf8("Prepare Clean Sig"),
                                     QVariantList()<<PROCESS_CLEAN
                                     <<CProcess_Clean::ALL_ACTION_BEFORE
                                     <<iActionType<<i);
            }
            ((VST_Work_Clean*)m_pNextState)->LoadMsgProcess();
        }
        m_pParent->SetState(m_pNextState);

        break;
    }
    case VarMsg_CoatClean_Step: //单步执行清洗
        if(!m_pNextState)
        {
            m_pNextState= new VST_Work_Clean(VarState_Clean_Step,
                                             QObject::trUtf8("Clean Step"),
                                             m_pParent,
                                             this,0);
            CProcess_Clean* pClean=GET_PROCESS(PROCESS_CLEAN,
                                               SM_CLEAN,CProcess_Clean);
            Q_ASSERT(pClean);
            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();
            pClean->m_iActionType=iActionType;
            pClean->m_iCurStep=msg.m_vlInput[4].toInt();

            QString name=pClean->GetActionName(iType);
            //单步清洗
            for(int i=0;i<200;i++)
            {
                ((VST_Work_Clean*)m_pNextState)->m_VarMsgProcess.
                        pushback_Msg(VarMsg_Process_ID,
                                     QObject::trUtf8("Before %1 Sig").arg(name),
                                     QVariantList()<<PROCESS_CLEAN
                                     <<CProcess_Clean::SINGLE_ACTION_BEFORE
                                     <<iActionType<<iType<<dTime<<iRPM<<i);
            }
            ((VST_Work_Clean*)m_pNextState)->m_VarMsgProcess.
                    pushback_Msg(VarMsg_CoatClean_Wait,
                                 QObject::trUtf8("%1 Sig").arg(name),
                                 QVariantList()
                                 <<iActionType<<iType<<dTime<<iRPM<<0);
            //单步清洗结束步骤
            for(int i=0;i<200;i++)
            {
                ((VST_Work_Clean*)m_pNextState)->m_VarMsgProcess.
                        pushback_Msg(VarMsg_Process_ID,
                                     QObject::trUtf8("After %1 Sig").arg(name),
                                     QVariantList()<<PROCESS_CLEAN
                                     <<CProcess_Clean::SINGLE_ACTION_AFTER
                                     <<iActionType<<iType<<dTime<<iRPM<<i);
            }
            ((VST_Work_Clean*)m_pNextState)->LoadMsgProcess();
        }
        m_pParent->SetState(m_pNextState);

        break;
    case VarMsg_CoatClean_AfterAll:
    {
        if(!m_pNextState)
        {
            m_pNextState= new VST_Work_Clean(VarState_Clean_Step,
                                             QObject::trUtf8("Clean After All"),
                                             m_pParent, this,0);
            int iActionType= msg.m_vlInput[0].toInt();
            for(int i=0;i<200;i++)
            {
                ((VST_Work_Clean*)m_pNextState)->m_VarMsgProcess.
                        pushback_Msg(VarMsg_Process_ID,
                                     QObject::trUtf8("After All Clean Sig"),
                                     QVariantList()<<PROCESS_CLEAN
                                     <<CProcess_Clean::ALL_ACTION_AFTER
                                     <<iActionType<<i);
            }
            ((VST_Work_Clean*)m_pNextState)->LoadMsgProcess();
        }
        m_pParent->SetState(m_pNextState);

        break;
    }
    case VarMsg_CoatClean_Wait:
    {
        CProcess_Clean* pClean=GET_PROCESS(PROCESS_CLEAN,
                                           SM_CLEAN,CProcess_Clean);
        Q_ASSERT(pClean);
        const QVariantList& vlInput=msg.m_vlInput;
        pClean->single_action_wait(vlInput[0].toInt(),vlInput[1].toInt(),
                vlInput[2].toDouble(), vlInput[3].toInt(),vlInput[4].toInt());
        if(!pClean->m_COAT_CLEAN_TIME.m_bstart)
        {
            MQT_THROW_CODESTR(ALARM_COAT_LOGIC_PARM, QObject::trUtf8("Coat Single process not start"));
        }
        //等待结束
        if(pClean->m_COAT_CLEAN_TIME.GetTime()>vlInput[2].toInt() )
        {
            return ;
        }
        else
        {
            MSleep(10);
            pushfront_Msg(msg);
        }
        break;
    }
    }
    VST_Work_Base::do_Msg(msg);
}

VST_Work_Clean *VST_Work_Clean::GetStepState()
{

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

