#include "BreakoutPauseScene.h"

#include "GameConfig.h"

#include "Vector2.h"

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

BreakoutPauseScene::BreakoutPauseScene()
: m_bIsActive(false)
{

}

BreakoutPauseScene::~BreakoutPauseScene()
{

}

void BreakoutPauseScene::Initialise()
{
	Visualisation* pVis = Visualisation::getSingleton();

	m_vTextLines.push_back(pVis->CreateSpriteText("PAUSED", MENU_NORMAL_COLOUR));
	m_vTextLines.push_back(pVis->CreateSpriteText("PRESS START TO CONTINUE", MENU_NORMAL_COLOUR));
	m_vTextLines.push_back(pVis->CreateSpriteText("PRESS L, R AND START TO QUIT", MENU_NORMAL_COLOUR));

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

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

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

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

	int numEntries = m_vTextLines.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 xPos = centre.x - (pVis->GetSpriteWidth(m_vTextLines[i]) / 2);
		pVis->DrawSprite(m_vTextLines[i], xPos, yPos);

		yPos += CHARACTER_HEIGHT * 2;
	}
}

void BreakoutPauseScene::Update()
{

}

void BreakoutPauseScene::Shutdown()
{
	m_vTextLines.clear();
}

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

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

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

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

		if(key == eKeyButtonStart && pButtonEvent->IsKeyDown())
		{
			BreakoutUnpauseGameEvent* pEvent = new BreakoutUnpauseGameEvent();
			EventManager::getSingleton()->QueueEvent(pEvent);
		}
	}

	return false;
}
