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

public enum UnlockedItems
{
    None,
    Trumpet,
    Tambourine,
    Clarinet,
    Cymbal,
}

public class StateController : MonoBehaviour
{

    // Singleton class
    public static StateController Instance = null;

    [Header("Respawning")]
    [SerializeField] GameObject player;
    public GameObject spawnPoint;
    [SerializeField] private GameObject deathCanvas;

    [Header("Pause Menu")]
    public bool isPaused = false;
    public GameObject pauseMenuCanvas;

    [Header("Debug")]
    public bool inDebugMode;
    public GameObject debugCanvas;

    [Header("Enemies")]
    GameObject[] enemiesInScene;

    [Header("Level Progression")]
    GameObject victoryCanvas;

    [Header("Unlocked Items")]
    public UnlockedItems itemProgression = UnlockedItems.None;

    void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        DontDestroyOnLoad(this.gameObject);
        SceneManager.sceneLoaded += OnSceneLoaded;
    }

    void Start() {
        if (this.inDebugMode)
        {
            debugCanvas = GameObject.Find("DebugCanvas");
            debugCanvas.SetActive(false);
        }
    }

    public bool HasTrumpet()
    {
        return this.itemProgression >= UnlockedItems.Trumpet;
    }


    public bool HasTambourine()
    {
        return this.itemProgression >= UnlockedItems.Tambourine;
    }


    public bool HasClarinet()
    {
        return this.itemProgression >= UnlockedItems.Clarinet;
    }

    public bool HasCymbal()
    {
        return this.itemProgression >= UnlockedItems.Cymbal;
    }

    void OnSceneLoaded(Scene scene, LoadSceneMode mode)
    {

        #region FIND OBJECTS 
        deathCanvas = GameObject.Find("DeathUICanvas");
        if (deathCanvas != null)
        {
            Button respawnButton = GameObject.Find("RespawnButton").GetComponent<Button>();
            if (respawnButton == null)
            {
                print("respawn button not found!");
            }
            respawnButton.onClick.AddListener(RespawnPlayer);
            deathCanvas.SetActive(false);
        }

        pauseMenuCanvas = GameObject.Find("PauseMenuCanvas");
        if (pauseMenuCanvas != null)
        {
            Button resumeButton = GameObject.Find("ResumeButton").GetComponent<Button>();
            resumeButton.onClick.AddListener(Unpause);
            TogglePauseMenu(false);
        }

        victoryCanvas = GameObject.Find("VictoryCanvas");
        if (victoryCanvas != null)
        {
            Button continueButton = GameObject.Find("ContinueButton").GetComponent<Button>();
            continueButton.onClick.AddListener(ContinueToNextLevel);
            victoryCanvas.SetActive(false);
        }

        // keep track of all enemies
        enemiesInScene = GameObject.FindGameObjectsWithTag("Enemy");
        // print(enemiesInScene);

        if (isPaused)
        {
            Unpause();
        }
        #endregion

        #region UNLOCK ITEMS
        switch (SceneManager.GetActiveScene().name)
        {
            case "GrenouilleVillage":
                this.itemProgression = UnlockedItems.None;
                break;
            case "Brasslands":
                this.itemProgression = UnlockedItems.Trumpet;
                break;
            case "GrappleScene":
                this.itemProgression = UnlockedItems.Tambourine;
                break;
            case "ClarinetScene":
                this.itemProgression = UnlockedItems.Clarinet;
                break;
            case "MushroomForest":
                this.itemProgression = UnlockedItems.Cymbal;
                break;
        };
        #endregion
    }

    void OnToggleDebugMenu()
    {
        if (inDebugMode)
        {
            debugCanvas.SetActive(!this.debugCanvas.activeSelf);
        }
    }

    void OnPause()
    {
        if (pauseMenuCanvas != null)
        {
            if (!isPaused)
            {
                Time.timeScale = 0;
                TogglePauseMenu(true);
            }
            else
            {
                Time.timeScale = 1;
                TogglePauseMenu(false);
            }
            isPaused = !isPaused;
        }
    }

    public void Unpause()
    {
        Time.timeScale = 1;
        TogglePauseMenu(false);
        isPaused = !isPaused;
    }

    void TogglePauseMenu(bool showPauseMenu)
    {
        pauseMenuCanvas.SetActive(showPauseMenu);
    }

    public void RespawnPlayer()
    {
        Destroy(GameObject.FindGameObjectWithTag("Player"));
        SetDeathCanvasActive(false);
        GameObject.Find("Main Camera").GetComponent<CameraMovement>().FindPlayer();
        RespawnEnemies();
        Instantiate(player, spawnPoint.transform.position, player.transform.rotation);
    }

    public void RespawnEnemies()
    {
        foreach (GameObject enemy in enemiesInScene)
        {
            enemy.SetActive(true);
        }
    }

    public void SetDeathCanvasActive(bool activeState)
    {
        deathCanvas.SetActive(activeState);
    }

    public void ShowVictoryCanvas()
    {
        victoryCanvas.SetActive(true);
    }

    public void ContinueToNextLevel()
    {
        SceneController.Instance.NextScene();
    }
}
