// RootLayer.cpp : Implementation of cRootLayer
#include "stdafx.h"
#include "Inject.h"
#include "RootLayer.h"

#include "BarLayer.h"
#include "Panel.h"
#include "View.h"

enum eChildIDs
{
   eChildBars,
   eChildPanel
};

/////////////////////////////////////////////////////////////////////////////
// cRootLayer

void cRootLayer::addView( cView *pView, ILayer *pRoot )
{
   m_views.push_back( pView );

   ViewParams vp = { pView->m_nIcon, pView->m_nIconModule, pView->m_strTitle, pView->m_nLeft, pView->m_nTop, pView->m_nWidth, pView->m_nHeight };

   // Next, make the bar entry
   m_pBars->AddBar( pView->m_nViewID, &vp );

   // Last, make the view entry
   m_pPanel->AddView( pView->m_nViewID, pRoot );
}

void cRootLayer::removeView( cView *pView )
{
   for( cViewList::iterator i = m_views.begin(); i != m_views.end(); ++ i )
   {
      if( *i == pView )
      {
         m_views.erase( i );
         break;
      }
   }
}

void cRootLayer::onCreate()
{
   LayerParams lpBars = { eChildBars, { 0, 0, 1, 1 }, 0 };

   CComObject< cBarLayer > *pBars;
   CComObject< cBarLayer >::CreateInstance( &pBars );

   m_pBars = pBars;

   m_pSite->CreateChild( &lpBars, pBars );

   LayerParams lpPanel = { eChildPanel, { 0, 0, 0, 0 }, 0 };

   CComObject< cPanel > *pPanel;
   CComObject< cPanel >::CreateInstance( &pPanel );

   m_pPanel = pPanel;

   m_pSite->CreateChild( &lpPanel, pPanel );

   m_pPanel->putref_Sink( this );
}

void cRootLayer::onDestroy()
{
   m_pBars.Release();
   m_pPanel.Release();
}

STDMETHODIMP cRootLayer::CreateView(ViewParams *pParams, ILayer *pLayer, IView **ppView)
{
   // First create the view object
   CComObject< cView > *pView;
   CComObject< cView >::CreateInstance( &pView );

   pView->m_pRoot = this;
   pView->m_nViewID = m_nNextViewID ++;
   pView->m_strTitle = pParams->label;
   pView->m_nIcon = pParams->icon;
   pView->m_nIconModule = pParams->iconLibrary;
   pView->m_nLeft = pParams->left;
   pView->m_nTop = pParams->top;
   pView->m_nWidth = pParams->width;
   pView->m_nHeight = pParams->height;

   addView( pView, pLayer );

   *ppView = pView;
   pView->AddRef();

	return S_OK;
}

STDMETHODIMP cRootLayer::SelectBar(long nID)
{
	// We are given the Bar ID, find the matching view
	for( cViewList::iterator i = m_views.begin(); i != m_views.end(); ++ i )
	{
		if( ( *i )->m_nViewID == nID )
		{
			long nPrevActive;
			m_pPanel->get_ActiveView( &nPrevActive );

			if( nPrevActive != nID )
			{
				ViewParams p = { ( *i )->m_nIcon, ( *i )->m_nIconModule, ( *i )->m_strTitle, ( *i )->m_nLeft, ( *i )->m_nTop, ( *i )->m_nWidth, ( *i )->m_nHeight };
				m_pPanel->ActivateView( ( *i )->m_nViewID, &p );

				( *i )->Fire_Activate();
			}
			else
			{
				( *i )->Deactivate();
			}

			return S_OK;
		}
	}

	// Invalid return value
	return E_INVALIDARG;
}

STDMETHODIMP cRootLayer::LoadView(BSTR strXML, IView **ppView)
{
   CComObject< cView > *pView;
   CComObject< cView >::CreateInstance( &pView );

   CComPtr< IUnknown > pRootControl;

   pView->m_pRoot = this;
   pView->m_nViewID = m_nNextViewID ++;
   pView->loadSchema( strXML, &pRootControl );

   ViewParams vp = { pView->m_nIcon, pView->m_nIconModule, pView->m_strTitle, pView->m_nLeft, pView->m_nTop, pView->m_nWidth, pView->m_nHeight };
   
   m_views.push_back( pView );

   *ppView = pView;
   pView->AddRef();

   // Next, make the bar entry
   m_pBars->AddBar( pView->m_nViewID, &vp );

   // Last, create all the controls
   m_pPanel->LoadView( pView->m_nViewID, pView, pRootControl );

	return S_OK;
}

STDMETHODIMP cRootLayer::PanelDeactivate(long nViewID)
{
   // Find the view of our desires and fire it's hide message
   for( cViewList::iterator i = m_views.begin(); i != m_views.end(); ++ i )
   {
      if( ( *i )->m_nViewID == nViewID )
         ( *i )->Fire_Deactivate();
   }

   return S_OK;
}

STDMETHODIMP cRootLayer::LoadViewObject(IUnknown *pSchema, IView **ppView)
{
   CComObject< cView > *pView;
   CComObject< cView >::CreateInstance( &pView );

   CComPtr< IUnknown > pRootControl;

   pView->m_pRoot = this;
   pView->m_nViewID = m_nNextViewID ++;
   pView->loadSchemaObject( pSchema, &pRootControl );

   ViewParams vp = { pView->m_nIcon, pView->m_nIconModule, pView->m_strTitle, pView->m_nLeft, pView->m_nTop, pView->m_nWidth, pView->m_nHeight };
   
   m_views.push_back( pView );

   *ppView = pView;
   pView->AddRef();

   // Next, make the bar entry
   m_pBars->AddBar( pView->m_nViewID, &vp );

   // Last, create all the controls
   m_pPanel->LoadView( pView->m_nViewID, pView, pRootControl );

	return S_OK;
}
