#include "ScoreScene.h"
#include "constants.h"
#include <fstream>
USING_NS_CC;

const Vec2 labelPos = Vec2(400,450);
const float step = 50;
const Vec2 exitButtonPos = Vec2(400,150);

Scene* ScoreScene::sceneWithScore(int score)
{
	srand (time(NULL));
    // 'scene' is an autorelease object
    Scene *scene = Scene::create();
    
    // 'layer' is an autorelease object
    ScoreScene *layer = ScoreScene::create();
	layer->addScore(score);
    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

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

	Menu* pMenu = Menu::create(exitButton, 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);
    
    return true;
}

void ScoreScene::addScore(int score)
{
	std::vector<Score> scores;

	std::fstream fs;
	int count=0;
	fs.open("score.txt", std::fstream::in);
	if (fs.is_open())
	{
		fs >> count;
		char t;
		fs >> t;

		for (unsigned i=0;i<count;i++)
		{
			Score sc;
			fs >> sc.score;
			fs >> t;
			char c[101];
			fs.getline(c,100,'|');
			sc.name = c;
			scores.push_back(sc);
		}
	}
	fs.close();

	unsigned index;
	Score currentScore;
	unsigned firstLabels = std::min(count,5);
	if (score!=-1)
	{
		index=0;
		if (count>0)
		{
			while( index<count)
			{
				if (score>= scores[index].score)
				{
					break;
				}
				index++;
			}
		}

		currentScore = Score(score,g_playerName);
		scores.insert(scores.begin() + index,currentScore);
	
		count++;
		firstLabels = std::min(count,5);
		if (index>=5 && firstLabels==5)
			firstLabels = 4;
	}
	Vector<LabelTTF*> labels;
	Vec2 pos = labelPos;

	std::ostringstream s;
	for (unsigned i=0;i<firstLabels;i++)
	{
		Score sc = scores[i];
		s = std::ostringstream();
		s << i+1 <<". " << sc.name << "  "<< sc.score;
		LabelTTF* label = LabelTTF::create(s.str(),fontname, nameFontSize);
		label->setPosition(pos);
		pos.y-=step;

		labels.pushBack(label);
	}

	if (score!=-1)
	{
		if (firstLabels == 4 && count>=5)
		{
			s = std::ostringstream();
			s << index +1 <<". " << currentScore.name << "  "<< currentScore.score;
			LabelTTF* label = LabelTTF::create(s.str(),fontname, nameFontSize);
			label->setPosition(pos);
			pos.y-=step;
			label->setColor(Color3B::YELLOW);

			labels.pushBack(label);
			firstLabels++;
		}
		else
		{
			LabelTTF* label = labels.at(index);
			label->setColor(Color3B::YELLOW);
		}
	}

	for (unsigned i=0;i<firstLabels;i++)
	{
		addChild(labels.at(i),1);
	}

	if (score!=-1)
	{
		fs.open("score.txt", std::fstream::out);
		if (fs.is_open())
		{
			fs << count;
			fs << '|';

			for (unsigned i=0;i<count;i++)
			{
				Score sc = scores[i];
				fs << sc.score;
				fs << '|';
				fs << sc.name.c_str();
				fs << '|';
			}
		}
		fs.close();
	}
}


void ScoreScene::exitButtonPressed(Ref* pSender)
{
	Director::sharedDirector()->popScene();
}

