#include "HUDScene.h"
#include "constants.h"
USING_NS_CC;

const Vec2 menuBGPos = Vec2(400,350);
const Vec2 pauseButtonPos = Vec2(650,650);
const Vec2 resumeButtonPos = Vec2(400,500);
const Vec2 soundButtonPos = Vec2(400,400);
const Vec2 exitButtonPos = Vec2(400,300);
const Vec2 topLabelPos = Vec2(400,650);
const Vec2 healthBarPos[2] = { Vec2(150,670),Vec2(450,670) };
const Vec2 actionPStartPos[2] = { Vec2(100,625), Vec2(400,625) };
const Vec2 actionStepPos = Vec2(60,0);


Scene* HUDScene::scene()
{
    // 'scene' is an autorelease object
    Scene *scene = Scene::create();
    
    // 'layer' is an autorelease object
	HUDScene *layer = HUDScene::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 HUDScene::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::sharedDirector()->getVisibleSize();
    Point origin = Director::sharedDirector()->getVisibleOrigin();

	_menuBG = Sprite::create("HUD\\menuBG.jpg");
	_menuBG->setPosition(menuBGPos);
	_menuBG->setVisible(false);
	addChild(_menuBG,4);

	_pauseButton = MenuItemImage::create("HUD\\pause.jpg","HUD\\pause.jpg",this,menu_selector(HUDScene::pauseButtonPressed));
	_pauseButton->setPosition(pauseButtonPos);

	_resumeButton = MenuItemImage::create("HUD\\resume.jpg","HUD\\resume.jpg",this,menu_selector(HUDScene::resumeButtonPressed));
	_resumeButton->setPosition(resumeButtonPos);
	_resumeButton->setVisible(false);

	_exitButton = MenuItemImage::create("HUD\\exit.jpg","HUD\\exit.jpg");
	_exitButton->setPosition(exitButtonPos);
	_exitButton->setVisible(false);

	_soundState=true;
	_soundButton = MenuItemImage::create("HUD\\soundoff.jpg","HUD\\soundoff.jpg", this, menu_selector(HUDScene::soundButtonPressed));
	_soundButton->setPosition(soundButtonPos);
	_soundButton->setVisible(false);

	_menu = Menu::create(_pauseButton,_resumeButton,_exitButton, _soundButton,NULL);
	_menu->setPosition(Vec2(0,0));
	this->addChild(_menu,5);

	Sprite* bottom = Sprite::create("HUD\\toppanel.jpg");
	bottom->setPosition(topLabelPos);
	addChild(bottom,0);

	for (unsigned i=0;i<2;i++)
	{
		char str[100] = {0};
		sprintf(str,"HUD\\healthbar%d.png",i+1);
		Sprite* healthbar = Sprite::create(str);
		healthbar->setZOrder(2);
		healthbar->setPosition(healthBarPos[i]);
		addChild(healthbar);
		_healthbars.pushBack(healthbar);

		Sprite* health = Sprite::create("HUD\\health.jpg");
		health->setZOrder(1);
		health->setPosition(healthBarPos[i]);
		addChild(health);
		_healths.pushBack(health);

		Vec2 pos = actionPStartPos[i];
		
		Vector<Sprite* > vec;
		for (unsigned j=0;j<3;j++)
		{
			Sprite* act = Sprite::create("HUD\\AP.png");
			act->setPosition(pos);
			act->setZOrder(1);
			addChild(act);
			vec.pushBack(act);
			pos+= actionStepPos;
		}

		_actionPoints.push_back(vec);

		Sprite* _throw = Sprite::create("HUD\\throw.png");
		_throw->setPosition(pos);
		_throw->setZOrder(1);
		addChild(_throw);
		_throws.pushBack(_throw);

		vec.clear();
	}

    return true;
}

void HUDScene::endButtonRef(Layer* layer,cocos2d::SEL_MenuHandler sel)
{
	_exitButton->setTarget(layer,sel);
}

void HUDScene::showGoodEnd(int score)
{
	showEnd(true,score);
}

void HUDScene::showBadEnd(int score)
{
	showEnd(false,score);
}

void HUDScene::showEnd(bool good, int score)
{
	_pauseButton->setVisible(false);

	g_pause=true;
	_menuBG->setVisible(true);

	LabelTTF* endGameLabel = LabelTTF::create("",fontname,nameFontSize);
	endGameLabel->setPosition(menuBGPos);
	addChild(endGameLabel,6);
	
	std::ostringstream s;
	if (good)
	{
		s <<"Finally, the colonyst ship found its way through\n";
		s <<"space tunnel and appeared in another galaxy.\n";
		s <<"Soon humanity will find new home and new life.\n";
		s <<"New era is comming!\n";
	}
	else
	{
		s<<"The time has come and supermassive black hole\n";
		s<<"swallowed galaxy and humanity with it.\n";
		s<<"Sad, but all your work was ineffective and senseless\n";
		s<<"And no one left to evaluate it\n";
	}
	s<<"Score: "<<score;
	endGameLabel->setString(s.str());
}

void HUDScene::resumeButtonPressed(Ref* pSender)
{
	showPauseMenu(false);
}

void HUDScene::pauseButtonPressed(Ref* pSender)
{
	showPauseMenu(true);
}

void HUDScene::showPauseMenu(bool vis)
{
	g_pause = vis;
	_menuBG->setVisible(vis);
	_exitButton->setVisible(vis);
	_resumeButton->setVisible(vis);
	_soundButton->setVisible(vis);
	_pauseButton->setVisible(!vis);
}

void HUDScene::onExit()
{
	//_detailedView->release();

	stopAllActions();
	removeAllChildren();

	Layer::onExit();
}

void HUDScene::soundButtonPressed(Ref* pSender)
{
	_soundState = !_soundState;

	Sprite* sprite;
	Sprite* spriteSel;
	
	if (_soundState)
	{
		soundMaster.play();
		sprite = Sprite::create("HUD\\soundoff.jpg");
		spriteSel = Sprite::create("HUD\\soundoff.jpg");
	}
	else
	{
		soundMaster.stop();
		sprite = Sprite::create("HUD\\soundon.jpg");
		spriteSel = Sprite::create("HUD\\soundon.jpg");
		
	}

	_soundButton->setNormalImage(sprite);
	_soundButton->setSelectedImage(sprite);
}

void HUDScene::showAP(int player,int number)
{
	unsigned i=0;
	for (;i<number;i++)
	{
		_actionPoints[player].at(i)->setVisible(true);
	}

	for (;i<3;i++)
	{
		_actionPoints[player].at(i)->setVisible(false);
	}
}

void HUDScene::showHealth(int player, float ratio)
{
	Vec2 size = _healths.at(player)->getSpriteFrame()->getOriginalSize();
	Vec2 pos = Vec2(healthBarPos[player].x - size.x*(1-ratio)/2,_healths.at(player)->getPosition().y);
	_healths.at(player)->setPosition(pos);
	_healths.at(player)->setScaleX(ratio);
}

void HUDScene::showPlayer(int player, bool vis)
{
	_healthbars.at(player)->setVisible(vis);
	_healths.at(player)->setVisible(vis);
	_throws.at(player)->setVisible(vis);
	for (unsigned j=0;j<3;j++)
	{
		_actionPoints[player].at(j)->setVisible(false);
	}
}

void HUDScene::showThrow(int player, bool vis)
{
	_throws.at(player)->setVisible(vis);
}