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


    public class MessageCenter
    {
        //委托消息传递
        public delegate void DelMessageDelivery(object kv);

        //消息中心缓存集合
        //<string:数据大的分类，DelMessageDelivery数据执行委托>
        public static Dictionary<object, DelMessageDelivery> _dicMessages = new Dictionary<object, DelMessageDelivery>();

        /// <summary>
        ///加消息的监听
        /// </summary>
        /// <param name="messageType">消息分类</param>
        /// <param name="handler">消息委托</param>
        public static void AddMsgListener(object messageType, DelMessageDelivery handler)
        {
            if(!_dicMessages.ContainsKey(messageType))
            {
                _dicMessages.Add(messageType, null);
            }
            _dicMessages[messageType] += handler;
        }
        /// <summary>
        /// 移除消息监听
        /// </summary>
        /// <param name="messageType">消息分类</param>
        /// <param name="handler">消息委托</param>
        public static void RemoveMsgListener(object messageType, DelMessageDelivery handler)
        {
            if(_dicMessages.ContainsKey(messageType))
            {
                _dicMessages[messageType] -= handler;
            }
        }

        /// <summary>
        /// 取消所有消息监听
        /// </summary>
        public static void ClearAllMsgListener()
        {
            if(_dicMessages != null)
            {
                _dicMessages.Clear();
            }
        }
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="messageType">消息分类</param>
        /// <param name="kv">键值对(对象)</param>
        public static void SendMessage(object messageType, object kv)
        {
            DelMessageDelivery del;
            if(_dicMessages.TryGetValue(messageType, out del))
            {
                if(del != null)
                {
                    //调用委托
                    del(kv);
                }
            }
        }
    }

/// <summary>
/// 配合委托，实现委托数据传递
/// </summary>
public class KeyValuesUpdate
{
    //键
    private string _Key;
    //值
    private object _Values;

    //只读属性
    public string Key
    {
        get
        {
            return _Key;
        }
    }
    public object Values
    {
        get
        {
            return _Values;
        }
    }
    public KeyValuesUpdate(string key, object valueObj)
    {
        _Key = key;
        _Values = valueObj;
    }
}
