#include "vsmt_base.h"
#include "logic.h"
VSMT_Base::VSMT_Base(int iID, const QString &sName)
    :VSM_Thread(iID, sName)
{

}

void VSMT_Base::SM_Home(int iStep, bool bSingle)
{
    UNUSED(iStep);
    UNUSED(bSingle);
}

void VSMT_Base::do_Msg(const V_Msg &msg)
{
    QString name = m_sName;

    QString stateName = "";
    if (m_pCurState) {
        stateName = m_pCurState->m_sName;
    }
    switch (msg.m_iID) {
        case VarMsg_EMO_ID:
        {
            m_gSystem.m_bEMG = true;
            V_Msg msgStop;
            msgStop.m_iID = VarMsg_Stop_ID;
            do_Msg(msgStop);
            //        m_gSystem.m_SysMsg.SetMsg(QObject::trUtf8("EMO On"),
            //                                  NOTIFY_MSG_TYPE);
            break;
        }
        case VarMsg_StartAuto_ID:
        {
            int i = 1;
            i = 2;
        }
        break;
        case VarMsg_StartSemi_ID:
        {
            int i = 1;
            i = 2;
            break;
        }
        case VarMsg_Pause_ID:
        {
            int i = 2;
            i = 3;
        }
        break;
        case VarMsg_StopSemi_ID:
        {
            if (m_iID == SM_TRANS)
            {
                int iCurOperator = m_pState_Semi->m_iCurOperator;
                if (iCurOperator >= 0)
                {
                    if (iCurOperator == CProcess_Trans::kTransWorktableToRailNear
                         || iCurOperator == CProcess_Trans::kTransRailNearToWorktable)
                    {
                        ResManager().Release(SM_WTUnload);
                    }
                    else if (iCurOperator == CProcess_Trans::kTransRailFarToWorktable)
                    {
                        ResManager().Release(SM_WTLoad);
                    }
                }

                m_pState_Semi->m_iCurOperator = -1;
            }

            ResManager().Release(m_iID);
            break;
        }
        case VarMsg_Stop_ID:
        {
            ResManager().Release(m_iID);
            break;     
        }

    }

    /*if(msg.m_iID != 0){
        if(m_iID == SM_MAIN || m_iID == SM_COAT || m_iID == SM_CLEAN || m_iID == SM_TRANS){
            if(m_iID == SM_MAIN)
                LOG(INFO)<<"SM_MAIN:"<<msg.m_iID<<" "<<msg.m_sName.toStdString().c_str();
            else if(m_iID == SM_COAT){
                LOG(INFO)<<"SM_COAT:"<<msg.m_iID<<" "<<msg.m_sName.toStdString().c_str();
            }else if(m_iID == SM_CLEAN){
                LOG(INFO)<<"SM_CLEAN:"<<msg.m_iID<<" "<<msg.m_sName.toStdString().c_str();
            }else{
                LOG(INFO)<<"SM_TRANS:"<<msg.m_iID<<" "<<msg.m_sName.toStdString().c_str();
            }
        }
    }*/


    VSM_Thread::do_Msg(msg);
}

void VSMT_Base::run()
{
    if (m_gSystem.m_bEMG) {
        if ((!m_pCurState->IsQueueEmpty()) || !IsQueueEmpty()) {
            m_pCurState->clear_Msg();
            clear_Msg();
            //            m_gSystem.m_SysMsg.SetMsg(QObject::trUtf8("EMO On"),
            //                                      NOTIFY_MSG_TYPE);

        }
        Wait();
        return;
    }
    try {
        VSM_Thread::run();
    } catch (QString& s) {
        m_gSystem.m_SysMsg.SetMsg(s, ERR_MSG_TYPE);
    }
}

void VSMT_Base::SM_HomeAll(int iStep)
{
    if (iStep < 100) {
        if (iStep == 0) {
            //所有轴原点归0 
            //注意，工作过程中不能归0
            CMotorDriver* pMotorDriver = GET_CARD(MOTOR_DRIVER, CMotorDriver);


            CHANNELMAP::const_iterator ii = pMotorDriver->mchannels.cbegin();
            for (; ii != pMotorDriver->mchannels.cend(); ii++) {
                if ((*ii)->m_itype == CMotorDriver::MODULE_MOTOR) {
                    int motorId = (*ii)->m_iID;
                    pMotorDriver->setHomeStatus(motorId, false);
                }
            }
#ifdef __SIMULATOR__
            MSleep(200);
#endif // __SIMULATOR__
        }

        SM_Home(iStep, false);
    } else if (iStep < 200) {
        iStep -= 100;
        VSMT_Base* pSMMain = GET_STATEMACHINE(SM_MAIN, VSMT_Base);
        VSMT_Base* pSMCoat = GET_STATEMACHINE(SM_COAT, VSMT_Base);
        VSMT_Base* pSMClean = GET_STATEMACHINE(SM_CLEAN, VSMT_Base);

        pSMMain->SM_Home(iStep, false);
        pSMCoat->SM_Home(iStep, false);
        pSMClean->SM_Home(iStep, false);
    } else if (iStep == 200) {
        g_SecsManager.SendEvent(EVENT_InitComplete);
        m_gSystem.m_SysMsg
            .SetMsg(QObject::trUtf8("All Home Finished"));
    }
}

bool VSMT_Base::IsSemiPaused()
{
    QMutexLocker l(&m_SMMutex);
    VS_Thread* pState = (VS_Thread*)m_pCurState;
    return pState->m_iStateType == VS_Thread::State_Idle
        && pState->IsQueueEmpty()
        && m_pHistoryState_Semi != 0;
}

bool VSMT_Base::IsAutoPasued()
{
    QMutexLocker l(&m_SMMutex);
    VS_Thread* pState = (VS_Thread*)m_pCurState;
    return pState->m_iStateType == VS_Thread::State_Idle
        && pState->IsQueueEmpty()
        && m_pHistoryState_Auto != 0;
}

bool VSMT_Base::IsSemiRunning()
{
    QMutexLocker l(&m_SMMutex);
    VS_Thread* pState = (VS_Thread*)m_pCurState;
    return pState->m_iStateType == VS_Thread::State_Semi || pState->m_iStateType == VS_Thread::State_Work_Semi;
}

bool VSMT_Base::IsCanDoSemi()
{
    //自动状态不能启动半自动操作
    return !IsAutoPasued() && !IsAutoRun();
}

bool VSMT_Base::IsCanDoManual()
{
    //空闲或暂停状态可以启动手动指令
    return IsIdle() || IsSemiPaused() || IsAutoPasued();
}
