using UnityEngine;

using ftt.Core.Managing.GameEvents;
using ftt.Core.Managing.LevelLoading;
using ftt.Core.Sounds;
using UI;

namespace ftt.Core.Managing
{
    public enum GameStates
    {
        ENTRY,
        PLAYING,
        LOST,
        WON
    }

    [DefaultExecutionOrder(-600)]
    public class GameManager : MonoBehaviour
    {
        static GameManager _s;
        public static GameManager GetManager => _s;

        GameStates currentState;
        public GameStates CurrentState => currentState;

        GameEventsHandler gameEventsHandler;
        LevelManager levelManager;
        SoundsHandler soundsHandler;
        PlayerController player;
        PlayerInteractions playerInteractions;

        public bool IsTorchOpen { get; set; }
        public bool IsPlayerHidden { get; set; }
        public int Sensitivity { get; set; }

        public GameEventsHandler GameEventsHandler => gameEventsHandler;
        public LevelManager LevelManager => levelManager;
        public SoundsHandler SoundsHandler => soundsHandler;
        public PlayerController Player => player;
        public PlayerInteractions PlayerInteractions => playerInteractions;

        public void SwitchGameState(GameStates newState)
        {
            currentState = newState;

            switch (currentState)
            {
                case GameStates.ENTRY:
                    {
                        //nop...
                    }
                    break;

                case GameStates.PLAYING:
                    {
                        Cursor.lockState = CursorLockMode.Locked;
                        Cursor.visible = false;

                        IsPlayerHidden = false;
                        IsTorchOpen = false;
                    }
                    break;

                case GameStates.LOST:
                    {
                    }
                    break;

                case GameStates.WON:
                    {
                    }
                    break;
            }
        }

        public void SetPlayerEntity(PlayerController controller) => player = controller;
        public void SetPlayerInteractions(PlayerInteractions interactions) => playerInteractions = interactions;

        private void Awake()
        {
            if (_s != null)
            { Destroy(gameObject); }
            else
            { _s = this; }

            Sensitivity = 100;
        }

        private void Start()
        {
            CacheManagers();
        }

        void CacheManagers()
        {
            gameEventsHandler = new GameEventsHandler();
            levelManager = FindObjectOfType<LevelManager>(true);
            soundsHandler = FindObjectOfType<SoundsHandler>(true);
        }

        public void NotifyCatchPlayer()
        {
            UIManager.S.EnablePanel("Defeat");
        }
    }
}