#include "vst_semi_clean.h"
#include "logic.h"
#include "vst_work_clean.h"
VST_Semi_Clean::VST_Semi_Clean(int iID, const QString &sName,
                               VSM_Thread *pParent)
    :VST_Semi(iID,sName,pParent)
{
	m_iCurOperator = -1;
}

void VST_Semi_Clean::do_Msg(const V_Msg &msg)
{
    if(msg.m_iID==VarMsg_StartAuto_ID)
    {
        int i= 0;
    }
    if(msg.m_iID==VarMsg_StartSemi_ID)
    {
        CProcess_Clean* pClean=GET_PROCESS(PROCESS_CLEAN,
                                         SM_CLEAN,
                                         CProcess_Clean);
        int iActionType=msg.m_vlInput[0].toInt();
		m_iCurOperator = iActionType;
        Q_ASSERT(iActionType==CLEANTABLE_ACTION||
                 iActionType==NORMAL_ACTION);
        QString strState=QObject::trUtf8("Clean Test");
        QString strMsg=QObject::trUtf8("Clean Test Sig");
        COATCLEANSTEPVECTOR step= pClean->m_vCoatCleanStep;
        if(iActionType==CLEANTABLE_ACTION)
        {
            strState=QObject::trUtf8("Clean CleanTable");
            strMsg=QObject::trUtf8("Clean CleanTable Sig");
            step= pClean->m_vCleanTableStep;
        }
        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 clean start step out of range");
        QString strErrEnd=QObject::trUtf8("Test clean end step out of range");
        if(iActionType==CLEANTABLE_ACTION)
        {
            strErrStart=QObject::trUtf8("Clean cleantable start step out of range");
            strErrEnd=QObject::trUtf8("Clean cleantable end step out of range");
        }
        if(OUTOFRANGE(iStartStep,0,iSize))
        {
            MQT_THROW_CODESTR(ALARM_CLEAN_LOGIC_PARM, strErrStart);
        }
        if(OUTOFRANGE(iEndStep,0,iSize))
        {
            MQT_THROW_CODESTR(ALARM_CLEAN_LOGIC_PARM, strErrEnd);
        }

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

            ((VST_Work_Clean*)m_pNextState)->m_VarMsgProcess.
                    pushback_Msg(VarMsg_CoatClean_BeforeAll, strMsg,
                                 QVariantList()<<iActionType);

            for(int i=iStartStep;i<=iEndStep;i++)
            {
                ((VST_Work_Clean*)m_pNextState)->m_VarMsgProcess.
                        pushback_Msg(VarMsg_CoatClean_Step, strMsg,
                                     QVariantList()<<iActionType<<step[i].iType
                                     <<step[i].dTime<<step[i].iRPM<<i);

            }
            ((VST_Work_Clean*)m_pNextState)->m_VarMsgProcess.
                    pushback_Msg(VarMsg_CoatClean_AfterAll, strMsg,
                                 QVariantList()<<iActionType);
            ((VST_Work_Clean*)m_pNextState)->LoadMsgProcess();
        }
        m_pParent->SetState(m_pNextState);
        QString strFinish=QObject::trUtf8("Clean Test Finish");
        if(iActionType==CLEANTABLE_ACTION)
        {
            strFinish=QObject::trUtf8("Clean CleanTable Finish");
        }else{
			m_gSystem.m_totalCleanTime.ClearTime();
			m_gSystem.m_totalCleanTime.StartTime();
		}
        pushback_Msg(VarMsg_CoatClean_Finished,strFinish);
    }
    if(msg.m_iID==VarMsg_CoatClean_Finished)
    {
		m_gSystem.m_totalCleanTime.StopTime();
        QString s=msg.m_sName;
        m_gSystem.m_SysMsg.SetMsg(s);
        m_pParent->pushfront_Msg(VarMsg_StopSemi_ID,
                                 QObject::trUtf8("Stop CleanTable Process Sig"));
		m_iCurOperator = -1;
    }
    VST_Semi::do_Msg(msg);
}

