// RiSampleItem.cpp : Implementation of CRiSampleItem
#include "stdafx.h"
#include "atlevent.h"

#include "RcSampleCmd.h"
#include "RiSampleItem.h"
#include "RpSamplePlugin.h"



// CRiSampleItem

// return string that will be recognized in stript engine
STDMETHODIMP CRiSampleItem::get_Name(BSTR* pVal)
{
	CComBSTR szName;
	szName = L"RsSample";
	return szName.CopyTo(pVal);
}

STDMETHODIMP CRiSampleItem::CreateCmdObj(const IID* piid, IRsNode** ppCmd)
{
	// initialization
    if (m_spFactory == NULL)
    {
        // get system factory
        CComPtr<IRpKernel>	spKernelPck;
		CRpSamplePlugin::GetKernelPackage(&spKernelPck);
        spKernelPck->GetSysFactory(&m_spFactory);
    }

    CComPtr<IRsCommand> spCommand;

    // create command object
    HRESULT hr = m_spFactory->CreateCommand(piid, &spCommand);
    if (FAILED(hr))
        return hr;

    CComQIPtr<IRsNode>	spNode = spCommand;
    hr = spNode->LoadDefault();
    if (FAILED(hr))
        return hr;

    return spNode.CopyTo(ppCmd);
}

STDMETHODIMP CRiSampleItem::ProcessCmd(IRsNode* pCmd)
{
	// lazy initialization
	if (m_spSysCore == NULL)
	{
		CComPtr<IRpKernel>  spKernelPackage;
		CRpSamplePlugin::GetKernelPackage(&spKernelPackage);
		spKernelPackage->GetSysCore(&m_spSysCore);
	}

	CComQIPtr<IRsCommand>	spCommand = pCmd;
	return m_spSysCore->ExecuteCommand(spCommand);
}



STDMETHODIMP CRiSampleItem::LogInfo(BSTR bszText)
{
	CComPtr<IRsNode> spCmd;

	// create command object and initialize input parameters
	HRESULT hr = CreateCmdObj(DEF_GUIDNAME_PTR(IRcSampleCmd), &spCmd);
	if (FAILED(hr))
		return hr;

	// initialize input parameters
	CComVariant vText(bszText);
	if (FAILED(hr = spCmd->SetValue(CON_SAMPLECMD_TEXTMGS, vText)))
		return hr;


	CComVariant vAction(LOGEVENT_INFO);
	vAction.ChangeType(VT_UI4);
	if (FAILED(hr = spCmd->SetValue(CON_SAMPLECMD_LOGTYPE, vAction)))
		return hr;


	// process cmd object
	return ProcessCmd(spCmd);

}

STDMETHODIMP CRiSampleItem::LogError(BSTR bszText)
{
	CComPtr<IRsNode> spCmd;

	// create command object and initialize input parameters
	HRESULT hr = CreateCmdObj(DEF_GUIDNAME_PTR(IRcSampleCmd), &spCmd);
	if (FAILED(hr))
		return hr;

	// initialize input parameters
	CComVariant vText(bszText);
	if (FAILED(hr = spCmd->SetValue(CON_SAMPLECMD_TEXTMGS, vText)))
		return hr;


	CComVariant vAction(LOGEVENT_ERROR);
	vAction.ChangeType(VT_UI4);
	if (FAILED(hr = spCmd->SetValue(CON_SAMPLECMD_LOGTYPE, vAction)))
		return hr;


	// process cmd object
	return ProcessCmd(spCmd);

}


STDMETHODIMP CRiSampleItem::Invoke(
								   /* [in] */ DISPID dispIdMember,
								   /* [in] */ REFIID riid,
								   /* [in] */ LCID lcid,
								   /* [in] */ WORD wFlags,
								   /* [out][in] */ DISPPARAMS *pDispParams,
								   /* [out] */ VARIANT *pVarResult,
								   /* [out] */ EXCEPINFO *pExcepInfo,
								   /* [out] */ UINT *puArgErr) 
{
	HRESULT hr = S_OK;
	if (pDispParams == 0) {
		return DISP_E_BADVARTYPE;
	}
	if (pDispParams->cArgs > 1) {
		return DISP_E_BADPARAMCOUNT;
	}
	if (pVarResult != 0) {
		::VariantInit(pVarResult);
	}
	ATL::CComVariant rgVars[1];
	VARIANT* rgpVars[1];
	UINT index = 0;
	for (; index < pDispParams->cArgs; ++index) {
		rgpVars[index] = &pDispParams->rgvarg[index];
	}
	VARIANT v0;
	VARIANT* v;
	switch (dispIdMember) {
		case 1:
			{
				if (pDispParams->cArgs != 1) {
					return DISP_E_BADPARAMCOUNT;
				}
				v = rgpVars[0];
				if (v->vt != VT_BSTR)
				{
					rgVars[0] = *rgpVars[0];
					v = &rgVars[0];
					if (FAILED(__VariantChangeType(v, &v0, VT_BSTR))) {
						if (puArgErr != 0) {
							*puArgErr = 0;
						}
						return DISP_E_TYPEMISMATCH;
					}
				}
				BSTR i1 = (BSTR) V_BSTR(v);
				hr = ((::IRiSampleItem*) this)->LogInfo(i1);
				break;
			}
		case 2:
			{
				if (pDispParams->cArgs != 1) {
					return DISP_E_BADPARAMCOUNT;
				}
				v = rgpVars[0];
				if (v->vt != VT_BSTR)
				{
					rgVars[0] = *rgpVars[0];
					v = &rgVars[0];
					if (FAILED(__VariantChangeType(v, &v0, VT_BSTR))) {
						if (puArgErr != 0) {
							*puArgErr = 0;
						}
						return DISP_E_TYPEMISMATCH;
					}
				}
				BSTR i1 = (BSTR) V_BSTR(v);
				hr = ((::IRiSampleItem*) this)->LogError(i1);
				break;
			}
		default:
			return DISP_E_MEMBERNOTFOUND;
	}
	if (FAILED(hr) && pExcepInfo != NULL)
	{
		AtlExcepInfoFromErrorInfo(hr, pExcepInfo);
	}
	return hr;
}


STDMETHODIMP CRiSampleItem::GetIDsOfNames(
	/* [in] */ REFIID riid,
	/* [size_is][in] */ LPOLESTR *rgszNames,
	/* [in] */ UINT cNames,
	/* [in] */ LCID lcid,
	/* [size_is][out] */ DISPID *rgDispId) 
{
	
	static LPOLESTR names[] = { L"bszText", L"LogInfo", L"LogError"};
	static DISPID dids[] = { 0, 1, 2};

	for (unsigned int i = 0; i < cNames; ++i) {
		int fFoundIt = 0;
		for (unsigned int j = 0; j < sizeof(names)/sizeof(LPOLESTR); ++j) {
			if (_wcsicmp(rgszNames[i], names[j]) == 0) {
				fFoundIt = 1;
				rgDispId[i] = dids[j];
				break;
			}
		}
		if (fFoundIt == 0) {
			return DISP_E_UNKNOWNNAME;
		}
	}
	return S_OK;
}
