/*
autor: Lubos Fazekas
tim: Gridders
ucel: Implementacia triedy Experience
*/

#include "Common/Support/Debug.h"

#include "Experience.h"


Experience::Experience(string p_Name,float p_Limit,float p_TimeChange)
{
	m_Name = p_Name;
	m_Limit = p_Limit;
	m_TimeChange = p_TimeChange;
	m_EndSimTime = 0;
	m_Active = false;
	m_EstimatedSuccessProbability = 0.0f;
	m_LastDecrementSimTime = 0;
}

Experience::~Experience()
{
	;
}

float Experience::getLimit()
{
	return m_Limit;
}

void Experience::activate(int p_EndSimTime,float p_EstimatedSuccessProbability)
{
	m_Active = true;
	m_EndSimTime = p_EndSimTime;
	m_EstimatedSuccessProbability = p_EstimatedSuccessProbability;
	TXTN("STATISTIKY - aktivovana skusenost: %s, end_time: %i, estimatedProbability: %f",m_Name.c_str(),p_EndSimTime,p_EstimatedSuccessProbability);
}

void Experience::deactivate(int p_SimTime)
{
	if(m_Active)
	{
		TXTN("%i STATISTIKY - dali sme gol, takze iba deaktivujeme skusenost %s",p_SimTime,m_Name.c_str());
		m_Active = false;
	}
	else
	{
		;
	}
}

void Experience::changeByTimeStep(int p_SimTime)
{
	TXTN("%i Experience::changeByTimeStep - Vyhodnocujem skusenost %s ",p_SimTime,m_Name.data());

	// ak je skusenost aktivna (hrac prave nadobuda danu skusenost)
	if(m_Active){
		TXTN("%i Experience::changeByTimeStep - Skusenost je aktivna",p_SimTime);
		// otestujeme, ci uz nastal cas, kedy je potrebne skusenost vyhodnotit
		if(p_SimTime > m_EndSimTime)
		{
			TXTN("%i STATISTIKY Experience::changeByTimeStep - Skusenosti vyprsal casovy limit ! Skusenost sa deaktivuje a limit sa zmeni %s",p_SimTime,m_Name.c_str());
			m_Active = false;
			float firstLimit = m_EstimatedSuccessProbability + 0.05f;
			//float secondLimit = m_Limit + 0.05f;
			float secondLimit = m_Limit;
			m_Limit = (firstLimit > secondLimit ? firstLimit : secondLimit);
			TXTN("%i STATISTIKY - Experience::changeByTimeStep - Novy limit je %f",p_SimTime,m_Limit);
		}
		else
		{
			TXTN("%i Experience::changeByTimeStep - Skusenosti este nevyprsal casovy limit !",p_SimTime);
		}
	}
	else{
		TXTN("%i Experience::changeByTimeStep - Skusenost nie je aktiva",p_SimTime);
	}


	// zmena limitu (dekrementovanie alebo inkrementovanie (v kazdom simulacnom cykle))
	// ale iba neaktivnym skusenostiam
	if(!m_Active){
		TXTN("%i Experience::changeByTimeStep - Dekrementuje limit (po kroku)",p_SimTime);
		m_Limit += m_TimeChange * (p_SimTime - m_LastDecrementSimTime);
		//if(m_Limit > 1)
		//	m_Limit = 1;
		if(m_Limit < 0)
			m_Limit = 0;

		TXTN("%i Experience::changeByTimeStep - Novy limit je: %f",p_SimTime,m_Limit);
	}
	else
	{
		TXTN("%i Experience::changeByTimeStep - Aktivnym skusenostiam sa limit nedekrementuje",p_SimTime);
	}

	// zapametame si posledny simulacny cas, kedy doslo k dekrementacii limitu; aby sa limit nedekrementoval aj pocas
	// preruseni hry
	m_LastDecrementSimTime = p_SimTime;
}