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

public class GameManager : MonoBehaviour {

    public List<GameObject> healthUI;
    public PlayerControllerScript playerScript;
    public Button retry;
    [SerializeField]
    protected GameObject gameOver;
    [SerializeField]
    protected GameObject pauseMenu;

    enum GameState { Playing, Paused, GameOver};
    GameState currentState;
    GameState previousState;

    public GameObject heart1;
    public GameObject heart2;
    public GameObject heart3;

    bool soundCheck;
	// Use this for initialization
	void Start ()
    {
        currentState = GameState.Playing;
        heart1.SetActive(true);
        heart2.SetActive(true);
        heart3.SetActive(true);
    }
	
	// Update is called once per frame
	void Update ()
    {
        HealthUpdate();

        if (Input.GetButtonDown("Menu"))
        {
            if (currentState == GameState.Playing)
                PauseGame();
            else
                UnPauseGame();
        }

        if (playerScript.health <= 0)
        {
            if (!soundCheck)
            {
                FindObjectOfType<AudioManagerScript>().PlayClip("PlayerDeath");
                soundCheck = true;
            }
            for (int i = 0; i < healthUI.Count - 1; i++)
                healthUI[i].SetActive(false);

            StartCoroutine(QuitGame());
            ChangeState(GameState.GameOver);
            gameOver.SetActive(true);
            
        }
            
     
	}

    void HealthUpdate()
    {
        switch((int)playerScript.health)
        {
            case 0:
                break;
            case 1:
                heart1.SetActive(true);
                heart2.SetActive(false);
                heart3.SetActive(false);
                break;
            case 2:
                heart1.SetActive(true);
                heart2.SetActive(true);
                heart3.SetActive(false);
                break;
            case 3:
                break;
        }
    }

    void Cameralerp()
    {

    }

    IEnumerator Lerp(GameObject lerpObject, Vector3 finalPos, float timeTaken)
    {
        float currentTime = 0;

        while (currentTime < timeTaken)
        {
            currentTime += Time.deltaTime;
            lerpObject.transform.position =
                Vector3.Lerp(lerpObject.transform.position, finalPos, currentTime / timeTaken);
            yield return 0;
        }
    }

    void PauseGame()
    {
        Time.timeScale = 0;
        pauseMenu.SetActive(true);
        ChangeState(GameState.Paused);
    }
    void UnPauseGame()
    {
        Time.timeScale = 1;
        pauseMenu.SetActive(false);
        ChangeState(GameState.Playing);
    }

    void ChangeState(GameState newState)
    {
        previousState = currentState;
        currentState = newState;
    }

    public void LoadMainMenu()
    {
        SceneManager.LoadScene("MainMenu");
    }

    IEnumerator QuitGame()
    {
        print("cunt");
        yield return new WaitForSeconds(3);
        Application.Quit();
    }
}
