using UnityEngine;
using UnityEngine.UI;
using Rokid.MRC;

public delegate void ButtonFunc(GameObject obj, object userData);
public delegate void ToggleFunc(GameObject obj, bool isOn);
public delegate void DropdownFunc(GameObject obj, int index);
public delegate void InputFieldFunc(GameObject obj, string val);
public delegate void InputFieldEndFunc(GameObject obj, string val);
public delegate void ScrollRectFunc(GameObject obj, Vector2 pos);
public delegate void SliderFunc(GameObject obj, float val);

// UI绑定事件接口
public static class UguiEvent
{
    //主要有Button、Toggle、Dropdown（下拉框）、InputField（输入框）、ScrollRect、Slider

    public static void ButtonClick(GameObject go, ButtonFunc func, object userData = null)
    {
        Button button = go.GetComponent<Button>();
        if(button == null)
            button = go.AddComponent<Button>();

        button.onClick.RemoveAllListeners();
        button.onClick.AddListener(delegate ()
        {
            if(func != null)
            {
                func(go, userData);
            }

        });
    }
    public static void ButtonClick(Transform go, ButtonFunc func, object userData = null)
    {
        Button button = go.GetComponent<Button>();
        if (button == null)
            button = go.gameObject.AddComponent<Button>();

        button.onClick.RemoveAllListeners();
        button.onClick.AddListener(delegate ()
        {
            if (func != null)
            {
                func(go.gameObject, userData);
            }

        });
    }

    public static void ToggleClick(GameObject go, ToggleFunc func)
    {
        Toggle toggle = go.GetComponent<Toggle>();
        toggle.onValueChanged.RemoveAllListeners();
        toggle.onValueChanged.AddListener(delegate (bool isOn)
        {
            if(func != null)
            {
                func(go, isOn);
            }
        });
    }

    public static void DropdownClick(GameObject go, DropdownFunc func)
    {
        Dropdown dropdown = go.GetComponent<Dropdown>();
        dropdown.onValueChanged.RemoveAllListeners();
        dropdown.onValueChanged.AddListener(delegate (int index)
        {
            if(func != null)
            {
                func(go, index);
            }
        });
    }

    public static void InputFieldChange(GameObject go, InputFieldFunc func)
    {
        InputField inputField = go.GetComponent<InputField>();
        inputField.onValueChanged.RemoveAllListeners();
        inputField.onValueChanged.AddListener(delegate (string val)
        {
            if(func != null)
            {
                func(go, val);
            }
        });
    }
    public static void InputFieldChange(InputField inputField, InputFieldFunc func)
    {
        if (inputField == null) return;
        inputField.onValueChanged.RemoveAllListeners();
        inputField.onValueChanged.AddListener(delegate (string val)
        {
            if (func != null)
            {
                func(inputField.gameObject, val);
            }
        });
    }

    public static void InputFieldEndEdit(GameObject go, InputFieldEndFunc func)
    {
        InputField inputField = go.GetComponent<InputField>();
        inputField.onEndEdit.RemoveAllListeners();
        inputField.onEndEdit.AddListener(delegate(string val)
        {
            if(func != null)
            {
                func(go, val);
            }
        });
    }
    public static void EventTriggerOnClick(GameObject go, EventTriggerListener.VoidDelegate func)
    {
        EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
        if (listener == null) listener = go.AddComponent<EventTriggerListener>();
        if (func != null)
        {
            listener.onClick += func;
        }
    }
    public static void EventTriggerOnEnter(GameObject go, EventTriggerListener.VoidDelegate func)
    {
        EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
        if (listener == null) listener = go.AddComponent<EventTriggerListener>();
        if (func != null)
        {
            listener.onEnter += func;
        }
    }

    public static void EventTriggerOnDown(GameObject go, EventTriggerListener.VoidDelegate func)
    {
        EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
        if(listener == null)
            listener = go.AddComponent<EventTriggerListener>();
        if(func != null)
        {
            listener.onDown += func;
        }
    }

    public static void EventTriggerOnUp(GameObject go, EventTriggerListener.VoidDelegate func)
    {
        EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
        if(listener == null)
            listener = go.AddComponent<EventTriggerListener>();
        if(func != null)
        {
            listener.onUp += func;
        }
    }

    public static void EventTriggerOnExit(GameObject go, EventTriggerListener.VoidDelegate func)
    {
        EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
        if(listener == null)
            listener = go.AddComponent<EventTriggerListener>();
        if(func != null)
        {
            listener.onExit += func;
        }
    }

    public static void EventTriggerOnSelect(GameObject go, EventTriggerListener.VoidDelegate func)
    {
        EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
        if(listener == null)
            listener = go.AddComponent<EventTriggerListener>();
        if(func != null)
        {
            listener.onSelect += func;
        }
    }

    public static void EventTriggerOnDragBegin(GameObject go, EventTriggerListener.VoidDelegate func)
    {
        EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
        if(listener == null)
            listener = go.AddComponent<EventTriggerListener>();
        if(func != null)
        {
            listener.onDragBegin += func;
        }
    }

    public static void EventTriggerOnDragEnd(GameObject go, EventTriggerListener.VoidDelegate func)
    {
        EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
        if(listener == null)
            listener = go.AddComponent<EventTriggerListener>();
        if(func != null)
        {
            listener.onDragEnd += func;
        }
    }

    public static void EventTriggerOnDrag(GameObject go, EventTriggerListener.VoidDelegate func)
    {
        EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
        if(listener == null)
            listener = go.AddComponent<EventTriggerListener>();
        if(func != null)
        {
            listener.onDrag += func;
        }
    }

    public static void EventTriggerOnScroll(GameObject go, EventTriggerListener.VoidDelegate func)
    {
        EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
        if(listener == null)
            listener = go.AddComponent<EventTriggerListener>();
        if(func != null)
        {
            listener.onScroll += func;
        }
    }

    public static void ScrollRectValueChange(GameObject go, ScrollRectFunc func)
    {
        ScrollRect scrollRect = go.GetComponent<ScrollRect>();
        scrollRect.onValueChanged.RemoveAllListeners();
        scrollRect.onValueChanged.AddListener(delegate (Vector2 pos)
        {
            if(func != null)
            {
                func(go, pos);
            }
        });
    }


    public static void SliderValueChange(GameObject go, SliderFunc func)
    {
        Slider slider = go.GetComponent<Slider>();
        slider.onValueChanged.RemoveAllListeners();
        slider.onValueChanged.AddListener(delegate (float val)
        {
            if(func != null)
            {
                func(go, val);
            }
        });
    }
    public static void SliderValueChange(Transform trans, SliderFunc func)
    {
        Slider slider = trans.GetComponent<Slider>();
        slider.onValueChanged.RemoveAllListeners();
        slider.onValueChanged.AddListener(delegate (float val)
        {
            if (func != null)
            {
                func(trans.gameObject, val);
            }
        });
    }
    /// <summary>
    /// 强制重建layout
    /// </summary>
    public static void ForceRebuildLayout(RectTransform trans)
    {
        LayoutRebuilder.ForceRebuildLayoutImmediate(trans);
    }

}