using System;
using System.Collections.Generic;
using System.Reflection;
using NBC;
using UnityEngine;

namespace NBF
{
    public class InputDef
    {
        public class UI
        {
            public static Dictionary<string, List<string>> Icons = new Dictionary<string, List<string>>();

            [InputIcon("icon_controller_123", "icon_controller_2")]
            public const string Back = "Back";

            [InputIcon("icon_controller_129", "icon_controller_1")]
            public const string Enter = "Enter";

            [InputIcon("icon_controller_129", "icon_controller_1")]
            public const string Reset = "Reset";
            
            [InputIcon("icon_controller_127", "icon_controller_19")]
            public const string Tab = "Tab";

            [InputIcon("icon_controller_29", "icon_controller_89")]
            public const string Prev = "Prev";

            [InputIcon("icon_controller_30", "icon_controller_77")]
            public const string Next = "Next";

            [InputIcon("icon_controller_98", "icon_controller_27")]
            public const string SubPrev = "SubPrev";

            [InputIcon("icon_controller_75", "  icon_controller_28")]
            public const string SubNext = "SubNext";

            [InputIcon("", "")] public const string Left = "Left";
            [InputIcon("", "")] public const string Right = "Right";
            [InputIcon("", "")] public const string Up = "Up";
            [InputIcon("", "")] public const string Down = "Down";
        }

        public static class Player
        {
            public static Dictionary<string, List<string>> Icons = new Dictionary<string, List<string>>();
            public const string Run = "Run";
            public const string Use1 = "Use1";
            public const string Use2 = "Use2";
            
            public const string QuickStarts = "Quick";
            public const string Quick1 = "Quick1";
            public const string Quick2 = "Quick2";
            public const string Quick3 = "Quick3";
            public const string Quick4 = "Quick4";
            public const string Quick5 = "Quick5";
            public const string Quick6 = "Quick6";
            public const string Quick7 = "Quick7";
            public const string Quick8 = "Quick8";
            public const string Quick9 = "Quick9";
            
            public const string ToBag = "ToBag";
            
            public const string Move = "Move";
            public const string Look = "Look";
            public const string UseTorch = "UseTorch";
        }

        #region Load Icon

        public static void LoadIcon()
        {
            // 获取InputDef类型
            Type inputDefType = typeof(NBF.InputDef);

            // 检查UI类
            Type uiType = inputDefType.GetNestedType("UI");
            if (uiType != null)
            {
                var uiFields = GetInputIconFieldsFromType(uiType);
                if (uiFields.Count > 0)
                {
                    foreach (var field in uiFields)
                    {
                        InputIconAttribute attribute =
                            (InputIconAttribute)Attribute.GetCustomAttribute(field, typeof(InputIconAttribute));

                        string value = (string)field.GetValue(null); // 获取常量值
                        if (attribute != null)
                        {
                            List<string> icons = new List<string>
                            {
                                attribute.KeyBoardIcon,
                                attribute.ControllerIcon
                            };
                            UI.Icons[value] = icons;
                        }
                    }
                }
            }

            // 检查Player类
            Type playerType = inputDefType.GetNestedType("Player");
            if (playerType != null)
            {
                var playerFields = GetInputIconFieldsFromType(playerType);
                if (playerFields.Count > 0)
                {
                    foreach (var field in playerFields)
                    {
                        InputIconAttribute attribute =
                            (InputIconAttribute)Attribute.GetCustomAttribute(field, typeof(InputIconAttribute));

                        string value = (string)field.GetValue(null); // 获取常量值
                        if (attribute != null)
                        {
                            List<string> icons = new List<string>
                            {
                                attribute.KeyBoardIcon,
                                attribute.ControllerIcon
                            };
                            Player.Icons[value] = icons;
                        }
                    }
                }
            }

            // foreach (var key in InputDef.UI.Icons.Keys)
            // {
            //     var icons = InputDef.UI.Icons[key];
            //     Log.Error($"KEY={key}  icon={string.Join(",", icons)}");
            // }
        }


        // 从特定类型获取带有InputIconAttribute的常量字段
        private static List<FieldInfo> GetInputIconFieldsFromType(Type type)
        {
            List<FieldInfo> result = new List<FieldInfo>();

            // 只获取公共常量字段
            FieldInfo[] fields = type.GetFields(
                BindingFlags.Public |
                BindingFlags.Static |
                BindingFlags.FlattenHierarchy);

            foreach (FieldInfo field in fields)
            {
                // 检查是否是常量且带有InputIconAttribute
                if (field.IsLiteral && !field.IsInitOnly &&
                    Attribute.IsDefined(field, typeof(InputIconAttribute)))
                {
                    result.Add(field);
                }
            }

            return result;
        }

        #endregion
    }
}