using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LevelManager : MonoBehaviour {

    public float LevelTime;
    public float StartTime;
    float Timer;

    public string nextLevelName;


    public float WaitTime;
    float nextLVtimer;





    public GameObject winUI;
    public GameObject loseUI;


    public GameObject BossCat;




    List<string> animationTriggers;
    Animator m_Animator = null;

    List<GameObject> limbList = new List<GameObject>();
    // Use this for initialization
    void Start () {
        Timer += StartTime;

        var found = FindObjectsOfType(typeof(LimbBehaviour));
        foreach( var i in found ) {
            var ii = i  as LimbBehaviour;
            limbList.Add(ii.gameObject);
        }
        

        if (BossCat != null)
            m_Animator = BossCat.gameObject.GetComponent<Animator>();

    }

    // Update is called once per frame
    void Update () {
        Timer += Time.deltaTime;

        if (Timer >= LevelTime) {
            winUI.gameObject.SetActive(true);
            nextLVtimer += Time.deltaTime;
        }

        if (nextLVtimer >= WaitTime)
        {
            winGame();
        }

        if (m_Animator != null && animationTriggers != null)
            m_Animator.SetTrigger(animationTriggers[healthLeft()]);
    }



    public void loseGame() {
        loseUI.gameObject.SetActive(true);

    }

    public void winGame() {
        winUI.gameObject.SetActive(true);

        //if (nextLVtimer > WaitTime)
        //{
        //
        //    SceneManager.LoadScene(nextLevelName, LoadSceneMode.Single);
        //}
    }

    public float levelProgression() {

        return Timer / LevelTime;
    }

    public int healthLeft() {
        int healthCount = 0;
        foreach (GameObject limb in limbList) {
            healthCount += limb.GetComponent<LimbBehaviour>().currentHealth;

        }


        return healthCount;
    
    }

    public void Restart()
    {
        Debug.Log("restart button pressed");
        SceneManager.LoadScene("Level0");
    }

    public void MainMenu()
    {
        Debug.Log("mainmenu pressed");
        SceneManager.LoadScene("MainMenu");
    }

}
