/*
 *  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 Library 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.
 */
 
#ifndef _GAMESTATE_H

#include <iostream>
#include "settings.h"
#include "globals.h"

using namespace std;

#define MAX_PLAYERS 6

enum STATES
{
	STATE_INIT=0,
	STATE_LOGO=1,
	STATE_INITIALMENU=2,
	STATE_PLAYING=3,
	STATE_STAGING=4,
	STATE_ENDRACE=5,
	
	STATE_EXIT=100
};

enum MODES
{
	MODE_FREEDRIVE=0,
	MODE_TIMETRIAL=1,
	MODE_NETMULTIFREE=2,
	MODE_SINGLERACE=3
};

class PLAYERINFO
{
public:		
	string carname;
	int carpaint;
	bool abs;
	bool tcs;
};

struct RACERESULT
{
	string carname;
	double totaltime;
	double bestlap;
	int numlaps;
	bool done;

public:
	RACERESULT(string name, double time, double best, int laps, bool finished)
	{
		carname = name;
		totaltime = time;
		bestlap = best;
		numlaps = laps;
		done = finished;
	};
};

struct SORTRESULT
{
	bool operator() (const RACERESULT& left, const RACERESULT& right)
	{
		if( !left.done && right.done ) return false;
		return ( left.totaltime < right.totaltime );
	}
};

class GAMESTATE
{
private:
	STATES state;
	MODES mode;
	PLAYERINFO players[MAX_PLAYERS];
	string trackname;
	int number_of_laps;
	std::vector<RACERESULT> raceresult;
/*
	string treedetail;
	string terraindetail;
*/	
public:
	GAMESTATE();
	void UpdateSettings();
	STATES GetGameState();
	void SetGameState(STATES newstate);
	void SetGameMode(MODES newmode) {mode = newmode; settings.Set( "game.mode", newmode ); }
	MODES GetGameMode() {return mode;}
	
	void SetCarName(int p, string cn);
	void SetCarPaint(int p, int cp);
	void SetCarABS(int p, bool newabs);
	void SetCarTCS(int p, bool newtcs);
	void SetTrackName(string tn) {trackname = tn; settings.Set( "game.track", tn); }
	void SetLaps(int laps) { number_of_laps = laps; }
	void SetRaceResult(int p, double time, double best, int laps, bool done)
	{
		raceresult.push_back(RACERESULT(players[p].carname, time, best, laps, done));
	};
	
	string GetTrackName() {return trackname;}
	string GetCarName(int p) {return players[p].carname;}
	int GetCarPaint(int p) {return players[p].carpaint;}
	bool GetCarABS(int p) {return players[p].abs;}
	bool GetCarTCS(int p) {return players[p].tcs;}
	int GetLaps() {return number_of_laps;}
	std::vector<RACERESULT> GetRaceResult() { return raceresult; };

	void ClearRaceResult() { raceresult.clear(); };
	void SortRaceResult() { std::sort(raceresult.begin(), raceresult.end(), SORTRESULT()); };
};

#define _GAMESTATE_H
#endif /* _GAMESTATE_H */
