//	HighScore.cpp - Simple class to handle the game's high scores.
//  ----------------------------------------------------------------------------
//	This file is part of 'GameTemplate', template code for a game.
//	Copyright (C) 2012  Niall Moody
//	
//	This program is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program.  If not, see <http://www.gnu.org/licenses/>.
//	----------------------------------------------------------------------------

#include "AppSettings.h"
#include "HighScore.h"
#include "UTF8File.h"

#include <sstream>
#include <string>

//------------------------------------------------------------------------------
HighScore::HighScore():
challengeHighScore(0),
challengeLastScore(0),
timeAttackHighScore(0),
timeAttackLastScore(0)
{
	UTF8File phil(FilePath::getAppDataPath().getChild(AppSettings::applicationName).getChild(L"HighScores.txt"));

	if(phil.exists())
	{
		std::wstring tempstr;
		std::wstringstream tempstr2;

		phil.readLine(tempstr);
		tempstr2.str(tempstr);
		tempstr2 >> challengeHighScore;

		phil.readLine(tempstr);
		tempstr2.clear();
		tempstr2.str(tempstr);
		tempstr2 >> challengeLastScore;

		phil.readLine(tempstr);
		tempstr2.clear();
		tempstr2.str(tempstr);
		tempstr2 >> timeAttackHighScore;

		phil.readLine(tempstr);
		tempstr2.clear();
		tempstr2.str(tempstr);
		tempstr2 >> timeAttackLastScore;
	}
}

//------------------------------------------------------------------------------
HighScore::~HighScore()
{
	std::wstringstream tempstr;
	UTF8File phil(FilePath::getAppDataPath().getChild(AppSettings::applicationName).getChild(L"HighScores.txt"));

	tempstr << challengeHighScore << std::endl;
	tempstr << challengeLastScore << std::endl;
	tempstr << timeAttackHighScore << std::endl;
	tempstr << timeAttackLastScore << std::endl;

	phil.writeText(tempstr.str());
}

//------------------------------------------------------------------------------
void HighScore::setChallengeScore(int val)
{
	challengeLastScore = val;
	if(val > challengeHighScore)
		challengeHighScore = val;
}

//------------------------------------------------------------------------------
void HighScore::setTimeAttackScore(int val)
{
	timeAttackLastScore = val;
	if(val > timeAttackHighScore)
		timeAttackHighScore = val;
}
