using UnityEngine;
using UnityEngine.UIElements;

namespace VOID.UserInterface.Game
{
    public class PauseMenuUI : MonoBehaviour
    {
        public static PauseMenuUI current { get; private set; }

        // Visual Elements
        private VisualElement _rootElement;
        private Button _resumeButton;
        private Button _endRunButton;
        private Button _settingsButton;
        private Button _exitButton;
        
        private void Awake()
        {
            current = this;

            var uiDocument = gameObject.GetComponent<UIDocument>();
            var templateElement = uiDocument.rootVisualElement;

            _rootElement = templateElement.Q("pause-menu-root");
            _resumeButton = _rootElement.Q<Button>("resume");
            _resumeButton.clicked += ResumeButton;
            _endRunButton = _rootElement.Q<Button>("end-run");
            _endRunButton.clicked += EndRunButton;
            _settingsButton = _rootElement.Q<Button>("settings");
            _settingsButton.clicked += SettingsButton;
            _exitButton = _rootElement.Q<Button>("exit");
            _exitButton.clicked += ExitButton;

            _rootElement.dataSource = this;
            _rootElement.pickingMode = PickingMode.Position;
            
            Hide();
        }

        public void Show()
        {
            Toggle(true);
        }

        public void Hide()
        {
            Toggle(false);
        }

        public void Toggle()
        {
            Toggle(_rootElement.style.display == DisplayStyle.None);
        }

        public void Toggle(bool show)
        {
            _rootElement.style.display = show ? DisplayStyle.Flex : DisplayStyle.None;
        }

        private void ResumeButton()
        {
            Hide();
        }

        private void EndRunButton()
        {
            // TODO:
            Debug.LogWarning("TODO: PAUSE MENU - END RUN");
        }

        private void SettingsButton()
        {
            // TODO:
            Debug.LogWarning("TODO: PAUSE MENU - SETTINGS");
        }

        private void ExitButton()
        {
            Application.Quit();
        }
    }
}
