//
//  MainMenu.cpp
//  GlobalGameJam15
//
//  Created by Cesar Parent on 25/01/2015.
//  Copyright (c) 2015 Cesar Parent. All rights reserved.
//

#include "MainMenu.h"
#include "Defines.h"
#include "DungeonGame.h"
#include "CreditsScreen.h"

MainMenu::MainMenu(Game& game) : Scene(game, kGameWidth/2, kGameHeight/2)
{
	setAnchorPoint(0, 0);
	setPosition(0, 0);
	setXScale(2);
	setYScale(2);
	
	_background = new SpriteNode("assets/uis/background.png");
	_background->setPosition(kGameWidth/4, kGameHeight/4);
	addChild(_background);
	
	_title = new LabelNode("Kryptomaniac", "assets/uis/font");
	_title->setAnchorPoint(.5, 0);
	_title->setPosition(kGameWidth/4, 20);
	_title->setFontSize(50);
	addChild(_title);
	
	_items[0] = new LabelNode("start", "assets/uis/font");
	_items[1] = new LabelNode("credits", "assets/uis/font");
	_items[2] = new LabelNode("quit", "assets/uis/font");
	
	for(int i = 0; i < 3; ++i)
	{
		_items[i]->setFontSize(30);
		_items[i]->setPosition(kGameWidth/4, 150+(i*40));
		addChild(_items[i]);
	}
	
	_currentItem = 0;
	for(int i = 0; i < 3; ++i)
	{
		if(i == _currentItem)
		{
			_items[i]->setColor({0x00,0x00,0x00,0xff});
			_items[i]->setBackground({0xff,0xff,0xff,0xff});
		}
		else
		{
			_items[i]->setColor({0xff,0xff,0xff,0xff});
			_items[i]->setBackground({0xff,0xff,0xff,0x00});
		}
	}
}

MainMenu::~MainMenu()
{
	delete _background;
	delete _title;
	for(int i = 0; i < 3; ++i)
	{
		delete _items[i];
	}
}

void MainMenu::exit()
{
	if(_currentItem == 0)
	{
		_game.pushScene(new DungeonGame(_game));
	}
	else if(_currentItem == 1)
	{
		_game.pushScene(new CreditsScreen(_game));
	}
	else if(_currentItem == 2)
	{
		_game.quit();
	}
}

#pragma mark delegate methods

void MainMenu::didEnterStack()
{
	Controls::setDelegate(this);
}

void MainMenu::didLeaveStack()
{
	Controls::setDelegate(nullptr);
}

void MainMenu::controllerButtonPressBegan(int controller, SDL_GameControllerButton button)
{
	if(button == SDL_CONTROLLER_BUTTON_A)
	{
		exit();
	}
	else if(button == SDL_CONTROLLER_BUTTON_DPAD_UP)
	{
		_currentItem--;
	}
	else if(button == SDL_CONTROLLER_BUTTON_DPAD_DOWN)
	{
		_currentItem++;
	}
	if(_currentItem < 0) _currentItem = 0;
	if(_currentItem > 2) _currentItem = 2;
	
	for(int i = 0; i < 3; ++i)
	{
		if(i == _currentItem)
		{
			_items[i]->setColor({0x00,0x00,0x00,0xff});
			_items[i]->setBackground({0xff,0xff,0xff,0xff});
		}
		else
		{
			_items[i]->setColor({0xff,0xff,0xff,0xff});
			_items[i]->setBackground({0xff,0xff,0xff,0x00});
		}
	}
}


void MainMenu::keyboardPressBegan(SDL_Scancode key)
{
	if(key == SDL_SCANCODE_UP)
	{
		_currentItem--;
	}
	else if(key == SDL_SCANCODE_DOWN)
	{
		_currentItem++;
	}
	else if(key == SDL_SCANCODE_RETURN)
	{
		exit();
		return;
	}
	else if(key == SDL_SCANCODE_F)
	{
		_game.toggleFullScreen();
	}
	
	if(_currentItem < 0) _currentItem = 0;
	if(_currentItem > 2) _currentItem = 2;
	
	for(int i = 0; i < 3; ++i)
	{
		if(i == _currentItem)
		{
			_items[i]->setColor({0x00,0x00,0x00,0xff});
			_items[i]->setBackground({0xff,0xff,0xff,0xff});
		}
		else
		{
			_items[i]->setColor({0xff,0xff,0xff,0xff});
			_items[i]->setBackground({0xff,0xff,0xff,0x00});
		}
	}
	
}

