using System;
using System.Collections.Generic;
using System.Linq;
using CCDCount.MODEL.AlarmModel;

namespace CCDCount.DLL.AlarmTools
{
    public static class SystemAlarm
    {
        private static List<AlarmMessModel> AlarmMessList = new List<AlarmMessModel>()
        {
            new AlarmMessModel()
            {
                ID = (int)AlarmMessageList.系统异常,
                AlarmType = "SystemException",
                IsAlarm =false
            },
            new AlarmMessModel()
            {
                ID = (int)AlarmMessageList.相机实例创建失败,
                AlarmType = "SystemException",
                IsAlarm =false
            },
            new AlarmMessModel()
            {
                ID = (int)AlarmMessageList.开启相机失败,
                AlarmType = "SystemException",
                IsAlarm =false
            },
            new AlarmMessModel()
            {
                ID = (int)AlarmMessageList.相机采集开始失败,
                AlarmType = "SystemException",
                IsAlarm =false
            },
            new AlarmMessModel()
            {
                ID = (int)AlarmMessageList.相机采集停止失败,
                AlarmType = "SystemException",
                IsAlarm =false
            },
            new AlarmMessModel()
            {
                ID = (int)AlarmMessageList.识别线程启动失败,
                AlarmType = "SystemException",
                IsAlarm =false
            },
            new AlarmMessModel()
            {
                ID = (int)AlarmMessageList.识别线程停止失败,
                AlarmType = "SystemException",
                IsAlarm =false
            },
            new AlarmMessModel()
            {
                ID = (int)AlarmMessageList.待识别队列数据堆积,
                AlarmType = "SystemException",
                IsAlarm =false
            },
            new AlarmMessModel()
            {
                ID = (int)AlarmMessageList.PLC通讯连接失败,
                AlarmType = "SystemException",
                IsAlarm =false
            },
            new AlarmMessModel()
            {
                ID = (int)AlarmMessageList.结果发送线程启动失败,
                AlarmType = "SystemException",
                IsAlarm =false
            },
            new AlarmMessModel()
            {
                ID = (int)AlarmMessageList.结果发送线程停止失败,
                AlarmType = "SystemException",
                IsAlarm =false
            },
            new AlarmMessModel()
            {
                ID = (int)AlarmMessageList.未检测到相机,
                AlarmType = "SystemException",
                IsAlarm =false
            },
            new AlarmMessModel()
            {
                ID = (int)AlarmMessageList.相机加载失败,
                AlarmType = "SystemException",
                IsAlarm =false
            },
            new AlarmMessModel()
            {
                ID = (int)AlarmMessageList.数据分配线程启动失败,
                AlarmType = "SystemException",
                IsAlarm =false
            },
            new AlarmMessModel()
            {
                ID = (int)AlarmMessageList.数据分配线程停止失败,
                AlarmType = "SystemException",
                IsAlarm =false
            },
        };

        /// <summary>
        /// 开启指定警告
        /// </summary>
        /// <param name="ID"></param>
        public static void AlarmAlert(AlarmMessageList ID,string Message, string AlarmPath)
        {
            var alarm = AlarmMessList.Find(x => x.ID == (int)ID);
            if (alarm == null || alarm.IsAlarm)
            {
                return;
            }
            else
            {
                var ChanegAlarm = AlarmMessList.Find(x => x.ID == (int)ID);
                ChanegAlarm.IsAlarm = true;
                ChanegAlarm.AlarmDateTime = DateTime.Now;
                ChanegAlarm.AlarmPath = AlarmPath;
                ChanegAlarm.AlarmMess = Message;

                FaultLog.RecordErrorMessage(alarm.AlarmMess,ChanegAlarm.AlarmPath);
            }
        }

        /// <summary>
        /// 关闭指定警告
        /// </summary>
        public static void AlarmCancel(AlarmMessageList ID)
        {
            AlarmMessList.Find(x => x.ID == (int)ID).IsAlarm = false;
        }

        /// <summary>
        /// 获取所有警告
        /// </summary>
        public static List<AlarmMessModel> GetAllAlarm()
        {
            return AlarmMessList;
        }

        /// <summary>
        /// 获取所有已触发警告
        /// </summary>
        public static List<AlarmMessModel> GetAlarm()
        {
            return AlarmMessList.Where(x => x.IsAlarm == true).ToList();
        }
    }
}
