// RpExamplesPlugin.cpp : Implementation of CRpExamplesPlugin
#include "stdafx.h"
#include "RpExamplesPlugin.h"

// factories 
#include "RsCmdFactory.h"
#include "RsNodeFactory.h"
#include "RsDoFactory.h"

// CRpExamplesPlugin

CComPtr<IRpKernel>			 CRpExamplesPlugin::m_spKernelPackage = NULL;
CComPtr<IRsSysFactory>		 CRpExamplesPlugin::m_spSysFactory = NULL;
CComPtr<IRsSysCore>			 CRpExamplesPlugin::m_spSysCore = NULL;
CComPtr<IRpExamplesPlugin>   CRpExamplesPlugin::m_spSamplePackage = NULL;


HRESULT CRpExamplesPlugin::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 CRpExamplesPlugin::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(IRpExamplesPlugin), (void**) &m_spSamplePackage)))
        return hRes;

	//get pointers to useful system resources like resources factories or syscore
	m_spKernelPackage->GetSysFactory(&m_spSysFactory);
	m_spKernelPackage->GetSysCore(&m_spSysCore);


	//create factories

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

	//node factory
	CComObject<CRsNodeFactory>* pNodeObj;
	hRes = CComObject<CRsNodeFactory>::CreateInstance(&pNodeObj);
	if (FAILED(hRes))
		return hRes;
	if (FAILED(hRes = pNodeObj->QueryInterface(&m_spNodeFactory)))
		return hRes;

	//data object factory
	CComObject<CRsDoFactory>* pDoObj;
	hRes = CComObject<CRsDoFactory>::CreateInstance(&pDoObj);
	if (FAILED(hRes))
		return hRes;
	if (FAILED(hRes = pDoObj->QueryInterface(&m_spDataObjFactory)))
		return hRes;

    return S_OK;
}

STDMETHODIMP CRpExamplesPlugin::UnInitialize()
{
	m_spKernelPackage.Release();
	m_spSysFactory.Release();
	m_spSysCore.Release();
	m_spSamplePackage.Release();
    return S_OK;
}


/// returns NodeFactory interface provided by package
STDMETHODIMP CRpExamplesPlugin::GetNodeFactory(IRsNodeFactory** ppNodeFactory)
{
	return m_spNodeFactory.CopyTo(ppNodeFactory);
}

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

/// returns Data object Factory interface provided by package
STDMETHODIMP CRpExamplesPlugin::GetDataObjFactory(IRsDataObjFactory** ppDataObjFactory)
{
	return m_spDataObjFactory.CopyTo(ppDataObjFactory);
}

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

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

STDMETHODIMP CRpExamplesPlugin::OnSysMessage(MSGID msgId, IRsUnknownMsg* pMsg)
{
	if (msgId == MSG_SYS_REGISTER) {
		//called when plugin has been loaded into rosetta
	}
	else if (msgId == MSG_SYS_UNREGISTER) {
		//called when plugin has been unloaded from rosetta
	}
	else {
		//handle other system messages here 
	};
    return S_OK;
}

