using System.Collections;
using System.Collections.Generic;
using ftt.Core.Managing;
using UnityEngine;

namespace UI
{
    public class UIManager : MonoBehaviour
    {
        // All UI panels of the game.
        public static UIManager S;

        [SerializeField] List<GameObject> uiPanels;

        bool isPaused = false;
        public bool IsPaused => isPaused;

        Animator anim;

        [SerializeField] GameObject chaseFilter;

        private void Awake()
        {
            S = this;
            EnablePanel("StartMenu");
        }

        private void Start()
        {
            anim = chaseFilter.GetComponent<Animator>();
        }

        private void Update()
        {
            if (GameManager.GetManager.CurrentState == GameStates.ENTRY) { return; }

            //handle if its in the start menu screen
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                PauseGame();
            }
        }

        public void PauseGame()
        {
            if (!isPaused)
            {
                Time.timeScale = 0f;

                isPaused = true;
                EnablePanel("PauseMenu");

                Cursor.lockState = CursorLockMode.None;
                Cursor.visible = true;
            }
            else
            {
                Time.timeScale = 1f;

                isPaused = false;

                UIManager.S.DisablePanels();

                Cursor.lockState = CursorLockMode.Locked;
                Cursor.visible = false;
            }
        }

        public void EnablePanel(string panelName)
        {
            foreach (GameObject panel in uiPanels)
            {
                panel.SetActive(panelName.Equals(panel.name));
            }
        }

        public void EnablePanels(params string[] panelNames)
        {
            foreach (string param in panelNames)
            {
                for (int i = 0; i != uiPanels.Count; i++)
                {
                    if (uiPanels[i].name.Equals(param))
                    {
                        uiPanels[i].SetActive(true);
                    }
                }
            }
        }

        public void DisablePanels()
        {
            foreach (GameObject panel in uiPanels)
            {
                panel.SetActive(false);
            }
        }

        public void SetSensitivity(int value)
        {
            GameManager.GetManager.Sensitivity = value;
        }

        public void ShowChaseEffect()
        {
            EnablePanel("ChaseFilter");

            anim.Play("OpenLightFilter");
        }

        public IEnumerator StopShowingChaseEffect()
        {
            anim.Play("CloseLightPanel");

            yield return new WaitForSeconds(1);
        }

        public void ShowDarkEffect()
        {
            EnablePanel("DarkFilter");
        }

        public void StopShowingDarkEffect()
        {
            DisablePanels();
        }

        public void ShowCrosshair()
        {
            EnablePanel("Crosshair");
        }

        void QuitGame()
        {
            Application.Quit();
        }
    }
}
