#include "cMainMenu.hpp"

using MainMenu::cMainMenu;
using MainMenu::eOptions;
using MainMenu::operator ++;
using MainMenu::operator --;

cMainMenu::cMainMenu()
	: mp_camera(nullptr), mp_font(nullptr), mp_titleText(nullptr), mp_singleText(nullptr), mp_headText(nullptr), mp_quitText(nullptr), m_option(Start), m_run(true)
{
	mp_camera = new VideoEngine::cCamera();

	m_titleSprite.setCamera(&mp_camera);
	m_singleSprite.setCamera(&mp_camera);
	m_headSprite.setCamera(&mp_camera);
	m_quitSprite.setCamera(&mp_camera);
	
	m_colour.r = 255;
	m_colour.g = 0;
	m_colour.b = 0;

	m_selectColour.r = 0;
	m_selectColour.g = 0;
	m_selectColour.b = 255;

	m_titleColour.r = 255;
	m_titleColour.g = 255;
	m_titleColour.b = 255;
}

cMainMenu::~cMainMenu()
{
	delete mp_camera;
	mp_camera = nullptr;

	delete mp_font;
	mp_font = nullptr;


}

const eOptions cMainMenu::ShowMenu()
{
	SingleSelect();
	while (m_run == true) {
		Display();
		Input();
	}

	return m_option;
}

void cMainMenu::OptionUp()
{
	--m_option;
	PlaySound();
	switch (m_option)
	{
	case Start:
		SingleSelect();
		break;
	case Head:
		HeadSelect();
		break;
	case Quit:
		QuitSelect();
		break;
	}
}

void cMainMenu::OptionDown()
{
	++m_option;
	PlaySound();
	switch (m_option)
	{
	case Start:
		SingleSelect();
		break;
	case Head:
		HeadSelect();
		break;
	case Quit:
		QuitSelect();
		break;
	}
}

void cMainMenu::OptionSelect()
{
	m_run = false;
}

void cMainMenu::setOption( const eOptions option /*= eOptions::Start*/ )
{
	m_option = option;
}

void cMainMenu::setSound( const cString dir, const cString filename )
{
	m_menuSound.setFileNameandDir(filename, dir);
	m_menuSound.setLoops(0);
}

void cMainMenu::setColour( const SDL_Colour colour )
{
	m_colour = colour;
}

void cMainMenu::setSelectColour( const SDL_Colour selectcolour )
{
	m_selectColour = selectcolour;
}

void cMainMenu::setTitleColour( const SDL_Colour titlecolour )
{
	m_titleColour = titlecolour;
}

void cMainMenu::setFont( const cString dir, const cString filename )
{
	delete mp_font;
	mp_font = nullptr;

	mp_font = new TextTypeEngine::cFont(dir, filename);
}

void cMainMenu::setTitleText( const cString text, const long int size, const long int x, const long int y )
{
	mp_titleText = new TextTypeEngine::cText(m_titleColour, &mp_font, text, size);

	m_titleSprite.setPosition(x, y);
	m_titleSprite.setImage((VideoEngine::cImage**)(&mp_titleText));
	m_titleSprite.setImageArea();
}

void cMainMenu::setSingleText( const cString text, const long int size, const long int x, const long int y )
{
	mp_singleText = new TextTypeEngine::cText(m_colour, &mp_font, text, size);

	m_singleSprite.setPosition(x, y);
	m_singleSprite.setImage((VideoEngine::cImage**)(&mp_singleText));
	m_singleSprite.setImageArea();
}

void cMainMenu::setHeadText( const cString text, const long int size, const long int x, const long int y )
{
	mp_headText = new TextTypeEngine::cText(m_colour, &mp_font, text, size);

	m_headSprite.setPosition(x, y);
	m_headSprite.setImage((VideoEngine::cImage**)(&mp_headText));
	m_headSprite.setImageArea();
}

void cMainMenu::setQuitText( const cString text, const long int size, const long int x, const long int y )
{
	mp_quitText = new TextTypeEngine::cText(m_colour, &mp_font, text, size);

	m_quitSprite.setPosition(x, y);
	m_quitSprite.setImage((VideoEngine::cImage**)(&mp_quitText));
	m_quitSprite.setImageArea();
}

///Private
void cMainMenu::ResetTextColour()
{
	if (mp_singleText != nullptr)
		mp_singleText->setColour(m_colour);

	if (mp_headText != nullptr)
		mp_headText->setColour(m_colour);

	if (mp_quitText != nullptr)
		mp_quitText->setColour(m_colour);
}

void cMainMenu::Display()
{
	mp_camera->Draw();
	VideoEngine::cVideo::Inst().Display();
}

void cMainMenu::Redraw()
{
	m_titleSprite.CameraDraw();
	m_singleSprite.CameraDraw();
	m_headSprite.CameraDraw();
	m_quitSprite.CameraDraw();
}

void cMainMenu::PlaySound()
{
	m_menuSound.PlaySound();
}

void cMainMenu::Input()
{
	EventEngine::cEventControl::Inst().CheckEvents();
}

void cMainMenu::SingleSelect()
{
	if (mp_singleText == nullptr)
		return;
	ResetTextColour();
	mp_singleText->setColour(m_selectColour);
	Redraw();
}

void cMainMenu::HeadSelect()
{
	if (mp_headText == nullptr)
		return;
	ResetTextColour();
	mp_headText->setColour(m_selectColour);
	Redraw();
}

void cMainMenu::QuitSelect()
{
	if (mp_quitText == nullptr)
		return;
	ResetTextColour();
	mp_quitText->setColour(m_selectColour);
	Redraw();
}