// Copyright (C) 2020 Daniel Mall
#include "CDemoDispatch.h"

CDemoDispatch::CDemoDispatch()
{
	m_nLastDemoSelected = eDemoDispatchId::nDemoIdNone;
}

CDemoDispatch::~CDemoDispatch()
{
}

void CDemoDispatch::Deallocate()
{
	delete m_pImgui;
	delete m_pTriangle;
	delete m_pSpokes;
}

/// activation of the selected demo
void CDemoDispatch::ActivateSelectedDemo(CDemoBase* pSelectedDemo)
{
	if (m_pDemo != nullptr)
	{
		// deactivate the current demo
		m_pDemo->Deactivate();
	}

	// activate the selected demo
	m_pDemo = pSelectedDemo;
	m_pDemo->Activate();
}

/// creation of the selected demo
void CDemoDispatch::CheckForSwitchDemo()
{
	if (m_nDemoSelected == m_nLastDemoSelected)
	{
		// there is no change in the selected demo
		return;
	}

	// create the new selected demo
	switch (m_nDemoSelected)
	{
	case eDemoDispatchId::nDemoIdImgui:
		if (m_pImgui == nullptr)
		{
			m_pImgui = new CDemoImgui;
		}
		ActivateSelectedDemo(m_pImgui);
		break;
	case eDemoDispatchId::nDemoIdSpokes:
		if (m_pSpokes == nullptr)
		{
			m_pSpokes = new CDemoSpokes;
		}
		ActivateSelectedDemo(m_pSpokes);
		break;
	case eDemoDispatchId::nDemoIdNone:
	case eDemoDispatchId::nDemoIdTriangle:
		if (m_pTriangle == nullptr)
		{
			m_pTriangle = new CDemoTriangle;
		}
		ActivateSelectedDemo(m_pTriangle);
		break;
	}

	// save the selected marker
	m_nLastDemoSelected = m_nDemoSelected;
}

/// dispatch the draw operation
void CDemoDispatch::Frame()
{
	CheckForSwitchDemo();
	if (m_pDemo != nullptr)
	{
		m_pDemo->Frame();
	}
}

void CDemoDispatch::PassAction(sg_pass_action& passAction)
{
	if (m_pDemo != nullptr)
	{
		m_pDemo->PassAction(passAction);
	}
}

/// dispatch the event (keyboard, mouse, etc.)
void CDemoDispatch::EventInput(const sapp_event* event)
{
	CheckForSwitchDemo();
	if (m_pDemo != nullptr)
	{
		m_pDemo->EventInput(event);
	}
}
