using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace WS
{
    ///<summary>按下</summary>
    public enum PointerEventType
    {
        ///<summary>按下</summary>
        OnDown,
        ///<summary>抬起</summary>
        OnUp,
        ///<summary>进入</summary>
        OnEnter,
        ///<summary>退出</summary>
        OnExit
    }

    ///<summary>指针事件管理</summary>
    public class PointerEventComponent : WSComponent
    {
        //所有指针事件
        private Dictionary<GameObject, PointerEventMono> AllPointerEvent = new Dictionary<GameObject, PointerEventMono>();

        /// <summary>给物体(UI)添加指针事件</summary>
        /// <param name="ui">UI</param>
        /// <param name="type">指针类型</param>
        /// <param name="callback">回调</param>
        public void AddPointerEvent(GameObject ui, PointerEventType type, UnityAction callback)
        {
            if (!AllPointerEvent.ContainsKey(ui))
            {
                PointerEventMono component = ui.AddComponent<PointerEventMono>();
                AllPointerEvent.Add(ui, component);
            }
            PointerEventMono pointer = AllPointerEvent[ui];
            switch (type)
            {
                case PointerEventType.OnDown:
                    pointer.OnDown = callback;
                    break;
                case PointerEventType.OnUp:
                    pointer.OnUp = callback;
                    break;
                case PointerEventType.OnEnter:
                    pointer.OnEnter = callback;
                    break;
                case PointerEventType.OnExit:
                    pointer.OnExit = callback;
                    break;
            }
        }

        public override void Dispose()
        {
            base.Dispose();
            foreach (var item in AllPointerEvent)
            {
                UnityEngine.Object.Destroy(item.Value);
            }
            AllPointerEvent.Clear();
        }
    }
}