/*
   core/cvar.h
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#ifndef __INCLUDED_CORE_CVAR_H__
#define __INCLUDED_CORE_CVAR_H__

#include <string>
#include <map>

namespace core
{

/// a variable encapsulation class
class Cvar
{
public:
	/// Cvar flags
	/**
	 * Archive	a cvar with this flag will be saved to the configuration file
	 * ReadOnly	the value of cvar with this flag can not be altered from the commandline
	 * Game		a cvar with this flag is only valid when a game is loaded
	 * Info		a cvar that updates player info
	 */
	enum Flags {Archive=1, ReadOnly=2, Game=4, Info=8};
	
	/// create a new variable
	Cvar(const char *name, unsigned int flags = 0);

/*----- inspectors ------------------------------------------------ */

	/// returns the name of the variable
	inline std::string const &name() { return cvar_name; }

	/// returns the info of the variable
	inline std::string const &info() { return cvar_info; }

	/// returns the flags of the variable
	inline unsigned int flags() const { return cvar_flags; }

	/// returns the float value of the variable
	inline float value() const { return cvar_value; }

	/// returns the string value of the variable
	inline const std::string &str() const { return cvar_str; }

/*----- mutators -------------------------------------------------- */

	/// set the info string
	void set_info(const char *);

	/// char * assignment operator 
	Cvar &operator=(const char *other);

	/// std::string assignment operator
	Cvar &operator=(const std::string &other);

	/// float assignment operator
	Cvar &operator=(float other);

/* ---- Static functions for the Cvar registry -------------------- */

	/// type definition for the Cvar registry
	typedef std::map<std::string, Cvar*> Registry;
	
	/// get a cvar value from the registry
	/** If the a cvar with the given name already exists in the registry,
 	*  its value will not be changed. If the cvar does not exist,
 	*  it will be created
 	*/
	static Cvar *get(const char *name, const char *value, unsigned int flags=0);

	/// get a cvar value from the registry
	/** If the a cvar with the given name already exists in the registry,
 	*  its value will not be changed. If the cvar does not exist,
 	*  it will be created
 	*/
	static Cvar *get(const char *name, float value, unsigned int flags=0);

	/// set a cvar value
	/** If the a cvar with the given name already exists in the registry,
 	* its value will be replaced
 	*/
	static Cvar *set(const char *name, const char *value, unsigned int flags=0);

	/// set a cvar value
	/** If the a cvar with the given name already exists in the registry,
 	* its value will be replaced
 	*/
	static Cvar *set(const char *name, float value, unsigned int flags=0);

	/// delete a cvar from the registry
	static void unset(const char *name);

	/// delete a cvar from the registry
	static void unset(std::string const &name);

	/// search for a named cvar, returns 0 if not found
	static Cvar *find(std::string const &name);

	/// search for a named cvar, returns 0 if not found
	static Cvar *find(const char *name);

	/// list the cvar registry
	static void list();

	/// the Cvar registry
	static inline Registry & registry() { return cvar_registry; }

	static Cvar		*sv_dedicated;	// dedicated server
	static Cvar		*sv_private;	// client with private server
	static Cvar		*sv_framerate;	// server framerate
	static Cvar		*sv_name;	// server name

	static Cvar		*con_ansi;	// console ANSI colors

	static Cvar		*cl_prediction;	// client prediction

	static Cvar		*net_host;	// network server ip (default binds to all interfaces)
	static Cvar		*net_port;	// network port
	static Cvar		*net_maxclients;// maximum number of connected clients
	static Cvar		*net_timeout;	// network timeout in seconds
	static Cvar		*net_framerate;	// client network send framerate

	static Cvar		*rconpassword;	// rcon password

private:
	std::string		cvar_name;
	std::string		cvar_info;
	std::string 		cvar_str;
	unsigned int 		cvar_flags;
	float 			cvar_value;

	static Registry		cvar_registry;

};

}

#endif // __INCLUDED_CORE_CVAR_H__