#if INPUTSYSTEM_SUPPORT
using System.Collections.Generic;
using System.Reflection;
using AlicizaX.Editor;
using AlicizaX.UI;
using AlicizaX.UI.Runtime;
using UnityEditor;
using UnityEngine;

namespace AlicizaX.UI111
{
    [CustomEditor(typeof(HotkeyBindComponent))]
    public class HotkeyBindComponentInspector : UnityEditor.Editor
    {
        private SerializedProperty hotButtonsProp;
        private HotkeyBindComponent _target;

        private void OnEnable()
        {
            _target = (HotkeyBindComponent)target;
            hotButtonsProp = serializedObject.FindProperty("hotButtons");
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();


            var holder = _target.GetComponent<UIHolderObjectBase>();
            if (holder == null)
            {
                EditorGUILayout.HelpBox(
                    "⚠ 当前对象缺少 UIHolderObjectBase 组件。\nHotkeyBindComponent 依赖它进行热键绑定事件。",
                    MessageType.Error
                );
            }

            EditorGUILayout.Space();

            // 🔸 灰掉显示 hotButtons 列表（不可手动编辑）
            GUI.enabled = false;
            DrawHotButtonListAlwaysExpanded(hotButtonsProp);
            GUI.enabled = true;

            EditorGUILayout.Space();

            // 🔘 按钮：扫描子物体（包含隐藏）
            if (GUILayout.Button("🔍 扫描所有子物体 (包含隐藏对象)"))
            {
                FindAllUXHotkeys();
            }

            serializedObject.ApplyModifiedProperties();
        }

        /// <summary>
        /// 自定义展开显示 hotButtons 列表
        /// </summary>
        private void DrawHotButtonListAlwaysExpanded(SerializedProperty listProp)
        {
            EditorGUILayout.LabelField("Hot Buttons", EditorStyles.boldLabel);

            if (listProp.arraySize == 0)
            {
                EditorGUILayout.HelpBox("当前没有绑定任何 UXHotkey。", MessageType.Info);
                return;
            }

            EditorGUI.indentLevel++;
            for (int i = 0; i < listProp.arraySize; i++)
            {
                var element = listProp.GetArrayElementAtIndex(i);
                var uxHotkey = element.objectReferenceValue as UXHotkeyButton;
                string name = uxHotkey != null ? uxHotkey.name : "Null";
                EditorGUILayout.ObjectField($"[{i}] {name}", element.objectReferenceValue, typeof(UXHotkeyButton), true);
            }

            EditorGUI.indentLevel--;
        }

        /// <summary>
        /// 查找所有子物体（包含隐藏）并绑定 UXHotkey
        /// </summary>
        private void FindAllUXHotkeys()
        {
            Undo.RecordObject(_target, "Scan UXHotkey");

            var uxHotkeys = _target.GetComponentsInChildren<UXHotkeyButton>(true);
            List<UXHotkeyButton> valiedHotkeys = new List<UXHotkeyButton>();
            foreach (var item in uxHotkeys)
            {
                var field = item.GetType()
                    .GetField("_hotKeyRefrence", BindingFlags.NonPublic | BindingFlags.Instance);
                var hotRefrenceObject = field.GetValue(item);
                if (hotRefrenceObject != null) valiedHotkeys.Add(item);
            }

            _target.GetType()
                .GetField("hotButtons", BindingFlags.NonPublic | BindingFlags.Instance)
                ?.SetValue(_target, valiedHotkeys.ToArray());

            var collectMethod = target.GetType().GetMethod("CollectUXHotkeys", BindingFlags.NonPublic | BindingFlags.Instance);
            collectMethod.Invoke(target, null);

            EditorUtility.SetDirty(_target);
            serializedObject.Update();
            Debug.Log($"[HotkeyBindComponent] 已找到 {uxHotkeys.Length} 个 UXHotkey 组件并绑定。");
        }
    }
}

#endif
