using com.superscene.util;
using parkMonitor.model;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace parkMonitor.server.uiLogServer {
	/// <summary>UI显示日志服务</summary>
	public class UILogServer {
		public static readonly UILogServer ins = new UILogServer();
		public static UILogServer getInstance() { return ins; }

		public bool showTime = true;
		//public int maxCount = 1024 * 1024; //最大显示数量
		public int maxListCount = 200;//最大显示数量
		object syncUpdate = new object();
		List<UILogModel> lstData = new List<UILogModel>();

		public UILogServer() {

		}

		public void log(string data) { update("", data, UILogColor.log); }
		public void info(string data) { update("", data, UILogColor.info); }
		public void reset(string data) { update("", data, UILogColor.reset); }
		public void warn(string data) { update("", data, UILogColor.warn); }
		public void error(string data) { update("", data, UILogColor.error); }

		public void log(string title, string data) { update(title, data, UILogColor.log); }
		public void info(string title, string data) { update(title, data, UILogColor.info); }
		public void reset(string title, string data) { update(title, data, UILogColor.reset); }
		public void warn(string title, string data) { update(title, data, UILogColor.warn); }
		public void error(string title, string data) { update(title, data, UILogColor.error); }

		private void update(string title, string data, Brush color = null) {
			try {
				string oneMessage = "";

				string endl = "\r\n";
				string infoTitle = "";
				if(showTime) {
					infoTitle += "" + DateTime.Now.ToString() + "";
					if(title != "") {
						infoTitle += " - ";
					}
				}
				infoTitle += title;
				if(infoTitle != "") {
					infoTitle += endl;
				}
				oneMessage += infoTitle;
				oneMessage += data;

				if(color == null) {
					color = UILogColor.info;
				}

				UILogModel md = new UILogModel(color, oneMessage);
				//List<UILogModel> lstTemp = null;
				bool isRemove = false;

				var mainWin = MainModel.ins.mainWin;
				var mainWinVM = MainModel.ins.mainWinVM;
				//string text = "";
				lock(syncUpdate) {
					lstData.Add(md);
					if(lstData.Count >= maxListCount) {
						lstData.RemoveAt(0);
						isRemove = true;
					}

					mainWin.Dispatcher.Invoke(() => {
						try {
							mainWin.addLogText(md);
							if(isRemove) {
								mainWin.removeFirstLog();
							}
							//Debug.WriteLine(tm.end());
						} catch(Exception) {

						}
					});
				}
			} catch(Exception) { }
		}
	}

	public class UILogColor {
		public static Brush log = null;
		public static Brush info = null;
		public static Brush reset = null;
		public static Brush warn = null;
		public static Brush error = null;
		static UILogColor() {
			log = create("#cdcdcd");
			info = create("#fff");
			reset = create("#43b9cb");
			warn = create("#deec46");
			error = create("#ff3d3d");
		}

		private static Brush create(string strColor) {
			Brush color = new SolidColorBrush((Color)ColorConverter.ConvertFromString(strColor));
			color.Freeze();
			return color;
		}
	}

	public class UILogModel {
		//public DateTime date = DateTime.Now;
		//public AlarmStatus alarmStatus = AlarmStatus.None;
		public Brush color = UILogColor.info;
		public string info = "";

		public UILogModel() {

		}

		public UILogModel(Brush _color, string _info) {
			color = _color;
			info = _info;
		}
	}

}
