#include "FirstScene.h"
#include "constants.h"
#include "ScoreScene.h"
#include "GameScene.h" 

USING_NS_CC;
using namespace cocos2d::ui;

const Vec2 textPos = Vec2(400,500);
const Vec2 namePos = Vec2(400,400);
const Vec2 tutorialPos = Vec2(400,325);
const Vec2 startPos = Vec2(400,250);
const Vec2 scorePos = Vec2(400,175);
const Vec2 exitPos = Vec2(400,100);

Scene* First::scene()
{
	srand (time(NULL));
    // 'scene' is an autorelease object
    Scene *scene = Scene::create();
    
    // 'layer' is an autorelease object
    First *layer = First::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool First::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::sharedDirector()->getVisibleSize();
    Point origin = Director::sharedDirector()->getVisibleOrigin();
    
	MenuItemImage* closeButton = MenuItemImage::create("HUD\\exit.jpg","HUD\\exit.jpg",this,menu_selector(First::menuCloseCallback));
	closeButton->setPosition(exitPos);

	MenuItemImage* startButton = MenuItemImage::create("HUD\\start.jpg","HUD\\start.jpg",this,menu_selector(First::menuStartCallback));
	startButton->setPosition(startPos);

	MenuItemImage* scoreButton = MenuItemImage::create("HUD\\score.jpg","HUD\\score.jpg",this,menu_selector(First::menuScoreCallback));
	scoreButton->setPosition(scorePos);

	Menu* pMenu = Menu::create(closeButton,startButton, scoreButton, NULL);
	pMenu->setPosition(Vec2(0,0));
    this->addChild(pMenu, 1);

	Sprite* pSprite = Sprite::create("HUD\\bg.jpg");
	pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
	this->addChild(pSprite, 0);

	LabelTTF* textLabel = LabelTTF::create(
		"Enter your name, forgotten king:",
		fontname, nameFontSize);
	textLabel->setColor(Color3B::YELLOW);
	textLabel->setPosition(textPos);

    this->addChild(textLabel, 1);

	//LabelTTF* pLabel = LabelTTF::create("Warning: generating world may cause small lag", "Arial", 32.0f);
    
    //pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
                        //  origin.y + pLabel->getContentSize().height));
	//pLabel->setColor(Color3B::YELLOW);
    //this->addChild(pLabel, 1);
    
	auto touchListener = EventListenerTouchOneByOne::create();

	touchListener->onTouchBegan = CC_CALLBACK_2(First::onTouchBegan, this);
	touchListener->onTouchEnded = CC_CALLBACK_2(First::onTouchEnded, this);
	touchListener->onTouchMoved = CC_CALLBACK_2(First::onTouchMoved, this);
	touchListener->onTouchCancelled = CC_CALLBACK_2(First::onTouchCancelled, this);

	_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
	
	_nameTextField = TextFieldTTF::textFieldWithPlaceHolder("Player",Size(300,60),TextHAlignment::CENTER,fontname,nameFontSize);
	_nameTextField->setDelegate(this);
	_nameTextField->setPosition(namePos);
	_nameTextField->setString("Player");
	_nameTextField->setColor(Color3B::YELLOW);
	addChild(_nameTextField,1);


	_players = CheckBox::create("HUD\\check_box_normal.png",
                                 "HUD\\check_box_normal_press.png",
                                 "HUD\\check_box_active.png",
                                 "HUD\\check_box_normal_disable.png",
                                 "HUD\\check_box_active_disable.png");

	_players->addTouchEventListener([&](Ref* sender, Widget::TouchEventType type){
        switch (type)
        {
                case ui::Widget::TouchEventType::BEGAN:
                        break;
                case ui::Widget::TouchEventType::ENDED:
                        break;
                default:
                        break;
        }
	});
	_players->setPosition(Vec2(250,450));
	_players->setZOrder(1);
	this->addChild(_players);
	 _endless = CheckBox::create("HUD\\check_box_normal.png",
                                 "HUD\\check_box_normal_press.png",
                                 "HUD\\check_box_active.png",
                                 "HUD\\check_box_normal_disable.png",
                                 "HUD\\check_box_active_disable.png");

	_endless->addTouchEventListener([&](Ref* sender, Widget::TouchEventType type){
        switch (type)
        {
                case ui::Widget::TouchEventType::BEGAN:
                        break;
                case ui::Widget::TouchEventType::ENDED:
                        break;
                default:
                        break;
        }
	});
	_endless->setPosition(Vec2(400,450));
	_endless->setZOrder(1);
	this->addChild(_endless);
	_friendly = CheckBox::create("HUD\\check_box_normal.png",
                                 "HUD\\check_box_normal_press.png",
                                 "HUD\\check_box_active.png",
                                 "HUD\\check_box_normal_disable.png",
                                 "HUD\\check_box_active_disable.png");

	_friendly->addTouchEventListener([&](Ref* sender, Widget::TouchEventType type){
        switch (type)
        {
                case ui::Widget::TouchEventType::BEGAN:
                        break;
				case ui::Widget::TouchEventType::ENDED:
                        break;
                default:
                        break;
        }
	});
	_friendly->setPosition(Vec2(650,450));
	_friendly->setZOrder(1);
	this->addChild(_friendly);
    return true;
}


void First::menuCloseCallback(Ref* pSender)
{
    Director::sharedDirector()->end();
}

void First::menuStartCallback(Ref* pSender)
{
	g_playerName = _nameTextField->getString();

	int players= 1;
	if (_players->isSelected())
		players++;

	Scene* pScene = GameScene::scene(players,_endless->isSelected(),_friendly->isSelected());
	Director::sharedDirector()->pushScene(pScene);
}

void First::menuScoreCallback(Ref* pSender)
{
	g_playerName = _nameTextField->getString();
	Scene* pScene = ScoreScene::sceneWithScore();
	Director::sharedDirector()->pushScene(pScene);
}

bool First::onTextFieldAttachWithIME(TextFieldTTF * pSender)
{
    return false;
}

bool First::onTextFieldDetachWithIME(TextFieldTTF * pSender)
{
    return false;
}

bool First::onTextFieldInsertText(TextFieldTTF * pSender, const char * text, int nLen)
{
    return false;
}

bool First::onTextFieldDeleteBackward(TextFieldTTF * pSender, const char * delText, int nLen)
{
    return false;
}

bool First::onDraw(TextFieldTTF * pSender)
{
    return false;
}

bool First::onTouchBegan(Touch *pTouch, Event *pEvent)
{
    return true;
}

void First::onTouchEnded(Touch *pTouch, Event *pEvent)
{
	_nameTextField->attachWithIME();
}
