#include "vst_work_base.h"
#include "logic.h"
#include "msg_def.h"
#include "vsmt_base.h"
VST_Work_Base::VST_Work_Base(int iID, const QString &sName,
                             VSM_Thread *pParent,
                             VS_Thread *pPreState,
                             VS_Thread *pNextState)
    :VST_Work(  iID, sName, pParent,
               pPreState, pNextState)
{

}

//处理回原点的过程
void VST_Work_Base::do_Msg(const V_Msg &msg)
{
	//if(0 != msg.m_iID){
	//	if(m_pParent&&m_pParent->m_iID== SM_MAIN)
	//		//LOG(INFO)<<"SMID:"<<m_pParent->m_iID<<"State:"<<m_iStateType<<"StateID:"<<m_iID<<"StateName:"<<m_sName.toStdString().c_str()<<"msgid:"<<msg.m_iID<<"MSG:"<<msg.m_sName.toStdString().c_str();
	//}
    if(msg.m_iID==VarMsg_Home_step)
    {
        ((VSMT_Base*)m_pParent)->SM_Home(msg.m_vlInput[0].toInt(),
                msg.m_vlInput[1].toInt());
    }
    else if(msg.m_iID==VarMsg_All_Home_step)
    {
        ((VSMT_Base*)m_pParent)->SM_HomeAll(msg.m_vlInput[0].toInt());
    }
    else if (msg.m_iID == VarMsg_AcquireResouce) {
        int vsmId = msg.m_vlInput[0].toInt();
        uint resId = msg.m_vlInput[1].toUInt();
        if (false == ResManager().Acquire(vsmId, resId)) {
            MQT_THROW_CODESTR(ALARM_RESERVED, QObject::tr("can't acquire resource %1").arg(resId));
        }
    } else if (msg.m_iID == VarMsg_ReleaseResouce) {
        int vsmId = msg.m_vlInput[0].toInt();
        uint resId = msg.m_vlInput[1].toUInt();
        if (vsmId <= 0) {
            ResManager().ReleaseForce(resId);
        } else {
            ResManager().Release(vsmId, resId);
        }
	}
	else if(msg.m_iID == VarMsg_Driver_ID)
	{
		V_Msg msg1 = msg;
		Q_ASSERT(msg1.m_vlInput.size() >= 2);
		int iDriverID = msg1.m_vlInput.at(0).toInt();
		int iDriverMsgID = msg1.m_vlInput.at(1).toInt();
		msg1.m_vlInput.pop_front();
		msg1.m_vlInput.pop_front();
		CDriverCard* pDriverCard = GET_CARD(iDriverID, CDriverCard);
		Q_ASSERT(pDriverCard);
		pDriverCard->do_msg(iDriverMsgID, msg1.m_vlInput, msg1.m_pvlOutput);
	}
    else if (msg.m_iID == VarMsg_ReleaseSchedularAndLockRes)
    {
        Q_ASSERT("not support VarMsg_ReleaseSchedularAndLockRes");
    }
	else if(msg.m_iID == VarMsg_StateExit_ID){
#if 0
		int iID = msg.m_vlInput[0].toInt();
		VST_Work_Base* pThat = this;
		if(m_iID == iID){
			//基类处理VarMsg_StateExit_ID时会将m_pParent->m_SMMutex加锁
			//QMutexLocker locker(&m_pParent->m_SMMutex);
			//VST_Work::do_Msg(msg);
			QString name = m_sName;
			clearAllNextState();

			VSM_Thread* pParent = m_pParent;
			VS_Thread* pPreSate = m_pPreState;

			pPreSate->m_pNextState = nullptr;
			m_pParent->SetState(m_pPreState);
			delete this;
			
			return;
		}
#endif // __SIMULATOR__
	}
    VST_Work::do_Msg(msg);
}

