/***************************************************************************
 *   Copyright (C) 2004 by Christian Hubinger                              *
 *   e9806056@student.tuwien.ac.at                                         *
 *                                                                         *
 *   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 2 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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "kfhighscore.h"

#include <qstringlist.h>
#include <kdebug.h>

#include <kfconfiguration.h>

KFHighscore::KFHighscore( QObject* parent ) : KHighscore( parent ) {
}

KFHighscore::~KFHighscore() {}

bool KFHighscore::isHighscore( int points ) {
	kdDebug() << "bool KFHighscore::isHighscore( int points ) Points:" << points << endl;
	QString minPoints = readEntry( 10, skill() + "_scores", "-" );
	int score = 0;
	bool ok;
	if ( minPoints == "-" ) {
		return true;
	}
	
	score = minPoints.toInt( &ok ); 
	kdDebug() << "Worst Highscore: " << score << endl;
	kdDebug() << "your score: " << points << endl;
	if ( points > score ) {
		kdDebug() << "You are in the Highscores" << endl;
		return true;
	} else {
		return false;
	}
}

void KFHighscore::addToHighscore( const QString& user, int level, int points ) {
	QStringList scoresList = readList( skill() + "_scores", 10);
	int i = 1;
	int place = 0;
	bool found = false;
	if ( scoresList.count() == 0 ) {
		place = 1;
	} else {
		for ( QStringList::Iterator it = scoresList.begin(); it != scoresList.end() && !found; ++it ) {
			kdDebug() << "Found score :" << *it << endl;    
			QString strScore = *it;
			int score = 0;
			bool ok;
			score = strScore.toInt( &ok ); 
			if ( points > score ) {
				place = i;
				found = true;
			}
			i++;
		}
	}
	kdDebug()  << "You are at place: " << place << endl;
	if ( place != 0 ) {
		placeHighScore( place, user, level, points );
	} else {
		placeHighScore( scoresList.count() + 1, user, level, points );
	}
}

void KFHighscore::placeHighScore( int pos, const QString& user, int level, int points ) {
	QPtrList<QStringList> listMoved; 
	for ( int i = pos; i < 11; i++ ) {
		QStringList *moved = new QStringList();
		QString user = readEntry( i, skill()+ "_user", "-" );
		QString score = readEntry( i, skill() +"_scores", "-" );
		QString level = readEntry( i, skill() +"_level", "-" );
		moved->append( user );
		moved->append( level );
		moved->append( score );
		listMoved.append( moved );
	}
	
	int k = 0;
	for ( int j = pos + 1; j < 11; j++ ) {
		QStringList *moved = listMoved.at( k );
		writeEntry( j,  skill()+ "_user", *moved->at( 0  ) );
		writeEntry( j,  skill()+ "_level", *moved->at( 1  ) );
		writeEntry( j,  skill()+ "_scores", *moved->at( 2 ) );
		k++;
	}
	
	writeEntry( pos,  skill()+ "_scores", points );
	writeEntry( pos,  skill()+ "_user", user );
	writeEntry( pos,  skill()+ "_level", level );
}





QPtrList<QStringList>* KFHighscore::getScores() {
	QPtrList<QStringList>* list = new QPtrList<QStringList>;
	QStringList scoresList = readList(skill()+"_scores", 10);
	QStringList levelList = readList(skill() +"_level", 10);
	QStringList userList = readList(skill() + "_user", 10);
	int i = 0;
	for ( QStringList::Iterator it = scoresList.begin(); it != scoresList.end(); ++it ) {
		QString strScore = *it;
		QString strLevel = *levelList.at( i );
		QString strUser = *userList.at( i );
		QStringList* entry = new QStringList();
		kdDebug() << "Found User: " << strUser << " Level: " << strLevel << " Score: " << strScore << endl;    
		entry->append( strUser );
		entry->append( strLevel );
		entry->append( strScore );
		list->append( entry );
		i++;
	}
	return list;
}


QString KFHighscore::skill(){
	switch ( Config().m_GameLevel ) {
		case 0:
		return "0";
		break;
		case 1:
		return "1";
		break;
		case 2:
		return "2";
		break;
		case 3:
		return "3";
		break;
	}	
	return "-1";
}