// RpSamplePlugin.cpp : Implementation of CRpSamplePlugin
#include "stdafx.h"
#include "RpSamplePlugin.h"

// command factory
#include "RsSPCmdFactory.h"

// CRpSamplePlugin

CComPtr<IRpKernel>			 CRpSamplePlugin::m_spKernelPackage = NULL;
CComPtr<IRpSamplePlugin>     CRpSamplePlugin::m_spSamplePackage = NULL;


HRESULT CRpSamplePlugin::GetKernelPackage(IRpKernel** ppPackage)
{
    // if you got assert here, CmdPrompt package is not initialized 
    ATLASSERT(m_spKernelPackage != NULL);
    return m_spKernelPackage.CopyTo(ppPackage);
}

// IRsUnknownPackage Methods

STDMETHODIMP CRpSamplePlugin::Initialize(IRsUnknownPackage *pParentPackage)
{
	ATLASSERT(pParentPackage != NULL);
   
    HRESULT hRes = pParentPackage->QueryInterface(DEF_GUIDNAME(IRpKernel), (void**) &m_spKernelPackage );
	if (FAILED(hRes))
		return hRes;

    if (FAILED(hRes = QueryInterface(DEF_GUIDNAME(IRpSamplePlugin), (void**) &m_spSamplePackage)))
        return hRes;


	// cmd factory
	CComObject<CRsSPCmdFactory>* pCmdObj;
	hRes = CComObject<CRsSPCmdFactory>::CreateInstance(&pCmdObj);
	if (FAILED(hRes))
		return hRes;
	if (FAILED(hRes = pCmdObj->QueryInterface(&m_spCmdFactory)))
		return hRes;

    return S_OK;
}

STDMETHODIMP CRpSamplePlugin::UnInitialize()
{
    m_spKernelPackage.Release();
    return S_OK;
}


/// returns NodeFactory interface provided by package
STDMETHODIMP CRpSamplePlugin::GetNodeFactory(IRsNodeFactory** ppNodeFactory)
{
	return E_NOTIMPL;
}

/// returns Command Factory interface provided by package
STDMETHODIMP CRpSamplePlugin::GetCmdFactory(IRsCmdFactory** ppCmdFactory)
{
	return m_spCmdFactory.CopyTo(ppCmdFactory);
}

/// returns Data object Factory interface provided by package
STDMETHODIMP CRpSamplePlugin::GetDataObjFactory(IRsDataObjFactory** ppDataObjFactory)
{
	return E_NOTIMPL;
}

STDMETHODIMP CRpSamplePlugin::GetModuleInstance(RtInstance* pInstance)
{
	if (pInstance)
	{
		HINSTANCE hInstance = _AtlBaseModule.GetModuleInstance();
		*pInstance = (RtInstance)hInstance;
		return S_OK;
	}
	return E_INVALIDARG;
}

STDMETHODIMP CRpSamplePlugin::GetResourceInstance(RtInstance* pInstance)
{
	if (pInstance)
	{
		HINSTANCE hInstance = _AtlBaseModule.GetResourceInstance();
		*pInstance = (RtInstance)hInstance;
		return S_OK;
	}
	return E_INVALIDARG;
}

STDMETHODIMP CRpSamplePlugin::OnSysMessage(MSGID msgId, IRsUnknownMsg* pMsg)
{
    return S_OK;
}

