#include "SelectMenuLayer.h"
#include "GameManager.h"
#include "MainMenuLayer.h"


USING_NS_CC;
using namespace ui;

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


bool SelectMenuLayer::init(){
	if (!BaseMenuLayer::init()){
		return false;
	}

	std::vector> functions;
	cocos2d::Vector buttons;

	initFunctions(functions);
	//insertamos los botones a la escena
	initButtons(buttons, functions);

	//habilitamos todos los niveles que el jugador puede realizar actualmente
	for (int i = 0; i < NUM_LEVELS; i++){
		if (i>GameManager::getInstance()->getNextLevel()) break;
		//habilitamos el boton
		buttons.at(i)->setEnabled(true);
		//cambiamos la imagen a habilitado
		buttons.at(i)->setBright(true);
	}

	//boton de ir hacia atras
	auto backBt = Button::create("back0", "back1", "back1", Widget::TextureResType::PLIST);
	backBt->setAnchorPoint(Point(0, 0.5));
	backBt->addClickEventListener(CC_CALLBACK_0(SelectMenuLayer::actionButtonBack, this));
	backBt->setPosition(Point(42.5* getScaleX(), 50 * getScaleY()));
	addChild(backBt);

	return true;
}

void SelectMenuLayer::initButtons(cocos2d::Vector& buttons, std::vector>& functions){
	int tag = 1;
	int sizeY = 0;
	int sizeX = 0;
	int marginX = 42.5* getScaleX();
	int marginY = _visibleSize.height - 100 * getScaleY();
	//queremos un aspecto de matriz, usamos para ello dos bucles
	for (int i = 0; i < NUM_LEVELS / 3; i++){
		for (int j = 0; j < NUM_LEVELS / 3; j++){
			auto bt = Button::create("level_unlock0", "level_unlock1", "level_lock", Widget::TextureResType::PLIST);
			//inicialmente los niveles estan bloqueados por lo que los botones estaran deshabilitados
			bt->setEnabled(false);
			bt->setBright(false);
			//ponemos funcionalidad y nombre a cada boton 
			bt->addClickEventListener(functions.at(tag - 1));
			bt->setTag(tag); _ostr << tag; tag++;
			bt->setTitleText(_ostr.str()); _ostr.str("");
			//indicamos como de grande sera la fuente
			bt->setTitleFontSize(18);
			bt->setAnchorPoint(Point(0, 0.5));
			bt->setPosition(Point(marginX, marginY));
			//calculamos la posicion para el siguiente boton
			sizeX = bt->getBoundingBox().size.width;
			sizeY = bt->getBoundingBox().size.height;
			marginX += 42.5* getScaleX() + sizeX;
			buttons.pushBack(bt);
			addChild(bt);
		}
		marginX = 42.5* getScaleX();
		marginY -= (42.5 * getScaleY() + sizeY);
	}
}




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