#include "BreakoutMainMenuScene.h"

#include "GameConfig.h"

#include "Vector2.h"

#include "ApplicationEvents.h"
#include "InputEvents.h"
#include "BreakoutEvents.h"
#include "EventManager.h"

BreakoutMainMenuScene::BreakoutMainMenuScene()
: m_bIsActive(false), m_selectedEntry(0), m_gameTitleID(0), m_gameTitleShadowID(0)
{

}

BreakoutMainMenuScene::~BreakoutMainMenuScene()
{

}

void BreakoutMainMenuScene::Initialise()
{
	Visualisation* pVis = Visualisation::getSingleton();
	MenuEntry startEntry;
	startEntry.Name = MENU_START_GAME_TEXT;
	startEntry.NormalSprite = pVis->CreateSpriteText(startEntry.Name, MENU_NORMAL_COLOUR);
	startEntry.SelectedSprite = pVis->CreateSpriteText(startEntry.Name, MENU_SELECTED_COLOUR);
	m_vMenuEntries.push_back(startEntry);

	MenuEntry quitEntry;
	quitEntry.Name = MENU_QUIT_GAME_TEXT;
	quitEntry.NormalSprite = pVis->CreateSpriteText(quitEntry.Name, MENU_NORMAL_COLOUR);
	quitEntry.SelectedSprite = pVis->CreateSpriteText(quitEntry.Name, MENU_SELECTED_COLOUR);
	m_vMenuEntries.push_back(quitEntry);

	m_gameTitleID = Visualisation::getSingleton()->CreateSpriteText(GAME_TITLE, GAME_TITLE_COLOUR);
	m_gameTitleShadowID = Visualisation::getSingleton()->CreateSpriteText(GAME_TITLE, GAME_TITLE_SHADOW_COLOUR);

	EventManager::getSingleton()->AddEventListener(ButtonInputEvent::cEventName, this);
}

void BreakoutMainMenuScene::Draw()
{	
	if(!m_bIsActive)
		return;

	Visualisation* pVis = Visualisation::getSingleton();
	pVis->ClearScreen();

	SDL_Surface* pScreen = pVis->GetScreen();
	Vector2i centre(pScreen->w / 2, pScreen->h / 2);

	Vector2i titlePos;
	std::string title = GAME_TITLE;
	titlePos.x = centre.x - (pVis->GetSpriteWidth(m_gameTitleID) / 2);
	titlePos.y = pScreen->h / 5;
	pVis->DrawSprite(m_gameTitleShadowID, titlePos.x, titlePos.y);
	int offset = CHARACTER_WIDTH / 4;
	pVis->DrawSprite(m_gameTitleID, titlePos.x + offset, titlePos.y + offset);

	int numEntries = m_vMenuEntries.size();
	int totalHeight = numEntries * CHARACTER_HEIGHT * 2; //Multiplied by 2 to add some spacing between entries.
	int yPos = centre.y - (totalHeight / 2);

	for(int i = 0 ; i < numEntries; i++)
	{
		int strSize = m_vMenuEntries[i].Name.size();
		int xPos = centre.x - (pVis->GetSpriteWidth(m_vMenuEntries[i].NormalSprite) / 2);
		if(i == m_selectedEntry)
			pVis->DrawSprite(m_vMenuEntries[i].SelectedSprite, xPos, yPos);
		else
			pVis->DrawSprite(m_vMenuEntries[i].NormalSprite, xPos, yPos);


		yPos += CHARACTER_HEIGHT * 2;
	}
}

void BreakoutMainMenuScene::Update()
{

}

void BreakoutMainMenuScene::Shutdown()
{
	m_vMenuEntries.clear();
}

void BreakoutMainMenuScene::Activate()
{
	m_bIsActive = true;
}

void BreakoutMainMenuScene::Deactivate()
{
	m_bIsActive = false;
}

bool BreakoutMainMenuScene::HandleEvent(IEvent *pEvent)
{
	if(!m_bIsActive)
		return false;

	if(pEvent->GetEventType() == ButtonInputEvent::cEventName)
	{
		ButtonInputEvent* pButtonEvent = (ButtonInputEvent*)pEvent;
		int key = pButtonEvent->GetKey();

		if((key == eKeyUp || key == eKeyUpLeft || key == eKeyUpRight) && pButtonEvent->IsKeyDown())
		{
			m_selectedEntry--;
			if(m_selectedEntry < 0)
				m_selectedEntry = 0;

			return true;
		}

		if((key == eKeyDown || key == eKeyDownLeft || key == eKeyDownRight) && pButtonEvent->IsKeyDown())
		{
			m_selectedEntry++;
			if(m_selectedEntry >= m_vMenuEntries.size())
				m_selectedEntry = m_vMenuEntries.size() - 1;

			return true;
		}

		if(key == eKeyButtonB)
		{
			std::string name = m_vMenuEntries[m_selectedEntry].Name;

			if(name == MENU_START_GAME_TEXT)
			{
				BreakoutStartGameEvent* pStartEvent = new BreakoutStartGameEvent();
				EventManager::getSingleton()->QueueEvent(pStartEvent);
			}
			else if(name == MENU_QUIT_GAME_TEXT)
			{
				ApplicationQuitEvent* pQuitEvent = new ApplicationQuitEvent();
				EventManager::getSingleton()->QueueEvent(pQuitEvent);
			}

			return true;
		}
	}
	return false;
}
