using NEIntelligentControl2.Models.Alarm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace NEIntelligentControl2.Views.Alarm
{
    /// <summary>
    /// 报警首页卡片
    /// </summary>
    public partial class AlarmCard : UserControl
    {
        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(AlarmCard));
        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get => GetValue(TitleProperty) as string; set => SetValue(TitleProperty, value); }
        /// <summary>
        /// 是否是风机
        /// </summary>
        public bool IsWindtubine { get; set; }

        private ObservableCollection<AlarmInfos> _List; // 报警集合
        private HashSet<string> _AlarmIDs;              // 报警ID集合
        public AlarmCard()
        {
            InitializeComponent();
            _List = new ObservableCollection<AlarmInfos>();
            _DGMain.ItemsSource = _List;
            _AlarmIDs = new HashSet<string>();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!IsWindtubine)
            {
                _DGMain.Columns[1].Visibility = Visibility.Collapsed;
            }
        }

        /// <summary>
        /// 添加升压站历史报警
        /// </summary>
        internal void UpdateAlarmData(AlarmPage ap)
        {
            if (ap == null || ap.Records == null) return;
            var vs = ap.Records.ToDictionary(v => v.Id, v => v);
            Dispatcher.Invoke(() =>
            {
                if (_List.Count > 100) return;
                foreach (var v in ap.Records)
                {
                    var ai = AlarmInfos.GetAlarmInfo(v);
                    if (_AlarmIDs.Contains(ai.Feature)) continue;
                    _AlarmIDs.Add(ai.Feature);
                    _List.Add(ai);
                }
            });
        }

        /// <summary>
        /// 更新报警列表
        /// </summary>
        /// <param name="fi">报警数据</param>
        internal void UpdateAlarmData(FaultInfo fi)
        {
            AlarmInfos ai = AlarmInfos.GetAlarmInfo(fi);
            if (_AlarmIDs.Contains(ai.Feature)) return;

            AddData(ai);
        }

        /// <summary>
        /// 更新报警列表
        /// </summary>
        /// <param name="v">数据</param>
        internal void UpdateAlarmData(AlarmInfo v)
        {
            AlarmInfos ai = AlarmInfos.GetAlarmInfo(v);
            if (_AlarmIDs.Contains(ai.Feature)) return;

            AddData(ai);
        }

        /// <summary>
        /// 将数据添加到列表中
        /// </summary>
        /// <param name="ai">报警数据</param>
        private void AddData(AlarmInfos ai)
        {
            Dispatcher.Invoke(() =>
            {
                _List.Insert(0, ai);
                _AlarmIDs.Add(ai.Feature);

                if (_List.Count > 100)
                {
                    var v = _List.Last();
                    if (_AlarmIDs.Count > 5000)
                        _AlarmIDs.Remove(v.Feature);
                    _List.Remove(v);
                }
            });
        }
    }



    /// <summary>
    /// 报警信息
    /// </summary>
    public class AlarmInfos : INotifyPropertyChanged
    {
        /// <summary>
        /// ID
        /// </summary>
        public long ID { get; set; }
        /// <summary>
        /// 报警时间
        /// </summary>
        public string Time { get; set; }
        /// <summary>
        /// 报警设备名称
        /// </summary>
        public string Title { get; set; }
        private string message;
        /// <summary>
        /// 报警信息
        /// </summary>
        public string Message { get => message; set { message = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Message")); } }

        /// <summary>
        /// 报警级别
        /// </summary>
        public string Rank { get; set; }
        /// <summary>
        /// 报警类型
        /// </summary>
        public string AlarmType { get; set; }
        private string feature;
        /// <summary>
        /// 报警特征
        /// </summary>
        public string Feature { get => feature ?? (feature = $"{Time}{Title}{Message}{AlarmType}"); }

        public event PropertyChangedEventHandler PropertyChanged;

        internal static AlarmInfos GetAlarmInfo(AlarmInfo value)
        {
            AlarmInfos ai = new AlarmInfos()
            {
                ID = value.Id,
                Time = value.LastUpdateTime != null ?
                value.LastUpdateTime?.ToString("yyyy-MM-dd HH:mm:ss") : value.AlertTime?.ToString("yyyy-MM-dd HH:mm:ss"),
                Title = value.WindturbineName,
                Message = value.AlertText,
                Rank = value.Rank,
                AlarmType = value.MessageType == 1 ? "触发" : "解除"
            };
            return ai;
        }

        /// <summary>
        /// 获取报警数据
        /// </summary>
        internal static AlarmInfos GetAlarmInfo(FaultInfo fi)
        {
            AlarmInfos ai = new AlarmInfos()
            {
                ID = fi.Id,
                Time = fi.FaultTime?.ToString("yyyy-MM-dd HH:mm:ss"),
                Title = fi.WindturbineName,
                Message = fi.AlertText,
                Rank = fi.Rank,
                AlarmType = fi.MessageType == 1 ? "触发" : "解除"
            };
            return ai;
        }
    }
}
