// RcRcSampleCmd.cpp : Implementation of CRcRcSampleCmd

#include "stdafx.h"
#include "RcSampleCmd.h"
#include "RpSamplePlugin.h"


// CRcSampleCmd

STDMETHODIMP CRcSampleCmd::OnGetValue(CONID conID, IRsDataCache* pDataCache, VARIANT* pVtData, RtBool* pHandled)
{
	return S_OK;
}

STDMETHODIMP CRcSampleCmd::OnSetValue(CONID con_id, IRsDataCache* pDataCache, VARIANT* pvtData, RtBool* pHandled)
{
	return S_OK;
}
STDMETHODIMP CRcSampleCmd::OnDefaultValue(CONID con_id, VARIANT* pVtData, RtBool* pHandled)
{
	return S_OK;
}

STDMETHODIMP CRcSampleCmd::OnInvalidate(CONID con_id, RtBool* pHandled)
{
	return S_OK;
}

STDMETHODIMP CRcSampleCmd::OnInitialize(void)
{
	return S_OK;
}

STDMETHODIMP CRcSampleCmd::OnLoadDefault(void)
{
	HRESULT hRes; 

	CComQIPtr<IRsBaseNode> spBase = m_spUnkAgg;

	// add connectors
	RtConDesc sDescText = {&CON_SAMPLECMD_TEXTMGS, L"TextMessage", L"Text message added to the log file", VT_BSTR,  NULL, CONFLG_IN};
	if (FAILED(hRes = spBase->AddNewConnector(&sDescText, NULL)))
		return hRes;

	RtConDesc   sLogType =  { &CON_SAMPLECMD_LOGTYPE, L"LogType",  L"Type of the log message",  VT_UI4,  NULL, CONFLG_IN};   
	if (FAILED(hRes = spBase->AddNewConnector(&sLogType, NULL)))
		return hRes;

	return S_OK;
}

STDMETHODIMP CRcSampleCmd::OnDestroy(void)
{
	return S_OK;
}

HRESULT CRcSampleCmd::FinalConstruct()
{
	// kernel package
	CComPtr<IRpKernel> spKernelPck;
	HRESULT hRes = CRpSamplePlugin::GetKernelPackage(&spKernelPck);
	
	// create base node
	if (SUCCEEDED(hRes))
		hRes = spKernelPck->CreateBaseNode(GetControllingUnknown(), &m_spUnkAgg);

	return hRes;
}

STDMETHODIMP CRcSampleCmd::Execute(void)
{
	HRESULT hRes;
	CComQIPtr<IRsBaseNode> spBase = m_spUnkAgg; 

	// text mesasge
	CComVariant vData;
	if (FAILED(hRes = spBase->GetValue(CON_SAMPLECMD_TEXTMGS, &vData)))
		return hRes;
	CComBSTR bszTextMsg = V_BSTR(&vData);


	// package operation on selection (copy/cut/ paste)
	CComVariant vPackageOp;
	if (FAILED(hRes = spBase->GetValue(CON_SAMPLECMD_LOGTYPE, &vPackageOp)))
		return hRes;
	RtLogTypeEvent eLogType = (RtLogTypeEvent)(V_UI4(&vPackageOp));

	switch(eLogType)
	{
	case LOGEVENT_INFO:
		// add to info message to the log
		RS_LOG(LOGLEV_INFO, S_OK, bszTextMsg); 
		break;
	
	case LOGEVENT_ERROR:
		// add to info message to the log
		RS_LOG(LOGLEV_ERROR, S_OK, bszTextMsg); 
		break;
	
	default:
		return E_INVALIDARG;
	}
		
	return S_OK;
}

STDMETHODIMP CRcSampleCmd::Undo(void)
{
	return S_OK;
}

STDMETHODIMP CRcSampleCmd::Redo(void)
{
	return S_OK;
}

STDMETHODIMP CRcSampleCmd::HasUndo(RtBool* isUndoable)
{
	*isUndoable = static_cast<RtBool>(false);
	return S_OK;
}


