using NEIntelligentControl2.Models.Alarm;
using NEIntelligentControl2.Service.Alarm;
using NEIntelligentControl2.Windows.Alarm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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 RealTimeFalutInfoView : UserControl
    {
        // ------------依赖属性--------------
        public static DependencyProperty IsFixedProperty = DependencyProperty.Register("IsFixed", typeof(bool), typeof(RealTimeFalutInfoView));
        public static DependencyProperty IsFaultProperty = DependencyProperty.Register("IsFault", typeof(bool), typeof(RealTimeFalutInfoView));
        public static DependencyProperty IsOpenedProperty = DependencyProperty.Register("IsOpened", typeof(bool), typeof(RealTimeFalutInfoView));
        /// <summary>
        /// 是否固定
        /// </summary>
        public bool IsFixed { get => (bool)GetValue(IsFixedProperty); set => SetValue(IsFixedProperty, value); }
        /// <summary>
        /// 是否是故障
        /// </summary>
        public bool IsFault { get => (bool)GetValue(IsFaultProperty); set => SetValue(IsFaultProperty, value); }
        /// <summary>
        /// 是否打开
        /// </summary>
        public bool IsOpened { get => (bool)GetValue(IsOpenedProperty); set => SetValue(IsOpenedProperty, value); }

        private ObservableCollection<FaultInfoModel> _FaultList;
        private HashSet<long> _FaultIds;
        private ObservableCollection<AlarmInfo> _AlarmList;
        private HashSet<long> _AlarmIds;

        private AlarmManager _AlarmManager;
        public RealTimeFalutInfoView()
        {
            InitializeComponent();
            IsFault = true;
            _FaultList = new ObservableCollection<FaultInfoModel>();
            _GDFault.ItemsSource = _FaultList;
            _AlarmList = new ObservableCollection<AlarmInfo>();
            _GDAlarm.ItemsSource = _AlarmList;
            _AlarmIds = new HashSet<long>();
            _FaultIds = new HashSet<long>();
            _AlarmManager = App.ServiceProvider.GetService(typeof(AlarmManager)) as AlarmManager;
            _AlarmManager.OnFaultCount += OnFaultCount;
            _AlarmManager.OnAlarmCount += OnAlarmCount;
        }

        private void OnAlarmCount(List<AlarmInfo> obj)
        {
            var ls = obj.Select(f => f.Id).ToList();
            var removed = _AlarmList.Where(f => !ls.Contains(f.Id)).ToList();
            Dispatcher.Invoke(() =>
            {
                for (int i = obj.Count - 1; i >= 0; i--)
                {
                    var v = obj[i];
                    if (_AlarmIds.Contains(v.Id)) continue;
                    _AlarmList.Insert(0, v);
                    _AlarmIds.Add(v.Id);
                }
                foreach (var v in removed)
                {
                    _AlarmList.Remove(v);
                    _AlarmIds.Remove(v.Id);
                }
            });
        }

        private void OnFaultCount(List<FaultInfoModel> obj)
        {
            var ls = obj.Select(f => f.FaultInfo.Id).ToList();
            var removed = _FaultList.Where(f => !ls.Contains(f.FaultInfo.Id)).ToList();
            Dispatcher.Invoke(() =>
                {
                    for (int i = obj.Count - 1; i >= 0; i--)
                    {
                        var v = obj[i];
                        if (_FaultIds.Contains(v.FaultInfo.Id)) continue;
                        _FaultList.Insert(0, v);
                        _FaultIds.Add(v.FaultInfo.Id);
                    }
                    foreach (var v in removed)
                    {
                        _FaultList.Remove(v);
                        _FaultIds.Remove(v.FaultInfo.Id);
                    }
                });
        }

        private void cc_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            IsFixed = !IsFixed;
        }

        private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var v = ((FrameworkElement)sender).DataContext as FaultInfoModel;
            if (v == null || v.FaultInfo == null) return;
            Windows.Alarm.DeviceDetailAlarmWindow.Show(v.FaultInfo);
            IsOpened = false;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            switch (((Control)sender).Tag)
            {
                case "fault": // 报警故障切换
                    IsFault = !IsFault;
                    break;
                case "rtalarm":// 实时报警
                    RealTimeAlarm();
                    break;
                case "hisalarm": // 历史报警
                    HistoryAlarm();
                    break;
                case "hisfault": // 历史故障
                    HistoryFault();
                    break;
                default:return;
            }
        }

        /// <summary>
        /// 历史故障
        /// </summary>
        private void HistoryFault()
        {
            HistoryFaultWindow.ShowWindow();
        }

        /// <summary>
        /// 历史报警
        /// </summary>
        private void HistoryAlarm()
        {
            HistoryAlarmWindow.ShowWindow();
        }

        /// <summary>
        /// 实时报警
        /// </summary>
        private void RealTimeAlarm()
        {
            RealTimeAlarmWindow.ShowWindow();
        }
    }
}
