using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using HBLConsole.Service;
using HBLConsole.Model;

namespace HBLConsole.Business
{
    public class AlarmHelper
    {
        private volatile static AlarmHelper _Instance;
        public static AlarmHelper GetInstance => _Instance ?? (_Instance = new AlarmHelper());
        private AlarmHelper()
        {
            Json<AlarmHzParameter>.Read();
            colorValue.TryAdd(AlarmLevel.一般报警.ToString(), Color.FromArgb(1, 255, 216, 0));
            colorValue.TryAdd(AlarmLevel.严重报警.ToString(), Color.FromArgb(1, 255, 0, 0));
        }
        private List<AlarmInfo> Alarms { get; set; } = new List<AlarmInfo>();
        ConcurrentDictionary<string, bool> flagbit = new ConcurrentDictionary<string, bool>();
        public ConcurrentDictionary<string, Color> colorValue = new ConcurrentDictionary<string, Color>();
        public Action<string> AddAction { get; set; }
        public Action<string> RemoveAction { get; set; }
        public Action ChangeAction { get; set; }

        /// <summary>
        /// 沿报警检测
        /// </summary>
        /// <param name="Trigger">触发变量</param>
        /// <param name="text">报警信息</param>
        /// <param name="edgeType">触发类型，上升沿 或 下降沿</param>
        public void EdgeAlarm(bool Trigger, string text, AlarmLevel alarmLevel = AlarmLevel.一般报警, AlarmTriggerType edgeType = AlarmTriggerType.Rising)
        {
            if (!flagbit.ContainsKey(text)) flagbit.TryAdd(text, false);
            if (edgeType == AlarmTriggerType.Rising ? Trigger : !Trigger)
            {
                if (edgeType == AlarmTriggerType.Rising ? !flagbit[text] : flagbit[text])
                {
                    AddAlarm(text, alarmLevel);
                    flagbit[text] = edgeType == AlarmTriggerType.Rising ? true : false;
                }
            }
            else
            {
                RemoveAlarm(text);
            }
            if (edgeType == AlarmTriggerType.Rising ? flagbit[text] : !flagbit[text]) flagbit[text] = Trigger;
        }

        /// <summary>
        /// 获取当前报警信息集合
        /// </summary>
        public List<AlarmInfo> GetAlarmInfo => Alarms;

        /// <summary>
        /// 添加报警信息
        /// </summary>
        /// <param name="AlarmInfo">报警信息</param>
        private void AddAlarm(string AlarmInfo, AlarmLevel alarmLevel)
        {
            if (Alarms.FirstOrDefault(p => p.AlarmInfomation == AlarmInfo) == null)
            {
                Alarms.Add(new AlarmInfo() { AlarmInfomation = AlarmInfo, date = DateTime.Now.ToString("HH:mm:ss"), AlarmLevel = alarmLevel.ToString() });

                //报警频率计算
                if (Json<AlarmHzParameter>.Data.AlarmHz.ContainsKey(AlarmInfo))
                    Json<AlarmHzParameter>.Data.AlarmHz[AlarmInfo]++;
                else
                    Json<AlarmHzParameter>.Data.AlarmHz.TryAdd(AlarmInfo, 1);

                if (AddAction != null) AddAction(AlarmInfo);//添加报警通知
                if (ChangeAction != null) ChangeAction();//更改报警通知
            }
        }

        /// <summary>
        /// 移除报警信息
        /// </summary>
        /// <param name="AlarmInfo">报警信息</param>
        private void RemoveAlarm(string AlarmInfo)
        {
            var result = Alarms.FirstOrDefault(p => p.AlarmInfomation == AlarmInfo);
            if (result != null)
            {
                Alarms.Remove(result);
                if (RemoveAction != null) RemoveAction(AlarmInfo);
                if (ChangeAction != null) ChangeAction();
            }
        }



    }
}
