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

void VST_Semi_Coat::do_Msg(const V_Msg &msg)
{
    if(msg.m_iID==VarMsg_StartSemi_ID)
    {
        CProcess_Coat* pCoat=GET_PROCESS(PROCESS_COAT,
                                         SM_COAT,
                                         CProcess_Coat);
        int iActionType=msg.m_vlInput[0].toInt();

		m_iCurOperator = iActionType;

        Q_ASSERT(iActionType==CLEANTABLE_ACTION||
                 iActionType==NORMAL_ACTION 
				 || iActionType==CLEANCOATFLUID_ACTION);
        QString strState=QObject::trUtf8("Coat Test");
        QString strMsg=QObject::trUtf8("Coat Test Sig");
        COATCLEANSTEPVECTOR step= pCoat->m_vCoatCleanStep;
        if(iActionType==CLEANTABLE_ACTION)
        {
            strState=QObject::trUtf8("Clean CoatTable");
            strMsg=QObject::trUtf8("Clean CoatTable Sig");
            step= pCoat->m_vCleanTableStep;
        }else if(iActionType == CLEANCOATFLUID_ACTION)
		{
			step = pCoat->m_vCleanTableRing;
		}
        int iSize=step.size();
        int iStartStep=msg.m_vlInput[1].toInt();
        int iEndStep=msg.m_vlInput[2].toInt();
        if(iStartStep<0) {iStartStep=0;}
        if(iEndStep<0) {iEndStep=iSize-1;}
        QString strErrStart=QObject::trUtf8("Test coat start step out of range");
        QString strErrEnd=QObject::trUtf8("Test coat end step out of range");
        if(iActionType==CLEANTABLE_ACTION)
        {
            strErrStart=QObject::trUtf8("Clean coattable start step out of range");
            strErrEnd=QObject::trUtf8("Clean coattable end step out of range");
        }else if(iActionType == CLEANCOATFLUID_ACTION)
		{
			strErrStart=QObject::trUtf8("Clean coattable coat fulid start step out of range");
			strErrEnd=QObject::trUtf8("Clean coattable coat fulid end step out of range");
		}
        if(OUTOFRANGE(iStartStep,0,iSize))
        {
            MQT_THROW_CODESTR(ALARM_COAT_LOGIC_PARM,strErrStart);
        }
        if(OUTOFRANGE(iEndStep,0,iSize))
        {
            MQT_THROW_CODESTR(ALARM_COAT_LOGIC_PARM, strErrEnd);
        }

        if(!m_pNextState)
        {
            m_pNextState=new VST_Work_Coat(VarState_Work_CoatClean, strState,
                                           m_pParent,this,0);

            ((VST_Work_Coat*)m_pNextState)->m_VarMsgProcess.
                    pushback_Msg(VarMsg_CoatClean_BeforeAll, strMsg,
                                 QVariantList()<<iActionType);
            for(int i=iStartStep;i<=iEndStep;i++)
            {
                ((VST_Work_Coat*)m_pNextState)->m_VarMsgProcess.
                        pushback_Msg(VarMsg_CoatClean_Step, strMsg,
                                     QVariantList()<<iActionType<<step[i].iType
                                     <<step[i].dTime<<step[i].iRPM<<i<<step[i].dSpayNum
									 <<step[i].dSpaySpd<<step[i].dCenterSpeed<<step[i].dEageSpeed<<step[i].dCenterWaitTime
									 <<step[i].dCoatPreSpyNum<<step[i].iCoatPreSpyWaitTime
									 <<step[i].dBumpNum<<step[i].dPreSpayNum);

            }
            ((VST_Work_Coat*)m_pNextState)->m_VarMsgProcess.
                    pushback_Msg(VarMsg_CoatClean_AfterAll, strMsg,
                                 QVariantList()<<iActionType);

            ((VST_Work_Coat*)m_pNextState)->LoadMsgProcess();
        }
        m_pParent->SetState(m_pNextState);
        QString strFinish=QObject::trUtf8("Coat Test Finish");
        if(iActionType==CLEANTABLE_ACTION)
        {
            strFinish=QObject::trUtf8("Clean CoatTable Finish");
        }else{
			m_gSystem.m_totalCoatTime.ClearTime();
			m_gSystem.m_totalCoatTime.StartTime();
		}

        pushback_Msg(VarMsg_CoatClean_Finished,strFinish);
    }
    if(msg.m_iID==VarMsg_CoatClean_Finished)
    {
		m_gSystem.m_totalCoatTime.StopTime();
        QString s=msg.m_sName;
        m_gSystem.m_SysMsg.SetMsg(s);
        m_pParent->pushfront_Msg(VarMsg_StopSemi_ID,
                                 QObject::trUtf8("Stop CoatTable Process Sig"));
		m_iCurOperator = -1;
    }
    VST_Semi::do_Msg(msg);
}

