using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Concurrent;

namespace BPASmartClient.Helper
{
    public class ActionManage
    {

        private volatile static ActionManage _Instance;
        public static ActionManage GetInstance => _Instance ?? (_Instance = new ActionManage());
        private ActionManage() { }

        //private static ConcurrentDictionary<string, delegate> actions = new ConcurrentDictionary<string, delegate>();
        private static ConcurrentDictionary<string, Delegation> actions = new ConcurrentDictionary<string, Delegation>();

        static readonly object SendLock = new object();
        static readonly object SendParLock = new object();
        static readonly object RegisterLock = new object();

        /// <summary>
        /// 注销委托
        /// </summary>
        /// <param name="key"></param>
        public void CancelRegister(string key)
        {
            if (actions.ContainsKey(key))
                actions.TryRemove(key, out Delegation t);
        }

        /// <summary>
        /// 执行注册过的委托
        /// </summary>
        /// <param name="key">注册委托的key</param>
        /// <param name="par">委托参数</param>
        /// <param name="Callback">委托回调</param>
        public void Send(string key, object par, Action Callback = null)
        {
            lock (SendLock)
                if (actions.ContainsKey(key)) actions[key].ActionPar.Invoke(par, Callback);
        }

        /// <summary>
        /// 执行注册过的委托
        /// </summary>
        /// <param name="key">注册委托的key</param>
        /// <param name="par">委托参数</param>
        /// <param name="Callback">委托回调</param>
        public void Send(string key, object[] par, Action Callback = null)
        {
            lock (SendLock)
                if (actions.ContainsKey(key)) actions[key].ActionPars.Invokes(par, Callback);
        }

        /// <summary>
        /// 执行注册过的委托
        /// </summary>
        /// <param name="key">注册委托的key</param>
        /// <param name="Callback">委托回调</param>
        public void Send(string key, Action Callback = null)
        {
            lock (SendLock)
                if (actions.ContainsKey(key)) actions[key].ActionBus?.Invoke(Callback);
        }

        public object SendResult(string key, object par = null)
        {
            lock (SendLock)
                if (actions.ContainsKey(key))
                    if (par == null)
                    {
                        //if (actions[key].FuncObj != null)
                        return actions[key].FuncObj?.Invoke();
                    }
                    else
                    {
                        //if (actions[key].FuncPar != null)
                        return actions[key].FuncPar?.Invoke(par);
                    }
            return default;
        }

        public void Register<T>(T action, string key)
        {
            lock (RegisterLock)
            {
                if (action != null)
                {
                    if (!actions.ContainsKey(key))
                    {
                        if (action is Action actionBus)
                            actions.TryAdd(key, new Delegation() { ActionBus = actionBus });

                        if (action is Action<object> actionObj)
                            actions.TryAdd(key, new Delegation() { ActionPar = actionObj });

                        if (action is Action<object[]> actionObjs)
                            actions.TryAdd(key, new Delegation() { ActionPars = actionObjs });

                        if (action is Func<object> funcObj)
                            actions.TryAdd(key, new Delegation() { FuncObj = funcObj });

                        if (action is Func<object, object> puncPar)
                            actions.TryAdd(key, new Delegation() { FuncPar = puncPar });
                    }
                }
            }

        }

    }


    internal class Delegation
    {
        /// <summary>
        /// 带参数的委托
        /// </summary>
        public Action<object> ActionPar { get; set; }
        /// <summary>
        /// 带参数的委托
        /// </summary>
        public Action<object[]> ActionPars { get; set; }
        /// <summary>
        /// 无参数的委托
        /// </summary>
        public Action ActionBus { get; set; }
        /// <summary>
        /// 有返回值的委托
        /// </summary>
        public Func<object> FuncObj { get; set; }
        /// <summary>
        /// 有返回值，有参数的委托
        /// </summary>
        public Func<object, object> FuncPar { get; set; }
    }


}
