#include "vst_wait_transfer_ex.h"
#include "vst_work_transfer.h"
#include "cprocess_trans.h"
#include "vsm_thread.h"
#include "logic.h"
#include "msg_def.h"
#include "vsmt_base.h"
VST_Wait_TransferEx::VST_Wait_TransferEx(int iID, const QString &sName,
                                     VSM_Thread *pParent)
    :VST_Wait_Base(  iID,  sName, pParent)
{

}

void VST_Wait_TransferEx::do_Msg(const V_Msg &msg)
{
    switch(msg.m_iID)
    {
        case VarMsg_AddMsg:
        {
            int iMsgid = msg.m_vlInput.at(0).toInt();
            QString msgName = msg.m_vlInput[1].toString();
            AddMsgToState(iMsgid, msgName);
            break;
        }

    case VarMsg_Auto_StartCoat://开始涂敷
    {
        //如果没有处于自动状态会异常
        CProcess_Trans* pTrans=GET_PROCESS(PROCESS_TRANS,
                                           SM_TRANS,CProcess_Trans);
        Q_ASSERT(pTrans);
        if(pTrans->m_pSlotCoat)
        {
			
              VSMT_Base* pSMCoat=GET_STATEMACHINE(SM_COAT,VSMT_Base);

              pSMCoat->pushBack_Msg_toState(nullptr,msg);
        }
        break;
    }
    case VarMsg_Auto_StartClean://开始清洗
    {
        CProcess_Trans* pTrans=GET_PROCESS(PROCESS_TRANS,
                                           SM_TRANS,CProcess_Trans);
        Q_ASSERT(pTrans);
        if(pTrans->m_pSlotClean)
        {
			//如果清洗台有晶圆了，则开始清洗
              VSMT_Base* pSMClean=GET_STATEMACHINE(SM_CLEAN,VSMT_Base);

              pSMClean->pushBack_Msg_toState(0,msg);
        }
        break;
    }
    case VarMsg_Auto_StartCut://开始切割
    {
        CProcess_Trans* pTrans=GET_PROCESS(PROCESS_TRANS,
                                           SM_TRANS,CProcess_Trans);
        Q_ASSERT(pTrans);
        if(pTrans->m_pSlotWT)
        {
              VSMT_Base* pSMMain=GET_STATEMACHINE(SM_MAIN,VSMT_Base);

              pSMMain->pushBack_Msg_toState(0,msg);
        }
        break;
    }
    }
    VST_Wait_Base::do_Msg( msg);
}

//将消息委托给VST_Work_Transfer执行，内部调用CProcess_Trans的消息循环
void VST_Wait_TransferEx::AddMsgToState(int iTransMsgID, const QString &msgName,
                                      bool bfront/*=false*/)
{

    if(!m_pNextState)
    {
        CProcess_Trans* processTrans = GET_PROCESS(PROCESS_TRANS, SM_TRANS, CProcess_Trans);
        QString sMsgName = processTrans->GetMsgName(iTransMsgID);
        m_pNextState= new VST_Work_Transfer(VarState_Work_Transfer,
                                            sMsgName,
                                            m_pParent,this,0);
        if(bfront)
        {
            ((VST_Work_Transfer*)m_pNextState)->m_VarMsgProcess.
                    pushfront_Msg(VarMsg_Transfer_Step,msgName,
                                  QVariantList()<< iTransMsgID << true);
        }
        else
        {
            ((VST_Work_Transfer*)m_pNextState)->m_VarMsgProcess.
                    pushback_Msg(VarMsg_Transfer_Step,msgName,
                                 QVariantList()<< iTransMsgID << true);
        }
        ((VST_Work_Transfer*)m_pNextState)->LoadMsgProcess();
    }
    m_pParent->SetState(m_pNextState);

}
