using JLGames.RocketDriver.Actions.Extensions;
using JLGames.RocketDriver.Actions.i18n;
using JLGames.RocketDriver.Actions.Utils;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace JLGames.RocketDriver.Editor.Windows.GettingStarted
{
    public class WinGettingStarted : EditorWindow
    {
        public const string PathUxml = "Assets/JLGames/RocketDriver/Editor/Windows/GettingStarted/WinGettingStarted.uxml";
        public const string PathUss = "Assets/JLGames/RocketDriver/Editor/Windows/GettingStarted/WinGettingStarted.uss";
        
        private static bool m_OnShow = false;
        public static bool OnShow => m_OnShow;

        private II18NManager m_I18NManager;
        private VisualElement m_Root;

        private void Awake()
        {
            var settings = Resources.Load<I18NSettings>("i18n/Editor_I18NSettings");
            m_I18NManager = new I18NManager();
            m_I18NManager.LoadDataFromSettings(settings);
            var i18NTitle = m_I18NManager.GetValue("start.title");
            name = i18NTitle;
            titleContent = new GUIContent(i18NTitle, i18NTitle);

            var lang = EditorPrefs.GetString(WinGettingStartedConst.EditorKeyLang);
            if (string.IsNullOrEmpty(lang)) // 初始化
            {
                lang = m_I18NManager.Register.DefaultLang;
                EditorPrefs.SetString(WinGettingStartedConst.EditorKeyLang, lang);
            }
            else
            {
                if (lang != m_I18NManager.Register.DefaultLang) m_I18NManager.Register.SetDefaultLang(lang);
            }

            m_OnShow = true;
        }

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

            // Import UXML
            var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(PathUxml);
            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>(PathUss);
            root.styleSheets.Add(styleSheet);

            InitElements();
            UpdateElementsI18N();
            RegisterElementsCallback();
        }

        private bool m_WasOverWindow;

        private InfoElement m_Info;
        private SectionElement m_Section1;
        private SectionElement m_Section2;
        private SectionElement m_Section3;
        private RateElement m_Rate;
        private FooterElement m_Footer;

        private void OnDestroy()
        {
            RuregisterElementsCallback();
            if (m_WasOverWindow)
            {
                if (null != m_Footer)
                {
                    var show = m_Footer.ToggleShow.value;
                    EditorPrefs.SetBool(WinGettingStartedConst.EditorKeyNotShow, !show);
                }
            }

            m_OnShow = false;
        }

        private void OnGUI()
        {
            // 判断手动触发过渲染
            // 手动渲染要求：焦点、 显示更新
            if (mouseOverWindow == this)
                m_WasOverWindow = true;

            var @event = Event.current;
            if (@event.type == EventType.KeyUp)
            {
                if (@event.keyCode == KeyCode.Escape)
                {
                    WindowsUtil.CloseGettingStarted();
                    return;
                }
            }
        }

        private void InitElements()
        {
            m_Info = new InfoElement(m_Root.GetElementByName("info"));

            var sections = m_Root.GetElementByName<ScrollView>("sections").contentContainer;

            m_Section1 = new SectionElement(sections.GetElementByName("start"));
            m_Section2 = new SectionElement(sections.GetElementByName("note"));
            m_Section3 = new SectionElement(sections.GetElementByName("support"));

            m_Rate = new RateElement(m_Root.GetElementByName("rate"));
            m_Footer = new FooterElement(m_Root.GetElementByName("footer"));
        }

        private void UpdateElementsI18N()
        {
            m_Info.UpdateValues(m_I18NManager);
            m_Section1.UpdateValues(m_I18NManager, "start.section1");
            m_Section2.UpdateValues(m_I18NManager, "start.section2");
            m_Section3.UpdateValues(m_I18NManager, "start.section3");
            m_Rate.UpdateValues(m_I18NManager);
            m_Footer.UpdateValues(m_I18NManager);
        }

        private void RegisterElementsCallback()
        {
            m_Section1?.RegisterButtonsCallback();
            m_Section2?.RegisterButtonsCallback();
            m_Section3?.RegisterButtonsCallback();
            m_Rate?.RegisterButtonsCallback();
            m_Footer?.RegisterButtonsCallback(UpdateElementsI18N);
        }

        private void RuregisterElementsCallback()
        {
            m_Footer?.UnregisterButtonsCallback();
            m_Rate?.UnregisterButtonsCallback();
            m_Section3?.UnregisterButtonsCallback();
            m_Section2?.UnregisterButtonsCallback();
            m_Section1?.UnregisterButtonsCallback();
        }
    }
}