// RiAnimPlugin.cpp : Implementation of CRiAnimPlugin

#include "stdafx.h"
#include "RiAnimPlugin.h"
#include "RpAnimPluginPackage.h"
#include "RcApOpenToolbar.h"
#include "RcCreateProcClip.h"

STDMETHODIMP CRiAnimPlugin::get_Name(BSTR* pVal)
{
	CComBSTR szName = L"RsAnimPlugin";
	*pVal = szName.Detach();
	return S_OK;
};

STDMETHODIMP CRiAnimPlugin::CreateAnimToolbar()
{
	//create hello world command
	CComPtr<IRsNode> spCmd;
	HRESULT hr = CreateCmdObj(DEF_GUIDNAME_PTR(IRcApOpenToolbar), &spCmd);
	if (FAILED(hr))
		return hr;

	//execute it
	return ProcessCmd(spCmd);
}


STDMETHODIMP CRiAnimPlugin::CreateProcClip(BSTR bszOwner)
{
	//create hello world command
	CComPtr<IRsNode> spCmd;
	HRESULT hr = CreateCmdObj(DEF_GUIDNAME_PTR(IRcCreateProcClip), &spCmd);
	if (FAILED(hr))
		return hr;

	CComVariant vData(bszOwner);
	if(FAILED(hr = spCmd->SetValue(CON_ANIMCMD_CLIPNAME, vData)))
		return hr;

	//execute it
	return ProcessCmd(spCmd);
}


HRESULT CRiAnimPlugin::CreateCmdObj(const IID* piid, IRsNode** ppCmd)
{
	// initialization
    if (m_spFactory == NULL)
    {
        // get system factory
        CComPtr<IRpKernel>	spKernelPck;
		CRpAnimPlugin::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);
}


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

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