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

public class MenuController : MonoBehaviour
{
    public Canvas uiCanvas;

    private Canvas controlsCanvas;
    private Canvas assuranceCanvas;
    private Canvas pauseCanvas;
    private Canvas winCanvas;
    private Canvas loseCanvas;

    private Assurance assurance;

    private bool isPaused = false;
    private bool pauseLock = false;

    private void Start()
    {
        Canvas[] subCanvas = GetComponentsInChildren<Canvas>();
        Dictionary<string, Canvas> canvases = new Dictionary<string, Canvas>();
        foreach (Canvas c in subCanvas)
        {
            canvases.Add(c.name, c);
        }

        controlsCanvas = canvases["ControlsCanvas"];
        assuranceCanvas = canvases["AssuranceCanvas"];
        pauseCanvas = canvases["PauseCanvas"];
        winCanvas = canvases["WinCanvas"];
        loseCanvas = canvases["LoseCanvas"];

        
        if (controlsCanvas == null)
            Debug.LogError("Failed to find canvas component");
        if (assuranceCanvas == null)
            Debug.LogError("Failed to find canvas component");
        if (pauseCanvas == null)
            Debug.LogError("Failed to find canvas component");
        if (winCanvas == null)
            Debug.LogError("Failed to find canvas component");
        if (loseCanvas == null)
            Debug.LogError("Failed to find canvas component");

        assurance = assuranceCanvas.GetComponent<Assurance>();

        if (assurance == null)
            Debug.LogError("Failed to find assurance component");

        pauseCanvas.enabled = false;
        controlsCanvas.enabled = false;
        winCanvas.enabled = false;
        loseCanvas.enabled = false;
        assuranceCanvas.enabled = false;
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (isPaused)
                Resume();
            else
                Pause();
        }

       
    }

    public void Lose()
    {
        uiCanvas.enabled = false;
        loseCanvas.enabled = true;
        Time.timeScale = 0f;
        pauseLock = true;
    }

    public void Win()
    {
        uiCanvas.enabled = false;
        winCanvas.enabled = true;
        Time.timeScale = 0f;
        pauseLock = true;
    }

    public void CloseControls()
    {
        controlsCanvas.enabled = false;
    }

    public void Pause()
    {
        if (pauseLock)
            return;
        uiCanvas.enabled = false;
        Time.timeScale = 0f;
        isPaused = true;
        pauseCanvas.enabled = true;
        assurance.Reset();
    }

    public void Resume()
    {
        if (pauseLock)
            return;
        uiCanvas.enabled = true;
        Time.timeScale = 1f;
        isPaused = false;
        pauseCanvas.enabled = false;
        assurance.Reset();
    }

    public void Controls()
    {
        controlsCanvas.enabled = true;
    }

    public void HideControls()
    {
        controlsCanvas.enabled = false;
    }

    public void Replay()
    {
        Time.timeScale = 1f;
        //Reloads the scene
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    public void ForceMainMenu()
    {
        Time.timeScale = 1f;
        SceneManager.LoadScene("MainMenuNew");
    }

    public void MainMenu()
    {
        UnityEvent requestCallback = new UnityEvent();
        requestCallback.AddListener(MainMenuCallback);
        assurance.Request(requestCallback, "You are about to leave the game, are you sure?");
    }

    public void MainMenuCallback()
    {
        Time.timeScale = 1f;
        SceneManager.LoadScene("MainMenuNew");
    }
}
