using UnityEngine;
using System.Collections;

public class LevelManager : MonoBehaviour
{

    public bool isGameRunning = false;

    public float currComboTime = 0f;
	public float comboTimeSpeed = 1f;
    public float givenComboTime = 10f;
    public bool comboTimer = false;

    public bool startGameTime = false;

    public bool hasSpawned = true;

	public float givenDelay = 1.5f;
	public float delaySpeed = 1.0f;
	private bool delay = false;
	private float delayTime = 3f;

    public bool audioHasPlayed = false;

	public int successfulStints = 0, failedStints = 0;

    public AudioManager audioManager;

	void Start()
	{
		delayTime = givenDelay;
		currComboTime = givenComboTime;
	}

	#region Message Event Listeners
    void OnEnable()
    {
        EventManager.StartListening(Constants.SpawnNewWorker, SpawnNewWorker);
        EventManager.StartListening(Constants.CombinationFailed, CombinationFailed);
        EventManager.StartListening(Constants.CombinationSuccessful, CombinationSuccessful);
        EventManager.StartListening(Constants.START_GAME, StartGameTime);
        EventManager.StartListening(Constants.GAME_OVER, GameOver);
    }

    void OnDisable()
    {
        EventManager.StopListening(Constants.CombinationFailed, CombinationFailed);
        EventManager.StopListening(Constants.CombinationSuccessful, CombinationSuccessful);
        EventManager.StopListening(Constants.SpawnNewWorker, SpawnNewWorker);
        EventManager.StopListening(Constants.START_GAME, StartGameTime);
        EventManager.StopListening(Constants.GAME_OVER, GameOver);
    }
	#endregion

    void Update()
    {
        if (isGameRunning)
        {
            if (comboTimer)
            {
				currComboTime -= Time.deltaTime * comboTimeSpeed;

                if (!audioHasPlayed) 
                {
                    EventManager.TriggerEvent(Constants.ComboTimerRunning);
                    audioHasPlayed = true;
                }

                if (currComboTime > 0)
                {
                    
                    EventManager.TriggerEvent(Constants.CheckForInput);
                }
				else
                {
                    audioHasPlayed = false;
                    EventManager.TriggerEvent(Constants.CombinationFailed);
                }
            }
        }
		if(delay)
		{
			delayTime -= Time.deltaTime * delaySpeed;
			if(delayTime <= 0)
			{
				SpawnNewWorker();
				delayTime = givenDelay;
			}
		}

    }
		
    void StartGameTime()
    {
        EventManager.TriggerEvent(Constants.SpawnNewWorker);
        isGameRunning = true;
        
    }

    void GameOver()
    {
        isGameRunning = false;
    }

    void SpawnNewWorker()
    {
		if(!delay)
			delay = true;
		else
		{
			currComboTime = givenComboTime;
			delay = false;
	        EventManager.TriggerEvent(Constants.GenerateCombo);
	        EventManager.TriggerEvent(Constants.UpdateCombo);
	        comboTimer = true;
		}
	}

    void CombinationFailed()
    {
        comboTimer = false;

		failedStints++;

		currComboTime = givenComboTime;
        EventManager.TriggerEvent(Constants.SpawnNewWorker);
    }

    void CombinationSuccessful()
    {
        comboTimer = false;

		successfulStints++;

		currComboTime = givenComboTime;
        EventManager.TriggerEvent(Constants.SpawnNewWorker);
    }

}
