/*
   core/reputation.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_REPUTATION_H__
#define __INCLUDED_CORE_REPUTATION_H__

#include <list>

#include "core/info.h"

namespace core {
	
class Reputation {

	class FactionRep {
	public:
		inline FactionRep(const std::string &label, const float reputation = 0)
		{
			factionrep_label.assign(label);
			factionrep_faction = 0;
			factionrep_reputation = reputation;
		}
		
		inline FactionRep(const Info *faction, const float reputation = 0)
		{
			factionrep_label.assign(faction->label());
			factionrep_faction = faction;
			factionrep_reputation = reputation;
		}
				
		inline ~FactionRep()
		{
		}
		
		inline const std::string & label() const
		{
			return factionrep_label;
		}
		
		inline const Info *faction() const
		{
			return factionrep_faction;
		}
		
		/**
		 * @brief reputation with the specified faction.
		 * Return value is between -100 (hostile) and 100 (friendly), 0 means neutral.
		 * */
		inline const float reputation() const
		{
			return factionrep_reputation;
		}
		
		inline void set_faction(const Info *faction)
		{
			factionrep_faction = faction;
		}
		
		inline void set_reputation(const float reputation)
		{
			factionrep_reputation = reputation;
			math::clamp(factionrep_reputation, -100.0f, 100.0f);
		}
				
	private:
		std::string	factionrep_label;
		const Info	*factionrep_faction;
		float		factionrep_reputation;
	};
	
public:
	typedef std::list<FactionRep *> FactionReps;
	
	Reputation();
	~Reputation();
	
	const float reputation(const Info *faction) const;
	
	const Info *find(const Info *faction) const;
		
	void set_faction(const std::string &label, const Info *action);
		
	void set_reputation(const std::string &label, const float reputation);
	
	void set_reputation(const Info *faction, const float reputation);
	
	void set_dirty(const bool dirty = true);
	
	void set_timestamp(const unsigned long timestamp);
	
	void clear();
	
	void assign(const Reputation &other);
	
	inline FactionReps::const_iterator factionreps_begin() const
	{
		return reputation_factionreps.begin();
	}
	
	inline FactionReps::const_iterator factionreps_end() const
	{
		return reputation_factionreps.end();
	}
	
	inline const FactionReps & factionreps() const
	{
		return reputation_factionreps;
	}
	
	inline FactionReps & factionreps()
	{
		return reputation_factionreps;
	}
	
	inline const bool dirty() const
	{
		return reputation_dirty;
	}
	
	inline const unsigned long timestamp() const
	{
		return reputation_timestamp;
	}
	
	/* ---- deserializers -------------------------------------- */
	
	/// receive a server-to-client update from a stream
	void receive_server_update(std::istream &is);
	
	/* ---- serializers ---------------------------------------- */
	
	/// serialize a server-to-client update on a stream
	void serialize_server_update(std::ostream & os) const;
	
private:
	FactionReps		reputation_factionreps;
	
	bool			reputation_dirty;
	
	unsigned long		reputation_timestamp;
};

} // namespace core
#endif // __INCLUDED_CORE_REPUTATION_H__