#include "MainMenuLayer.h"
#include "Level1.h"
#include "SelectMenuLayer.h"
#include "OptionsLayer.h"
#include "ui/CocosGUI.h"
#include "GameManager.h"




USING_NS_CC;
using namespace ui;

Scene* MainMenuLayer::createScene()
{
	auto scene = Scene::create();
	auto layer = MainMenuLayer::create();
	scene->addChild(layer);
	return scene;
}


bool MainMenuLayer::init(){
	if (!BaseMenuLayer::init()){
		return false;
	}
	std::vector> functions;
	//inicializamos un vector de funciones. Cada funcion lanza un nivel
	initFunctions(functions);

	//lanza automaticamente el siguiente nivel a jugar
	auto startButton = Button::create("start0", "start1", "start1", Widget::TextureResType::PLIST);
	startButton->setAnchorPoint(Point(0.5, 1));
	startButton->setPosition(Vec2(_visibleSize.width*0.5, _visibleSize.height - (90 * getScaleY())));
	if (GameManager::getInstance()->getNextLevel() == NUM_LEVELS)
	{
		startButton->addClickEventListener(functions.at(NUM_LEVELS - 1));
	}
	else
	{
		startButton->addClickEventListener(functions.at(GameManager::getInstance()->getNextLevel()));
	}
	addChild(startButton);

	//lanza el menu de seleccion de nivel
	auto nextHeight = startButton->getPositionY() - startButton->getBoundingBox().size.height - (30 * getScaleY());
	auto selectButton = Button::create("select0", "select1", "select1", Widget::TextureResType::PLIST);
	selectButton->setAnchorPoint(Point(0.5, 1));
	selectButton->addClickEventListener(CC_CALLBACK_0(MainMenuLayer::selectMenuButton, this));
	selectButton->setPosition(Point(startButton->getPositionX(), nextHeight));
	addChild(selectButton);

	//lanza las opciones para ajustar volumen y otras configuraciones
	nextHeight = selectButton->getPositionY() - selectButton->getBoundingBox().size.height - (30 * getScaleY());
	auto optionsButton = Button::create("options0", "options1", "options1", Widget::TextureResType::PLIST);
	optionsButton->setAnchorPoint(Point(0.5, 1));
	optionsButton->addClickEventListener(CC_CALLBACK_0(MainMenuLayer::optionsButton, this));
	optionsButton->setPosition(Point(startButton->getPositionX(), nextHeight));
	addChild(optionsButton);

	return true;
}



void MainMenuLayer::selectMenuButton(){
	Director::getInstance()->replaceScene(TransitionFadeBL::create(1, SelectMenuLayer::createScene()));
}

void MainMenuLayer::optionsButton(){
	Director::getInstance()->replaceScene(TransitionFlipX::create(1, OptionsLayer::createScene()));
}



void MainMenuLayer::initFunctions(std::vector>& functions){
	functions.push_back(CC_CALLBACK_0(MainMenuLayer::actionButton1, this));
	functions.push_back(CC_CALLBACK_0(MainMenuLayer::actionButton2, this));
	functions.push_back(CC_CALLBACK_0(MainMenuLayer::actionButton3, this));
	functions.push_back(CC_CALLBACK_0(MainMenuLayer::actionButton4, this));
	functions.push_back(CC_CALLBACK_0(MainMenuLayer::actionButton5, this));
	functions.push_back(CC_CALLBACK_0(MainMenuLayer::actionButton6, this));
	functions.push_back(CC_CALLBACK_0(MainMenuLayer::actionButton7, this));
	functions.push_back(CC_CALLBACK_0(MainMenuLayer::actionButton8, this));
	functions.push_back(CC_CALLBACK_0(MainMenuLayer::actionButton9, this));
}

void MainMenuLayer::actionButton1(){ Director::getInstance()->replaceScene(TransitionSplitCols::create(1, Level1::createScene())); }
void MainMenuLayer::actionButton2(){  /*TODO*/ }
void MainMenuLayer::actionButton3(){  /*TODO*/ }
void MainMenuLayer::actionButton4(){  /*TODO*/ }
void MainMenuLayer::actionButton5(){  /*TODO*/ }
void MainMenuLayer::actionButton6(){  /*TODO*/ }
void MainMenuLayer::actionButton7(){  /*TODO*/ }
void MainMenuLayer::actionButton8(){  /*TODO*/ }
void MainMenuLayer::actionButton9(){  /*TODO*/ }