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

public interface IEvent 
{

}
public class Events<T> : IEvent
{
    public UnityAction<T> func;
    public Events(UnityAction<T>func) 
    {
        this.func += func;
    }
}
public class Events : IEvent 
{
    public UnityAction func;

    public Events(UnityAction func) 
    {
        this.func += func;
    }
}
public class EventCenter : BaseMgr<EventCenter>
{
    private Dictionary<string, IEvent> eventdic = new Dictionary<string, IEvent>();

    public void AddUpdateListener<T>(string name,UnityAction<T> func) 
    {
        if (!eventdic.ContainsKey(name))
            eventdic.Add(name, new Events<T>(func));
        else
            (eventdic[name] as Events<T>).func += func;
    }
    public void AddUpdateListener(string name, UnityAction func)
    {
        if (!eventdic.ContainsKey(name))
            eventdic.Add(name, new Events(func));
        else
            (eventdic[name] as Events).func += func;
    }
    public void EventTrigger<T>(string name,T obj)
    {
        if (!eventdic.ContainsKey(name)) return;
        (eventdic[name] as Events<T>).func?.Invoke(obj);
    }

    public void EventTrigger(string name)
    {
        if (!eventdic.ContainsKey(name)) return;
        (eventdic[name] as Events).func?.Invoke();
    }

    public void RemoveUpdateListener<T>(string name, UnityAction<T> func)
    {
        if (!eventdic.ContainsKey(name)) return;
        else
            (eventdic[name] as Events<T>).func-= func;
    }

    public void RemoveUpdateListener(string name, UnityAction func)
    {
        if (!eventdic.ContainsKey(name)) return;
        else
            (eventdic[name] as Events).func -= func;
    }
    public void Clear() 
    {
        eventdic = null;
    }
}
