using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace EscapeRoomEngine.Engine.Runtime.Editor
{
    #if UNITY_EDITOR
    /// <summary>
    /// An <see cref="EditorWindow"/> used to test the <see cref="Engine"/>.
    /// </summary>
    public class EngineEditor : EditorWindow
    {
        private bool _registeredUpdateEvent;
        private Button _passToNextRoomButton, _skipCurrentRoomButton, _skipAndPassButton;
        
        [MenuItem("Window/Engine Editor")]
        public static void ShowEditor()
        {
            var window = GetWindow<EngineEditor>();
            window.titleContent = new GUIContent("Engine Editor");
        }

        public void CreateGUI()
        {
            _passToNextRoomButton = new Button(PassToNextRoom)
            {
                text = "Pass To Next Room"
            };
            rootVisualElement.Add(_passToNextRoomButton);
            _skipCurrentRoomButton = new Button(SkipCurrentRoom)
            {
                text = "Skip Current Room"
            };
            rootVisualElement.Add(_skipCurrentRoomButton);
            _skipAndPassButton = new Button(SkipAndPass)
            {
                text = "Skip Current And Pass To Next Room"
            };
            rootVisualElement.Add(_skipAndPassButton);
            
            EditorApplication.playModeStateChanged += _ => UpdateUI();
            UpdateUI();
        }

        private void PassToNextRoom()
        {
            if (Engine.Instance && EditorApplication.isPlaying)
            {
                Engine.Instance.CurrentRoom.Match(some: room => room.EnterRoom());
                UpdateUI();
            }
        }

        private void SkipCurrentRoom()
        {
            if (Engine.Instance && EditorApplication.isPlaying)
            {
                Engine.Instance.CurrentRoom.Match(some: room => room.SkipRoom());
                UpdateUI();
            }
        }

        private void SkipAndPass()
        {
            if (Engine.Instance && EditorApplication.isPlaying)
            {
                Engine.Instance.CurrentRoom.Match(some: room => room.SkipRoom());
                Engine.Instance.CurrentRoom.Match(some: room => room.EnterRoom());
                UpdateUI();
            }
        }

        private void UpdateUI()
        {
            if (Engine.Instance && EditorApplication.isPlaying)
            {
                if (!_registeredUpdateEvent)
                {
                    Engine.Instance.UpdateUIEvent += UpdateUI;
                    _registeredUpdateEvent = true;
                }
            }
            else
            {
                _registeredUpdateEvent = false;
            }
            
            _passToNextRoomButton.SetEnabled(EditorApplication.isPlaying && Engine.Instance && Engine.Instance.NumberOfRooms > 1);
            _skipCurrentRoomButton.SetEnabled(EditorApplication.isPlaying && Engine.Instance && Engine.Instance.NumberOfRooms > 0);
            _skipAndPassButton.SetEnabled(EditorApplication.isPlaying && Engine.Instance && Engine.Instance.NumberOfRooms > 0);
        }
    }
    #endif
}