using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

/// <summary>
/// Item 基类 方便使用组件
/// </summary>
public class BaseItem : MonoBehaviour
{
    /// <summary>
    /// 存储面板上所有控件
    /// </summary>
    private readonly Dictionary<string, List<UIBehaviour>> controlDic = new Dictionary<string, List<UIBehaviour>>();

    /// <summary>
    /// 初始化 查找控件
    /// </summary>
    protected virtual void Awake()
    {
        FindChildrenControl<Button>();
        FindChildrenControl<Image>();
        FindChildrenControl<Text>();
        FindChildrenControl<Toggle>();
        FindChildrenControl<Slider>();
        FindChildrenControl<ScrollRect>();
        FindChildrenControl<InputField>();
    }

    /// <summary>
    /// 得到对应名称的控件
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="controlName"></param>
    /// <returns></returns>
    public T GetControl<T>(string controlName) where T : UIBehaviour
    {
        if (controlDic.ContainsKey(controlName))
        {
            for (int i = 0; i < controlDic[controlName].Count; i++)
            {
                if (controlDic[controlName][i] is T)
                    return controlDic[controlName][i] as T;
            }
        }
        return null;
    }

    /// <summary>
    /// 查找所有子对象
    /// </summary>
    /// <typeparam name="T"></typeparam>
    private void FindChildrenControl<T>() where T : UIBehaviour
    {
        T[] controls = GetComponentsInChildren<T>();

        for (int i = 0; i < controls.Length; i++)
        {
            string controlName = controls[i].gameObject.name;
            if (controlDic.ContainsKey(controlName))
            {
                controlDic[controlName].Add(controls[i]);
            }
            else
            {
                controlDic.Add(controlName, new List<UIBehaviour>() { controls[i] });
            }

            if (controls[i] is Button button)
            {
                button.onClick.AddListener(() =>
                {
                    OnClick(controlName);
                });
            }
            else if (controls[i] is Toggle toggle)
            {
                toggle.onValueChanged.AddListener((value) =>
                {
                    OnChangeToggle(controlName, value);
                });
            }
            else if (controls[i] is Slider slider)
            {
                slider.onValueChanged.AddListener((value) =>
                {
                    OnChangeSlider(controlName, value);
                });
            }
            else if (controls[i] is InputField inputField)
            {
                inputField.onValueChanged.AddListener((value) =>
                {
                    OnChangeInputFile(controlName, value);
                });
            }
        }
    }

    /// <summary>
    /// 显示自己 
    /// </summary>
    public virtual void ShowMe()
    {

    }

    /// <summary>
    /// 隐藏自己
    /// </summary>
    public virtual void HideMe()
    {

    }

    /// <summary>
    /// 按钮点击事件
    /// </summary>
    /// <param name="btnName"></param>
    protected virtual void OnClick(string btnName)
    {

    }

    /// <summary>
    /// Toogle点击
    /// </summary>
    /// <param name="toogleName">toogle名称</param>
    /// <param name="isOn">状态值</param>
    protected virtual void OnChangeToggle(string toogleName, bool isOn)
    {

    }

    /// <summary>
    /// Slider拖动
    /// </summary>
    /// <param name="SliderName">Slider组件名称</param>
    /// <param name="value">数值</param>
    protected virtual void OnChangeSlider(string SliderName, float value)
    {

    }

    /// <summary>
    /// 输入框输入
    /// </summary>
    /// <param name="inputFileName">输入框名称</param>
    /// <param name="value">输入框内的值</param>
    protected virtual void OnChangeInputFile(string inputFileName, string value)
    {

    }
}
