#include "vsmt_coat.h"
#include "logic.h"
#include "vst_idle_coat.h"
#include "vst_work_coat.h"
#include "vst_wait_coat.h"
#include "vst_semi_coat.h"
#include "cprocess_trans.h"
#include "cprocess_coat.h"
VSMT_Coat::VSMT_Coat(int iID, const QString &sName)
    :VSMT_Base(iID, sName)
{
    mprocesss[PROCESS_COAT] =
        new CProcess_Coat(m_gSystem.m_mapNameList[PROCESS_COAT],
                          PROCESS_COAT);

}

void VSMT_Coat::SM_Home(int iStep, bool bSingle)
{
    CMotorDriver* pMotorDriver = GET_CARD(MOTOR_DRIVER,
                                          CMotorDriver);
    Q_ASSERT(pMotorDriver);
    switch (iStep) {
        case 1:
        {
            CProcess_Coat* pCoat = GET_PROCESS(PROCESS_COAT, SM_COAT, CProcess_Coat);
            Q_ASSERT(pCoat);
            pCoat->nozzeloperator_coat(INIT_COAT);
            pMotorDriver->setio(IO_OUTPUT, OUT_COAT_VACUUM_BREAK, false);

            break;
        }
        case 2:
            pMotorDriver->home(MOTORE1_COAT_CLEAN_ARM, false);
        case 3:
            pMotorDriver->wait_home_finished(MOTORE1_COAT_CLEAN_ARM);
            break;
        case 4:
            pMotorDriver->home(MOTORE2_COAT_COAT_ARM, false);
            break;
        case 5:
            pMotorDriver->wait_home_finished(MOTORE2_COAT_COAT_ARM);
            break;
        case 6:
            pMotorDriver->home(MOTORE3_COAT_BUMP, true);
            break;
        case 7:
            pMotorDriver->wait_home_finished(MOTORE3_COAT_BUMP);
            break;
        case 8:
            pMotorDriver->home(MOTORE_COAT_SPINDLE, false);
            break;
        case 9:
            pMotorDriver->wait_home_finished(MOTORE_COAT_SPINDLE);
            break;
        case 10:
            if (bSingle) {
                m_gSystem.m_SysMsg.SetMsg(QObject::trUtf8("%1 home finished")
                                          .arg(m_sName));
            }
            else
            {
                //回原完了关闭保护罩
                CProcess_Coat* pCoat = (CProcess_Coat*)FindProcess(PROCESS_COAT);
                Q_ASSERT(pCoat);
                pCoat->CloseProtect(true);
            }
            break;
    }
}

void VSMT_Coat::init_Machine()
{
    m_pState_Idle = new VST_Idle_Coat(VarState_Idle_Coat, QObject::trUtf8("Coat Idle"), this);
    m_pState_Wait = new VST_Wait_Coat(VarState_Wait_Coat, QObject::trUtf8("Coat Wait"), this);
    m_pState_Semi = new VST_Semi_Coat(VarState_Semi_Coat, QObject::trUtf8("Coat Test"), this);

    VSMT_Base::init_Machine();
}

void VSMT_Coat::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_RestartAutoCoat:
        {
            CProcess_Trans* pTrans = GET_PROCESS(PROCESS_TRANS, SM_TRANS, CProcess_Trans);
            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_pSlotCoat && pTrans->m_pSlotCoat->WPstatus == WPCOATFINISH)) {

                LOG_WARN("Restart auto coating");
                //停止所有
                V_Msg msg(VarMsg_Stop_ID, "Restart auto coat stop");
                VSMT_Base::do_Msg(msg);

                //启用自动
                /*m_pState_Idle->pushfront_Msg(VarMsg_StartAuto_ID, "Start Auto", QVariantList());*/
                V_Msg msg2(VarMsg_StartAuto_ID, "Restart auto coat start working");
                m_pState_Idle->do_Msg(msg2);

                //启用切割
                m_pState_Wait->pushback_Msg(VarMsg_Auto_StartCoat,
                                            QObject::trUtf8("Restart coating"), QVariantList());

                return;
            } else {
                m_gSystem.m_SysMsg.SetMsg(QObject::tr("Can't restart coating,not start coat"));
                return;
            }
        }
        break;
        case VarMsg_AbortAutoCoat:
            //TODO:如果还没传片结束的时候，pTrans->m_pSlotCoat已经有值了
        {
            CProcess_Trans* pTrans = GET_PROCESS(PROCESS_TRANS, SM_TRANS, CProcess_Trans);
            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_pSlotCoat)) {

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

                //启用自动
                /*m_pState_Idle->pushfront_Msg(VarMsg_StartAuto_ID, "Start Auto", QVariantList());*/
                V_Msg msg2(VarMsg_StartAuto_ID, "Abort auto coat 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 coat table wafer status."));

                return;

            } else {
                m_gSystem.m_SysMsg.SetMsg(QObject::tr("Can't abort coating,not start coat"));
                return;
            }
        }
        break;
        case VarMsg_StopSemi_ID:
        {
            ((VST_Semi_Coat*)m_pState_Semi)->m_iCurOperator = -1;
            break;
        }
        case VarMsg_Stop_ID:
        {
            ((VST_Semi_Coat*)m_pState_Semi)->m_iCurOperator = -1;
            break;
        }
        default:
            break;
    }

    VSMT_Base::do_Msg(msg);
}

void VSMT_Coat::PausePos()
{
    try {
        CProcess_Coat* pCoat = GET_PROCESS(PROCESS_COAT, SM_COAT,
                                           CProcess_Coat);
        Q_ASSERT(pCoat);
        pCoat->m_COAT_CLEAN_TIME.StopTime();
        pCoat->SpindleStop();
        pCoat->SetdefaultSpeed();
        pCoat->SpindleUp(false);
        pCoat->nozzeloperator_coat(INIT_COAT);
        pCoat->CoatArmUp(true);
        pCoat->MoveCoatWaitPos();
        pCoat->MoveCleanWaitPos();
    } catch (QString& s) {
        MqDebug() << s;
    }

}

