using UnityStandardAssets.ImageEffects;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameController : MonoBehaviour {

    [SerializeField]private float monkEnergyIncrease;
    [SerializeField]private float monkTimeToIncreaseEnergy;
    [SerializeField]private float monkMaxEnergy;
    
    public ParticleSystem auraOne, auraTwo, auraThree;
    private int tier = 1;

    private float focusTimer;
    private float actualMonkEnergy;

    [SerializeField]private AudioSource monkAttackedSound;
    [SerializeField]private GameObject victoryAnimation;
    [SerializeField]private GameObject defeatAnimation;

	// Use this for initialization
	void Awake () {
        GameObject.FindGameObjectWithTag("SceneFader").GetComponent<SceneFader>().FadeScene(FadeState.FadeOut, null);
	}
	
	// Update is called once per frame
	void Update () {
        MonkMeditationTimer();
	}

    private void Defeat ()
    {
        GetComponent<EnemiesWaves>().enabled = false;
        enabled = false;
        DestroyAllEnemies();
        GameObject.FindGameObjectWithTag("Player").SetActive(false);
        defeatAnimation.SetActive(true);
    }

    private void CheckVictory ()
    {
        if (actualMonkEnergy >= monkMaxEnergy)
        {
            GetComponent<EnemiesWaves>().enabled = false;
            enabled = false;
            DestroyAllEnemies();
            GameObject.FindGameObjectWithTag("Player").SetActive(false);
            GameObject.FindGameObjectWithTag("BackgroundCamera").GetComponent<BlurOptimized>().enabled = false;
            victoryAnimation.SetActive(true);
        }
    }

    private void DestroyAllEnemies ()
    {
        GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");

        foreach (GameObject item in enemies)
        {
            item.GetComponent<EnemyLife>().DestroyEnemy();
        }
    }

    private void MonkMeditationTimer ()
    {
        focusTimer += Time.deltaTime;

        if (focusTimer >= monkTimeToIncreaseEnergy)
        {
            actualMonkEnergy += monkEnergyIncrease;
            focusTimer = 0f;
            CheckTier();
            CheckVictory();
        }
    }


    void CheckTier() {
        if (actualMonkEnergy < monkMaxEnergy / 3 && tier != 1) {
            tier = 1;
            auraOne.Play();            
            Invoke("DisableAuras", 0.5f);
        }
        else if (monkMaxEnergy / 3 <= actualMonkEnergy && actualMonkEnergy < 2 *monkMaxEnergy/3 && tier != 2) {
            tier = 2;
            auraTwo.Play();
            Invoke("DisableAuras", 0.5f);
        }
        else if (2*monkMaxEnergy/3 <= actualMonkEnergy && tier != 3) {
            tier = 3;
            auraThree.Play();            
            Invoke("DisableAuras", 0.5f);
        }
    }

    void DisableAuras() {
        switch (tier) {
            case 1:
                auraTwo.Stop();
                auraThree.Stop();
                break;
            case 2:
                auraOne.Stop();
                auraThree.Stop();
                break;
            default:
                auraOne.Stop();
                auraTwo.Stop();
                break;
        }
    }

    public void ReduceMonkEnergy (float amount)
    {
        monkAttackedSound.Play();
        actualMonkEnergy -= amount;
        CheckTier();

        if (actualMonkEnergy <= 0f)
        {
            Defeat();
        }
    }

    private void ChangeScene ()
    {
        SceneManager.LoadScene("Menu");
    }

    public void BacktoMenuClicked ()
    {
        GameObject.FindGameObjectWithTag("SceneFader").GetComponent<SceneFader>().FadeScene(FadeState.FadeIn, ChangeScene);
    }

    private void Restart ()
    {
        SceneManager.LoadScene("Game");
    }

    public void RestartClicked()
    {
        GameObject.FindGameObjectWithTag("SceneFader").GetComponent<SceneFader>().FadeScene(FadeState.FadeIn, Restart);
    }
}
