using JLGames.RocketDriver;
using JLGames.RocketDriver.Actions.Extensions;
using JLGames.RocketDriver.Actions.UIElements;
using JLGames.RocketDriver.Editor.Windows.Main.Service;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace JLGames.RocketDriver.Editor.Windows.Main
{
    public class MainEditor : EditorWindow
    {
        [MenuItem("Window/UIElements/MainEditor3")]
        public static void ShowExample()
        {
            MainEditor wnd = GetWindow<MainEditor>();
            wnd.titleContent = new GUIContent("MainEditor");
            wnd.ShowUtility();
        }

        private RocketDriverSettings m_Settings;

        private VisualElement m_Root;
        private ToggleGroup m_BarGroup;
        private VisualElement m_Content;

        private ServiceContentView m_ServiceContentView;
        private ServiceNodeInspector m_ServiceNodeInspector;

        public void CreateGUI()
        {
            // Each editor window contains a root VisualElement object
            VisualElement root = rootVisualElement;

            // Import UXML
            var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(MainEditorConfig.MainEditorUxml);
            var uxml = visualTree.CloneTree();
            m_Root = uxml.GetElementByName("root");
            root.Add(m_Root);

            // A stylesheet can be added to a VisualElement.
            // The style will be applied to the VisualElement and all of its children.
            var styleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>(MainEditorConfig.MainEditorUss);
            root.styleSheets.Add(styleSheet);

            InitView();
        }

        public void ShowTab(int tabIndex)
        {
            m_BarGroup?.SetToggleOn(tabIndex, true);
        }

        private void OnSelectionChange()
        {
            var settings = Selection.activeObject as RocketDriverSettings;
            if (null == settings) return;

            m_Settings = settings;
        }

        private void InitView()
        {
            m_BarGroup = m_Root.GetElementByChain<ToggleGroup>("toolbar/bar-group");
            m_Content = m_Root.GetElementByName("content");
            m_BarGroup.RegisterCallback<ChangeEvent<bool>>(OnBarToggleChanged);

            m_ServiceContentView = m_Root.Q<ServiceContentView>();
            m_ServiceContentView.OnNodeSelected = OnServiceNodeSelected;
            m_ServiceNodeInspector = m_Root.Q<ServiceNodeInspector>();
        }

        private void OnBarToggleChanged(ChangeEvent<bool> evt)
        {
            var toggle = evt.target as Toggle;
            if (null == toggle || !toggle.value) return;

            var tabIndex = toggle.tabIndex;
            var contentSize = m_Content.childCount;

            for (var index = 0; index < contentSize; index++)
            {
                m_Content[index].style.display = tabIndex == index ? DisplayStyle.Flex : DisplayStyle.None;
            }
        }

        private void OnServiceNodeSelected(ServiceNodeView nodeView)
        {
            m_ServiceNodeInspector.UpdateSelection(nodeView);
        }
    }
}