#include "vsmt_clean.h"
#include "vst_idle_clean.h"
#include "vst_wait_clean.h"
#include "vst_semi_clean.h"
#include "logic.h"
#include "cprocess_clean.h"
#include "cprocess_trans.h"
VSMT_Clean::VSMT_Clean(int iID, const QString &sName)
    :VSMT_Base(iID, sName)
{
    mprocesss[PROCESS_CLEAN] =
        new CProcess_Clean(m_gSystem.m_mapNameList[PROCESS_CLEAN],
                           PROCESS_CLEAN);

    LOG_INFO("vsMT_base");
}

void VSMT_Clean::SM_Home(int iStep, bool bSingle)
{
    CMotorDriver* pMotorDriver = GET_CARD(MOTOR_DRIVER,
                                          CMotorDriver);
    Q_ASSERT(pMotorDriver);
    switch (iStep) {
        case 1:
        {
            CProcess_Clean* pClean = GET_PROCESS(PROCESS_CLEAN, SM_CLEAN, CProcess_Clean);
            Q_ASSERT(pClean);
            pClean->nozzeloperator_clean(INIT_CLEAN);

            pMotorDriver->setio(IO_OUTPUT, OUT_CLEAN_VACUUM_BREAK, false);
            break;
        }
        case 2:
            pMotorDriver->home(MOTORF1_CLEAN_ARM, false);
        case 3:
            pMotorDriver->wait_home_finished(MOTORF1_CLEAN_ARM);
            break;
        case 4:
            break;
        case 5:
            break;
        case 6:
            pMotorDriver->home(MOTORF_CLEAN_SPINDLE, false);
            break;
        case 9:
            pMotorDriver->wait_home_finished(MOTORF_CLEAN_SPINDLE);
            break;
        case 10:
            if (bSingle) {
                m_gSystem.m_SysMsg.SetMsg(QObject::trUtf8("%1 home finished")
                                          .arg(m_sName));
            }
            else
            {
                CProcess_Clean* pClean = (CProcess_Clean*)FindProcess(PROCESS_CLEAN);
                Q_ASSERT(pClean);
                pClean->CloseProtect(true);
            }
            break;
    }
}

void VSMT_Clean::init_Machine()
{
    m_pState_Idle = new VST_Idle_Clean(VarState_Idle_Clean, QObject::trUtf8("Clean Idle"), this);
    m_pState_Wait = new VST_Wait_Clean(VarState_Wait_Clean, QObject::trUtf8("Clean Wait"), this);
    m_pState_Semi = new VST_Semi_Clean(VarState_Semi_Clean, QObject::trUtf8("Clean Test"), this);

    VSMT_Base::init_Machine();
}

void VSMT_Clean::do_Msg(const V_Msg &msg)
{

    if (msg.m_iID == VarMsg_Pause_ID) {
        VS_Thread* pState = (VS_Thread*)m_pCurState;
        switch (pState->m_iStateType) {
            case VS_Thread::State_Work_Semi:
            case VS_Thread::State_Work_Auto:
                PausePos();
                ReturnToParentState(VarState_Work_CoatClean);
                if (m_pCurState->m_iID == VarState_Work_CoatClean) {
                    m_pCurState->clearAllNextState();
                }
                break;
        }
    }


    switch (msg.m_iID) {
        case VarMsg_StopSemi_ID:
        {
            ((VST_Semi_Clean*)m_pState_Semi)->m_iCurOperator = -1;
            break;
        }
        case VarMsg_Stop_ID:
        {
            ((VST_Semi_Clean*)m_pState_Semi)->m_iCurOperator = -1;
            break;
        }
        case VarMsg_AbortAutoClean:
        {
            CProcess_Trans* pTrans = GET_PROCESS(PROCESS_TRANS, SM_TRANS, CProcess_Trans);
            //HACK:传片到m_pSlotClean时，还没传片结束，这个值也会为真
            if ((m_pHistoryState_Auto&&m_pHistoryState_Auto->m_iStateType == VS_Thread::State_Work_Auto) 
                || m_pCurState->m_iStateType == VS_Thread::State_Work_Auto
                || (m_pCurState->m_iStateType == VS_Thread::State_Wait && pTrans->m_pSlotClean)) {

                //停止所有
                LOG_WARN("Abort auto cleaning");
                //停止所有
                V_Msg msg(VarMsg_Stop_ID, "Abort auto clean stop");
                VSMT_Base::do_Msg(msg);

                //启用自动
                V_Msg msg2(VarMsg_StartAuto_ID, "Abort auto clean start working");
                m_pState_Idle->do_Msg(msg2);

                pushfront_Msg(VarMsg_Pause_ID, QObject::trUtf8("Pause Sig"));

                m_gSystem.m_SysMsg.SetMsg(QObject::tr("Please reset clean table wafer status."));

                return;

            } else {
                m_gSystem.m_SysMsg.SetMsg(QObject::tr("Can't abort cleaning,not start clean"));
                return;
            }
        }
        break;
        default:
            break;
    }
    VSMT_Base::do_Msg(msg);
}

void VSMT_Clean::PausePos()
{
    try {
        CProcess_Clean* pClean = GET_PROCESS(PROCESS_CLEAN,
                                             SM_CLEAN,
                                             CProcess_Clean);
        Q_ASSERT(pClean);
        pClean->m_COAT_CLEAN_TIME.StopTime();
        pClean->SpindleStop();
        pClean->SetdefaultSpeed();
        pClean->SpindleUp(false);
        pClean->nozzeloperator_clean(INIT_CLEAN);
        pClean->MoveCleanWaitPos();
    } catch (QString& s) {
        MqDebug() << s;
    }
}

