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

public class GameManager : MonoBehaviour {

    public static bool win = false;
    public static bool lose = false;
    public List<Button> buttons;
    public List<CharacterOne> enemies;
    public int aliveCheck;
    public int buttonCheck;

    public Button winMessage;
    public Button loseMessage;
    public Button restart;
    
    void Awake()
    {
        
    }
	// Use this for initialization
	void Start () {
        aliveCheck = enemies.Count;
        buttonCheck = buttons.Count;

        StartCoroutine(checkState());
	}
	
	// Update is called once per frame
	void Update () {
		
        //if(!lose)
        //{
           
        //    loseMessage.gameObject.SetActive(false);
        //    restart.gameObject.SetActive(false);
            
        //}

        if(win)
        {
            winMessage.gameObject.SetActive(true);
            Debug.Log("YOU WIN!");
            //HOORAY
        }
        if (lose)
        {
            restart.gameObject.SetActive(true);
            loseMessage.gameObject.SetActive(true);
            //AWW
        }

	}

    IEnumerator checkState()
    {
        while (true)
        {
            for (int i = 0; i < enemies.Count; i++)
            {
                if (!enemies[i].alive)
                {
                    aliveCheck--;
                }
            }
            if (aliveCheck == 0)
            {
                win = true;
            }
            else
                aliveCheck = enemies.Count;

            for(int i = 0; i < buttons.Count; i++)
            {
                if(buttons[i].interactable == false)
                {
                    buttonCheck--;
                }
            }
            if (buttonCheck == 0)
            {
                lose = true;
                Debug.Log("MY ISLAND NOW, IDIOT");
            }
            else
                buttonCheck = buttons.Count;


            yield return new WaitForSeconds(2f);
        }
    }

    public void Restart()
    {
        lose = false;
        loseMessage.gameObject.SetActive(false);
        restart.gameObject.SetActive(false);
        
        SceneManager.LoadScene(1);
    }

}
