#include "vsmt_transfer.h"
#include "logic.h"
#include "vst_idle_transfer.h"
#include "vst_work_transfer.h"
//#include "vst_wait_transfer.h"
#include "vst_semi_transfer.h"
#include "vst_wait_transfer_ex.h"
#include "cprocess_scan.h"
VSMT_Transfer::VSMT_Transfer(int iID, const QString &sName)
    : VSMT_Base(iID, sName)
{
    mprocesss[PROCESS_TRANS] =
        new CProcess_Trans(m_gSystem.m_mapNameList[PROCESS_TRANS],
                           PROCESS_TRANS);
    mprocesss[PROCESS_SCAN] =
        new CProcess_Scan(m_gSystem.m_mapNameList[PROCESS_SCAN],
                          PROCESS_SCAN);
}

void VSMT_Transfer::SM_Home(int iStep, bool bSingle)
{
    CMotorDriver* pMotorDriver = GET_CARD(MOTOR_DRIVER, CMotorDriver);
    Q_ASSERT(pMotorDriver);
    CProcess_Trans* pTrans = (CProcess_Trans*)FindProcess(PROCESS_TRANS);
    Q_ASSERT(pTrans);
    pTrans->AssertRailNearHasWafer(false, QObject::tr("Home Axis"));
    pTrans->AssertRailFarHasWafer(false, QObject::tr("Home Axis"));

    if (pTrans->m_pSlotGrab || pTrans->m_pSlotRailNear || pTrans->m_pSlotRailFar) {
        MQT_THROW_CODESTR(ALARM_TRANS_SAFE_PROTECT, QObject::trUtf8("rail has wafer."));
    }
    switch (iStep) {
        case 1:
            //打开夹抓，防止异常断电的情况下仍然夹有晶圆的情况
            pTrans->ClawRelease();
            pMotorDriver->home(MOTORH1_UPDOWN);
            pMotorDriver->home(MOTORJ1_UPDOWN);
            pMotorDriver->wait_home_finished(MOTORH1_UPDOWN);
            pMotorDriver->wait_home_finished(MOTORJ1_UPDOWN);
            break;
        case 2:
            pMotorDriver->home(MOTORH2_TRANS);
            pMotorDriver->home(MOTORJ2_TRANS);
            pMotorDriver->wait_home_finished(MOTORH2_TRANS);
            pMotorDriver->wait_home_finished(MOTORJ2_TRANS);
            break;
        case 3:
            pMotorDriver->home(MOTORD_TRANS);
            pMotorDriver->wait_home_finished(MOTORD_TRANS);
            break;
        case 4:
            pMotorDriver->home(MOTORA_CLAW);
            pMotorDriver->wait_home_finished(MOTORA_CLAW);
            break;
        case 5:
            pMotorDriver->home(MOTORB1_LOCK);
            pMotorDriver->home(MOTORB2_RAIL);
            pMotorDriver->home(MOTORC_CASS);
            break;
        case 6:
            pMotorDriver->wait_home_finished(MOTORB1_LOCK);
            pMotorDriver->wait_home_finished(MOTORB2_RAIL);
            pMotorDriver->wait_home_finished(MOTORC_CASS);
            break;
        case 8:
            pTrans->MoveInitPos();
            break;
        case 10:
            if (bSingle) {
                m_gSystem.m_SysMsg.SetMsg(QObject::trUtf8("%1 home finished")
                                          .arg(m_sName));
            }
            break;
    }
}

void VSMT_Transfer::init_Machine()
{
    m_pState_Idle = new VST_Idle_Transfer(VarState_Idle_Transfer, QObject::trUtf8("Transfer Idle"), this);
    //m_pState_Wait= new VST_Wait_Transfer(VarState_Wait_Transfer,QObject::trUtf8("Transfer Wait"), this);
    m_pState_Wait = new VST_Wait_TransferEx(VarState_Wait_Transfer, QObject::trUtf8("Transfer Wait"), this);
    m_pState_Semi = new VST_Semi_Transfer(VarState_Semi_Transfer, QObject::trUtf8("Transfer Test"), this);

    VSMT_Base::init_Machine();
}

void VSMT_Transfer::do_Msg(const V_Msg& msg)
{
    switch (msg.m_iID) {
        case VarMsg_StopSemi_ID:
        {
            //((VST_Semi_Transfer*)m_pState_Semi)->m_iCurOperator = -1;
            break;
        }
        case VarMsg_Stop_ID:
        {
            ((VST_Semi_Transfer*)m_pState_Semi)->m_iCurOperator = -1;
            break;
        }
        case VarMsg_AbortAutoTrans:
        {
            //取消传片，暂时没有办法处理
            LOG_WARN("Abort auto transfer");
            return;

        }
        break;
    }
    VSMT_Base::do_Msg(msg);
}

