//using BPASmartClient.Peripheral;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Concurrent;

namespace BPASmartClient.Message
{
    public class MessageLog
    {
        private volatile static MessageLog _Instance;
        public static MessageLog GetInstance => _Instance ?? (_Instance = new MessageLog());
        private MessageLog() { }

        #region 普通消息日志
        /// <summary>
        /// 日志显示委托
        /// </summary>
        public Action<string> InfoNotify { get; set; }

        /// <summary>
        /// 日志信息
        /// </summary>
        public string LogInfo { get; set; } = string.Empty;

        /// <summary>
        /// 普通日志输出
        /// </summary>
        /// <param name="info"></param>
        public void Show(string info)
        {
            Debug.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")}：{info}");
            LogInfo = $"{DateTime.Now.ToString("HH:mm:ss")}：{info} \n\r {LogInfo}";
            if (InfoNotify != null) InfoNotify(info);
        }
        #endregion

        #region 异常消息日志
        /// <summary>
        /// 异常日志委托
        /// </summary>
        public Action<string> ExInfoNotify { get; set; }

        /// <summary>
        /// 异常日志信息
        /// </summary>
        public string ExLogInfo { get; set; } = string.Empty;

        /// <summary>
        /// 异常日志输出
        /// </summary>
        /// <param name="info"></param>
        public void ShowEx(string info)
        {
            Debug.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")}：{info}");
            ExLogInfo = $"{DateTime.Now.ToString("HH:mm:ss")}：{info} \n\r {ExLogInfo}";
            if (ExInfoNotify != null) ExInfoNotify(info);
        }
        #endregion

        #region 设备过程日志
        /// <summary>
        /// 设备过程日志委托
        /// </summary>
        public Action<string, string> DeviceProcessLogNotify { get; set; }

        /// <summary>
        /// 设备日志信息字典
        /// </summary>
        public ConcurrentDictionary<string, string> DPLogInfo = new ConcurrentDictionary<string, string>();

        /// <summary>
        /// 设备日志输出
        /// </summary>
        /// <param name="info"></param>
        public void DeviceProcessLogShow(string id, string info)
        {
            if (!DPLogInfo.ContainsKey(id))
                DPLogInfo.TryAdd(id, info);
            else
                DPLogInfo[id] = $"{DateTime.Now.ToString("HH:mm:ss")}：{info} \n\r {DPLogInfo[id]}";
            Debug.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")}：{info}");
            DeviceProcessLogNotify?.Invoke(id, info);
        }

        #endregion

        #region 设备告警日志
        /// <summary>
        /// 设备告警日志委托
        /// </summary>
        public Action<string, string> DeviceAlarmLogNotify { get; set; }
        public Action AlarmLogNotify { get; set; }
        /// <summary>
        /// 设备告警日志委托字典
        /// </summary>
        public ConcurrentDictionary<string, string> DPAlarmInfo = new ConcurrentDictionary<string, string>();
        /// <summary>
        /// 设备告警日志输出
        /// </summary>
        /// <param name="info"></param>
        public void AddDeviceAlarmLogShow(string id, string info)
        {
            DPAlarmInfo[info] = id;
            Debug.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")}：{info}");
            if (DeviceAlarmLogNotify != null) DeviceAlarmLogNotify(id, info);
            if (AlarmLogNotify != null) AlarmLogNotify();
        }
        /// <summary>
        /// 设备告警日志输出
        /// </summary>
        /// <param name="info"></param>
        public void DeleteDeviceAlarmLogShow(string id, string info)
        {
            if (DPAlarmInfo.ContainsKey(id))
            {
                string mes = string.Empty;
                DPAlarmInfo.Remove(id, out mes);
                if (AlarmLogNotify != null) AlarmLogNotify();
            }
        }


        #endregion

        #region 查找设备ID
        /// <summary>
        /// 查询设备ID
        /// </summary>
        /// <returns></returns>
        public object ReturnDeviceID()
        {
            object DeviceId = null;
            //StackTrace trace = new StackTrace();
            //for (int i = 1; i < 10; i++)
            //{
            //    Type type = trace.GetFrame(i).GetMethod().ReflectedType;
            //    try
            //    {
            //        object obj = Activator.CreateInstance(type);
            //        if (obj is IPeripheral)
            //        {
            //            IPeripheral peripheral = obj as IPeripheral;
            //            DeviceId = type?.GetProperty("DeviceId")?.GetValue(peripheral, null);
            //        }
            //        if (DeviceId != null) break;
            //    }
            //    catch (Exception ex)
            //    {

            //    }

            //}

            return DeviceId;
        }
        #endregion



    }
}
