using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.EventSystems;

namespace GameSDK
{
    /// <summary>
    /// UI 视觉增强工具 - 为按钮添加高亮色、为 InputField 添加选中边框效果
    /// 挂载到 SDKRoot 上即可自动生效
    /// </summary>
    public class SDKUIStyle : MonoBehaviour
    {
        [Header("颜色配置")]
        public Color buttonHoverColor = new Color(0.9f, 0.95f, 1f, 1f);
        public Color buttonPressedColor = new Color(0.7f, 0.8f, 0.9f, 1f);
        public Color inputSelectedColor = new Color(0.3f, 0.7f, 1f, 1f);
        public Color inputNormalColor = new Color(0.85f, 0.85f, 0.85f, 1f);
        public float transitionDuration = 0.15f;

        private void Start()
        {
            ApplyStyles();
        }

        private void ApplyStyles()
        {
            // 为所有按钮添加颜色过渡
            var buttons = transform.GetComponentsInChildren<Button>(true);
            foreach (var btn in buttons)
            {
                if (btn.colors.highlightedColor == Color.white && btn.colors.pressedColor == Color.white)
                {
                    var colors = btn.colors;
                    colors.highlightedColor = buttonHoverColor;
                    colors.pressedColor = buttonPressedColor;
                    colors.fadeDuration = transitionDuration;
                    btn.colors = colors;
                }
            }

            // 为所有 InputField 添加选中边框效果
            var inputs = transform.GetComponentsInChildren<TMP_InputField>(true);
            foreach (var input in inputs)
            {
                var img = input.targetGraphic as Image;
                if (img != null)
                {
                    img.color = inputNormalColor;
                }
                input.onSelect.AddListener((s) =>
                {
                    if (input.targetGraphic is Image im)
                        im.color = inputSelectedColor;
                });
                input.onDeselect.AddListener((s) =>
                {
                    if (input.targetGraphic is Image im)
                        im.color = inputNormalColor;
                });
            }
        }
    }
}
