using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using UnityStandardAssets.Characters.FirstPerson;

public class PauseMenu : MonoBehaviour {
    public static PauseMenu instance;
    private GameObject pausePanel, gameOverPanel, victoryText, lossText;
    private float defaultTime;
    [SerializeField]
    private string currentScene = "dark";

    private bool _paused = false;
    private bool paused
    {
        get { return _paused; }
        set
        {
            _paused = value;
            Time.timeScale = _paused ? 0 : defaultTime;
            Player.instance.GetComponent<FirstPersonController>().enabled = !_paused;
			Cursor.lockState = !_paused ? CursorLockMode.Locked : CursorLockMode.None;
			Cursor.visible = _paused;
        }
    }

    private Text coinVal, slainVal, timeVal;
    private bool gameOver = false;

    private void Awake()
    {
        instance = this;
        defaultTime = Time.timeScale;
        pausePanel = transform.FindChild("Pause").gameObject;
        gameOverPanel = transform.FindChild("Over").gameObject;
        victoryText = gameOverPanel.transform.FindChild("Win").gameObject;
        lossText = gameOverPanel.transform.FindChild("Lose").gameObject;
        timeVal = gameOverPanel.transform.FindChild("Time").GetComponent<Text>();
        coinVal = gameOverPanel.transform.FindChild("Coins").GetComponent<Text>();
        slainVal = gameOverPanel.transform.FindChild("Slain").GetComponent<Text>();
    }

    void LateUpdate ()
    {
        if (Input.GetKeyDown(KeyCode.Escape) && !gameOver)
        {
            paused = !paused;
            pausePanel.SetActive(paused);
        }
	}
    public void Restart()
    {
		Gorgon.all = new List<Gorgon>();
        SceneManager.LoadScene(currentScene);
    }
    public void Resume()
    {
        paused = false;
    }
    public void MainMenu()
    {
        Time.timeScale = defaultTime;
        SceneManager.LoadScene("Main Menu");
    }
    public void Quit()
    {
        Application.Quit();
    }

    public void GameOver(bool won)
    {
        victoryText.SetActive(won);
        lossText.SetActive(!won);

        var player = Player.instance;
        timeVal.text = player.time.ToString();
        coinVal.text = player.coins.ToString();
        slainVal.text = player.killCount.ToString();
        gameOver = true;
        paused = true;
        gameOverPanel.SetActive(true);
    }
}
