/*********************************************************************
*	Highscore
*	SOURCE FILE
*	Autor:	Michal Jirouš
*	Datum: 14.7.2008
*	Soubor: game_highscore.cpp
*	Popis: Databaze nejlepsich dosazenych vysledku dle nazvu levelu
**********************************************************************/

#include "game_highscore.h"
#include "sys_console.h"

#include "encryption.h"
#include <map>
#include <sstream>
#include "mathematic.h"

using namespace highscore;
using namespace systemConsole;
using namespace encryption;
using namespace std;

map<string,HighScoreInfo> highScoreMap;

string g_sDatabaseFile;

void save( const char *filename );
#include "level_loader.h"
//nacteni databaze ze souboru
bool highscore::loadDatabase( const char * filename )
{
	g_sDatabaseFile = filename;
	console << "Loading highscore database..." << endline;
	string sHighScore = decryptFile( filename );	//desifrovani souboru

	if( sHighScore.empty() )
	{
		console << "Error: Loading highscore database, Reason: No data for input." << endline;
		return false;
	}

	istringstream bufin;
	bufin.str( sHighScore );

	HighScoreInfo newInfo;
	string sLevelName;


	int sum = 0;
	size_t size = 0;
	//nacteni dat z retezce
	while( true )
	{
		bufin >> sLevelName;
	
		if( bufin.eof()  )
			break;
		bufin >> newInfo.m_sPlayer;
		bufin >> newInfo.m_uiTime;
		bufin >> newInfo.m_ifileSum;
		bufin >> newInfo.m_fileSize;

		if( !getFileSum( (MAPS_DIRECTORY + sLevelName).c_str(), sum, size ) )
			continue;
		if( sum != newInfo.m_ifileSum || size != newInfo.m_fileSize )
			continue;

		highScoreMap.insert( make_pair( sLevelName, newInfo ) );
	
	}

	console << "Highscore database successfuly loaded." << endline;
	save( g_sDatabaseFile.c_str() );
	return true;
}



//nastavi hodnotu highscore dle nazvu levelu (levelName)
void highscore::setHighScore( string levelName, string playerName, Uint32 time )
{
	int sum = 0;
	size_t size = 0;
	if( !getFileSum( (MAPS_DIRECTORY + levelName).c_str(), sum, size ) )
		return;

	map<string,HighScoreInfo>::iterator iter = highScoreMap.find( levelName );
	if( iter == highScoreMap.end() )
	{
		HighScoreInfo newInfo;
		newInfo.m_sPlayer = playerName;
		newInfo.m_uiTime = time;
		newInfo.m_fileSize = size;
		newInfo.m_ifileSum = sum;
		highScoreMap.insert( make_pair( levelName, newInfo ) );
	}
	else
	{
		iter->second.m_sPlayer = playerName;
		iter->second.m_uiTime = time;
		iter->second.m_ifileSum = sum;
		iter->second.m_fileSize = size;
	}

	save( g_sDatabaseFile.c_str() );
}

void save( const char *filename )
{
	//ulozeni do retezce
	ostringstream buf;
	for( map<string,HighScoreInfo>::iterator iter = highScoreMap.begin(); iter != highScoreMap.end(); iter++ )
	{
		buf << iter->first << " " << iter->second.m_sPlayer << " " << iter->second.m_uiTime << " " << iter->second.m_ifileSum << " " << iter->second.m_fileSize << " ";
	}
	//zasifrovani a ulozeni do souboru
	encrypt( g_sDatabaseFile.c_str(), buf.str() );
	buf.clear();
}


//vraci objekt highscore
HighScoreInfo * highscore::getHighScore( string levelName )
{
	map<string,HighScoreInfo>::iterator iter = highScoreMap.find( levelName );
	if( iter == highScoreMap.end() )
		return NULL;
	return &iter->second;
}

