using UnityEngine;
using System.Collections;

public class SatisfactionRateHandler : MonoBehaviour {

    private bool startGameTime;

	public float maxSatisfaction = 100.0f, minSatisfaction = 0.0f;

    public float satisfactionRate = 100.0f;
    public float satisfactionSpeed = 1.0f;

	public float buffRate = 10;
	public float debuffRate = 10;

	// TODO: Add dificulty for more correct answers

	void Start()
	{
		buffRate = (0.1f * satisfactionRate) / 100; // 10% of satisfaction
		debuffRate = (0.05f * satisfactionRate) / 100; // 5% of satisfaction
	}

    void OnEnable()
    {

        EventManager.StartListening(Constants.START_GAME, StartGameTime);
		EventManager.StartListening(Constants.CombinationSuccessful, BuffSatisfactionRate);
		EventManager.StartListening(Constants.CombinationFailed, DebuffSatisfactionRate);
    }

    void OnDisable()
    {
        EventManager.StopListening(Constants.START_GAME, StartGameTime);
		EventManager.StopListening(Constants.CombinationSuccessful, BuffSatisfactionRate);
		EventManager.StopListening(Constants.CombinationFailed, DebuffSatisfactionRate);
	
    }
    // Update is called once per frame
    void Update()
    {
        if (startGameTime)
        {
            satisfactionRate -= satisfactionSpeed * Time.deltaTime;
            if (satisfactionRate <= 0)
            {
				satisfactionRate = 0;
                startGameTime = false;
                EventManager.TriggerEvent(Constants.GAME_OVER);
            }
        }
    }
	
	void BuffSatisfactionRate()
	{
		if(satisfactionRate < maxSatisfaction)
			satisfactionRate += buffRate;
	}

	void DebuffSatisfactionRate()
	{
		if(satisfactionRate > minSatisfaction)
			satisfactionRate -= debuffRate;
	}

    void StartGameTime()
    {
        satisfactionRate = 1;
        startGameTime = true;
    }
}
