#include "logic.h"
#include "vst_wait_base.h"
#include "msg_def.h"

VST_Wait_Base::VST_Wait_Base(int iID, const QString& sName, VSM_Thread* pParent)
    : VST_Wait(iID, sName, pParent)
{

}

void VST_Wait_Base::do_Msg(const V_Msg& msg)
{
    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();
        ResManager().Release(vsmId, resId);
    }
    else if (msg.m_iID == VarMsg_ReleaseSchedularAndLockRes)
    {
        int vsmId = msg.m_vlInput[0].toInt();
        uint resId = msg.m_vlInput[1].toUInt();
        uint acqResId = msg.m_vlInput[2].toUInt();
        if (!ResManager().CanAcquire(vsmId, resId))
        {
            MQT_THROW_CODESTR(ALARM_RESERVED, QObject::tr("can't acquire resource %1 in VST_Wait_Base").arg(resId));
        }

        ResManager().Acquire(vsmId, acqResId);

        ResManager().Release(vsmId, resId);
    }

    VST_Wait::do_Msg(msg);
}
