// RpViewPackage.h : Declaration of the CRpViewPackage
// CRpViewPackage represents the plugin. We implement here the
// IRsUnknownPackage interface that is asked during the plugin loading.
// The interface then enable communication with system and all other
// packages.

#pragma once

#define VIEW_PACKAGE_VERSION1			1	// package version
#define VIEW_PACKAGE_VERSION			VIEW_PACKAGE_VERSION1  

[
	object,
	helpstring("IRpViewPackage Interface"),
	uuid("AE9FC197-3975-4525-89E5-3E888F940F6C"),
	pointer_default(unique)
]
__interface IRpViewPackage : public IUnknown
{
public:
};



[
	ATTRIBUTE_COCLASS,
	threading(both),
	vi_progid("RsViewPlugin.RpViewPackage"),
	progid("RsViewPlugin.RpViewPackage.1"),
	version(1.0),
	uuid("C80B54A0-DB26-4632-851F-0D0CF9A34D93"),
	helpstring("RpViewPackage Class")
]
class ATL_NO_VTABLE CRpViewPackage: 
	public IRpViewPackage,
    public IRsUnknownPackage,
    public IRsModulePackage,
	public IRsPackageDesc
{
public:
    CRpViewPackage()
    {
    }

    HRESULT FinalConstruct()
    {
        return S_OK;
    }

    void FinalRelease() 
    {
    }

    BEGIN_COM_MAP(CRpViewPackage)
        COM_INTERFACE_ENTRY(IRpViewPackage)
        COM_INTERFACE_ENTRY2(IRsObject, IRsUnknownPackage)
        COM_INTERFACE_ENTRY(IRsUnknownPackage)
        COM_INTERFACE_ENTRY(IRsModulePackage)
    END_COM_MAP()

    DECLARE_CLASSFACTORY_SINGLETON(CRpViewPackage)
    DECLARE_NOT_AGGREGATABLE(CRpViewPackage)
    DECLARE_PROTECT_FINAL_CONSTRUCT()

    // IRsUnknownPackage
    STDMETHOD(Initialize)(IRsUnknownPackage *pParentPackage);           // Called to initialize this package
    STDMETHOD(UnInitialize)();                                          // This method releases m_pKernelPackage.
    STDMETHOD(OnSysMessage)(MSGID msgId, IRsUnknownMsg* pMsg);          // package messages handler
    STDMETHOD(GetNodeFactory)(IRsNodeFactory** ppNodeFactory);          // returns NodeFactory interface provided by package
    STDMETHOD(GetCmdFactory)(IRsCmdFactory** ppCmdFactory);             // returns Command Factory interface provided by package
    STDMETHOD(GetDataObjFactory)(IRsDataObjFactory** ppDataObjFactory); // returns Data Object Factory interface provided by package
    STDMETHOD(GetModuleInstance)(RtInstance* pInstance);                // returns Package module instance
    STDMETHOD(GetResourceInstance)(RtInstance* pInstance);              // returns Package resource instance

    // IRsObject Methods
    RS_DECLARE_IRS_OBJECT(CRpViewPackage, L"ViewPlugin Package");

    // package version
    RS_DECLARE_PACKAGE_VERSION(PRODUCT_VERSION, VIEW_PACKAGE_VERSION);
	RS_PACKAGE_DESCRIPTION(L"View sdk example");

public:
    // Help functions
	static HRESULT GetKernelPackage(IRpKernel** ppPackage); // Returns kernel package interface
	static HRESULT GetKernelNode(IRnKernel** ppKernelNode); // Returns kernel node 
	static HRESULT GetSysFactory(IRsSysFactory** ppSysFactory); // Returns system factory 
    static HRESULT CreateDataObj(const IID* piid, IRsDataObject** ppDataObj);
    static HRESULT CreateCmdObj(const IID *piid, IRsNode **ppCmd);
    static HRESULT CreateNode(const IID *piid, IRsNode **ppNode);
    static HRESULT ExecuteCommand(IRsNode *pCmdNode);   // Call to synchronous execution of commands
	static HRESULT SendCommand(IRsNode *pCmdNode);      // Send command to commands' queue
    
    // Log support
    RS_LOG_ACCESS(m_spKernelPackage)
    #define RS_LOG(lev, hr, text) CRpViewPackage::MyLogEntry(lev, hr, text, __FILE__, __LINE__);   


private:
    // Pointer to kernel Package.
    static CComPtr<IRpKernel>			m_spKernelPackage;
    // Pointer to sys factory for creating rs objects
    static CComPtr<IRsSysFactory>		m_spSysFactory;
    // Pointer to sys core for synchronous command processing
    static CComPtr<IRsSysCore>			m_spSysCore;

    // Pointer to view Package.
    static CComPtr<IRpViewPackage>		m_spSamplePackage;

    // node factory
    CComPtr<IRsNodeFactory>				m_spNodeFactory;
    // command factory
    CComPtr<IRsCmdFactory>				m_spCmdFactory;
};


