using parkMonitor.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace parkMonitor.server {

	/// <summary>单线程命令服务</summary>
	public class CmdServer {
		public static CmdServer ins = new CmdServer();

		Dictionary<CmdType, List<Action>> mapFun = new Dictionary<CmdType, List<Action>>();
		Dictionary<CmdType, List<Delegate>> mapFunArgs = new Dictionary<CmdType, List<Delegate>>();

		public void listen(CmdType type, Action fun) {
			if(!mapFun.ContainsKey(type)) {
				mapFun[type] = new List<Action>();
			}
			mapFun[type].Add(fun);
		}

		//public void listen(CmdType type, Action<object> fun) {
		//	if(!mapFun.ContainsKey(type)) {
		//		mapFunArgs[type] = new List<Delegate>();
		//	}
		//	mapFunArgs[type].Add(fun);
		//}

		public void listen<T>(CmdType type, Action<T> fun) {
			if(!mapFunArgs.ContainsKey(type)) {
				mapFunArgs[type] = new List<Delegate>();
			}
			mapFunArgs[type].Add(fun);
		}

		public void send(CmdType type) {
			if(!mapFun.ContainsKey(type)) {
				return;
			}

			var lstFun = mapFun[type];
			for(int i = 0; i < lstFun.Count; ++i) {
				lstFun[i].Invoke();
			}
		}

		public void send<T>(CmdType type, T data) {
			if(!mapFunArgs.ContainsKey(type)) {
				return;
			}

			var lstFun = mapFunArgs[type];
			for(int i = 0; i < lstFun.Count; ++i) {
				Action<T> fun = lstFun[i] as Action<T>;
				if(fun == null) {
					continue;
				}
				fun.Invoke(data);
			}
		}
	}

	/// <summary>命令类型</summary>
	public enum CmdType {
		/// <summary>窗口关闭</summary>
		MainWinClosed,
	}

}
