//	GameCounter.cpp - Singleton used to count all sorts of things.
//	----------------------------------------------------------------------------
//	This file is part of 'GameTemplate', a game.
//	Copyright (c) 2008 Niall Moody.
//
//	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 3 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, see <http://www.gnu.org/licenses/>.
//	----------------------------------------------------------------------------

#ifdef DEBUG
#pragma warning(disable : 4786) //Stupid MS headers...
#endif

#include "GameCounter.h"

#include <algorithm>

using namespace std;

//------------------------------------------------------------------------------
GameCounter& GameCounter::getInstance()
{
	static GameCounter retval;

	return retval;
}

//------------------------------------------------------------------------------
void GameCounter::incrementCount(const string& counter, int val)
{
	int i;
	map<string, Counter *>::iterator it = counters.find(counter);

	if(it == counters.end())
		createCounter(counter);

	counters[counter]->value += val;
	for(i=0;i<counters[counter]->noteCounts.size();++i)
	{
		Counter *c = counters[counter];

		if((c->value % c->noteCounts[i]) == 0)
			c->noteDests[i]->countReached(counter, c->value);
	}
}

//------------------------------------------------------------------------------
int GameCounter::getCount(const string& counter) const
{
	int retval = 0;
	map<string, Counter *>::const_iterator it = counters.find(counter);

	if(it != counters.end())
		retval = it->second->value;

	return retval;
}

//------------------------------------------------------------------------------
int GameCounter::getScore() const
{
	int retval = 0;
	map<string, Counter *>::const_iterator it;

	for(it=counters.begin();it!=counters.end();++it)
	{
		if((it->first != "Fighter") && (it->first != "Time"))
			retval += it->second->value;
	}

	return retval;
}

//------------------------------------------------------------------------------
void GameCounter::reset()
{
	map<string, Counter *>::const_iterator it;

	for(it=counters.begin();it!=counters.end();++it)
		it->second->value = 0;
}

//------------------------------------------------------------------------------
void GameCounter::registerNotification(const string& counter,
									   int value,
									   CounterNotification *notification)
{
	map<string, Counter *>::iterator it = counters.find(counter);

	if(it == counters.end())
		createCounter(counter);

	counters[counter]->noteCounts.push_back(value);
	counters[counter]->noteDests.push_back(notification);
}

//------------------------------------------------------------------------------
void GameCounter::unregisterNotification(const std::string& counter,
										 int value,
										 CounterNotification *notification)
{
	map<string, Counter *>::iterator it = counters.find(counter);

	if(it != counters.end())
	{
		counters[counter]->noteCounts.erase(find(counters[counter]->noteCounts.begin(),
												 counters[counter]->noteCounts.end(),
												 value));
		counters[counter]->noteDests.erase(find(counters[counter]->noteDests.begin(),
												counters[counter]->noteDests.end(),
												notification));
	}
}

//------------------------------------------------------------------------------
GameCounter::GameCounter()
{
	
}

//------------------------------------------------------------------------------
GameCounter::~GameCounter()
{
	map<string, Counter *>::const_iterator it;

	for(it=counters.begin();it!=counters.end();++it)
		delete it->second;
}

//------------------------------------------------------------------------------
void GameCounter::createCounter(const string& name)
{
	Counter *counter = new Counter;

	counters.insert(make_pair(name, counter));
}
