using FLY.OBJComponents.Common;
using FLY.OBJComponents.IService;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FLY.OBJComponents.Server
{
    /// <summary>
    /// 报警管理系统
    /// </summary>
    public class WarningSystem : INotifyPropertyChanged, IWarningService
    {
        #region IWarningService

        /// <summary>
        /// 使能
        /// </summary>
        public bool Enable { get; set; }

        /// <summary>
        /// 当前正在报警的!!!!!!
        /// </summary>
        public IBuffer<FlyData_WarningHistory> ReasonList { get; }

        /// <summary>
        /// 最近报警列表
        /// </summary>
        public IBuffer<FlyData_WarningHistory> NewestList { get; }

        #endregion



        public WarningSystem()
        {
            Enable = true;
            ReasonList = new Buffer<FlyData_WarningHistory>();
            NewestList = new BufferStorage<FlyData_WarningHistory>("warning_newest.csv");
        }

        public Action ResetEvent;
        public void Reset()
        {
            ResetEvent?.Invoke();
        }
        #region IWarningServiceSimple


        public void Add(byte errcode, string description, ERR_STATE state)
        {
            FlyData_WarningHistory reason = new FlyData_WarningHistory();
            reason.Time = DateTime.Now;

            reason.ErrCode = errcode;
            reason.Description = description;
            reason.State = state;

            FlyData_WarningHistory error = null;
            Buffer<FlyData_WarningHistory> reasonList = ReasonList as Buffer<FlyData_WarningHistory>;
            int error_id=0;
            if (reasonList.Count > 0)
            {
                
                int idx = reasonList.list.FindIndex(e => e.ErrCode == errcode);
                if (idx >= 0)
                {
                    error_id = reasonList.GetID(idx);
                    error = reasonList.list[idx];
                }
            }
            switch (state)
            {
                case ERR_STATE.ON:
                    if (error == null)
                    {
                        ((Buffer<FlyData_WarningHistory>)ReasonList).Add(reason.Clone());

                    }
                    else
                    {
                        //已经有了,再发生,不管它。
                        return;
                    }
                    break;
                case ERR_STATE.OFF:
                    if (error == null)
                    {
                        //没有在报警,不管它。
                        return;
                    }
                    else
                    {
                        error.State = state;
                        ((Buffer<FlyData_WarningHistory>)ReasonList).Remove(error_id);

                    }
                    break;
            }
            ((Buffer<FlyData_WarningHistory>)NewestList).Add(reason);
        }

        #endregion

        /// <summary>
        /// 获取能被交易的属性
        /// </summary>
        /// <returns></returns>
        public string[] GetPropertys()
        {
            return new string[] {
                "Enable"
            };
        }
        #region INotifyPropertyChanged 成员
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

    }
}