// RCRcParameterPassing.cpp : Implementation of CRCRcParameterPassing

#include "stdafx.h"
#include "RcParameterPassing.h"
#include "RpExamplesPlugin.h"


// CRcParameterPassing

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

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

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

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

STDMETHODIMP CRcParameterPassing::OnLoadDefault(void)
{
	CComQIPtr<IRsBaseNode> spBase = m_spUnkAgg;   // Get the BaseNode interface
	ATLASSERT(spBase);

	HRESULT hr;

	//declare command connectors which will serve as input parameters
	RtConDesc sNumber = {&CON_PP_FLOAT_PARAM, L"Number",	  L"Number parameter", VT_R4,  NULL, CONFLG_IN};
	RtConDesc sString = {&CON_PP_STRING_PARAM, L"String",	  L"String parameter", VT_BSTR,  NULL, CONFLG_IN};

	//create connector from descriptor and add it to our command node
	if (FAILED(hr = spBase->AddNewConnector(&sNumber, NULL))) return hr;
	if (FAILED(hr = spBase->AddNewConnector(&sString, NULL))) return hr;

	return S_OK;
}

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

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

	return hRes;
}

STDMETHODIMP CRcParameterPassing::Execute(void)
{
	CComQIPtr<IRsNode> spNode = m_spUnkAgg;

	//read connectr values and check types 
	CComVariant vFlt, vStr;
	spNode->GetValue(CON_PP_FLOAT_PARAM, &vFlt);
	if (vFlt.ChangeType(VT_R4) != S_OK)
		return E_INVALIDARG;

	spNode->GetValue(CON_PP_STRING_PARAM, &vStr);
	if (vStr.ChangeType(VT_BSTR) != S_OK)
		return E_INVALIDARG;

	//format message for output
	CStringW str;
	str.Format(L"Passed parameters: number = %f, string = '%s'", vFlt.fltVal, vStr.bstrVal);
	MessageBox(NULL,str.GetBuffer(),L"Parameter passing example", MB_OK);
	return S_OK;
}

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

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

STDMETHODIMP CRcParameterPassing::HasUndo(RtBool* isUndoable)
{
	//Command is not undoable.
	*isUndoable = false;
	return S_OK;
}


