using NEIntelligentControl2.Service.AGC;
using System;
using System.Collections.Generic;
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.AGC
{
    /// <summary>
    /// 所有AGC关键信息卡片
    /// </summary>
    public partial class AllAGCInfoCard : UserControl
    {
        // -----------事件-------------
        public static readonly RoutedEvent SelectionChangedEvent = EventManager.RegisterRoutedEvent("SelectionChanged", RoutingStrategy.Bubble, typeof(EventHandler<RoutedEventArgs>), typeof(DynamicAGCCard));
        public event RoutedEventHandler SelectionChanged { add { AddHandler(SelectionChangedEvent, value); } remove { RemoveHandler(SelectionChangedEvent, value); } }

        private Dictionary<string, SimpleAGCCard> _SAGCCardPairt;// 简单AGC卡片集合
        private AGCManager _AGCManager;

        private bool _IsActive;

        public AllAGCInfoCard()
        {
            InitializeComponent();

            _AGCManager = App.ServiceProvider.GetService(typeof(AGCManager)) as AGCManager;
            _SAGCCardPairt = new Dictionary<string, SimpleAGCCard>();

            Init();
        }

        private void Init()
        {
            var vs = _AGCManager.AGCInfos;
            int index = 0;
            foreach (var v in vs)
            {
                if (_SAGCCardPairt.ContainsKey(v.Title)) continue;
                var card = new SimpleAGCCard(v);
                card.Tag = index++;
                card.MouseUp += Card_MouseUp;

                _SAGCCardPairt.Add(v.Title, card);
                _UGMain.Children.Add(card);
            }
        }

        /// <summary>
        /// 刷新数据
        /// </summary>
        internal async void RefreshData()
        {
            while (_IsActive)
            {
                try
                {
                    Dispatcher.Invoke(() =>
                    {
                        foreach (var v in _SAGCCardPairt)
                        {
                            v.Value.PowerActual = v.Value.AGCInfo.AiPoints.Where(p => p.Type == Models.AGC.TagType.ActualPower).FirstOrDefault()?.Value.ToString("f2");
                            v.Value.PowerSet = v.Value.AGCInfo.AiPoints.Where(p => p.Type == Models.AGC.TagType.PowerSet).FirstOrDefault()?.Value.ToString("f2");
                        }
                    });
                }
                catch { }
                await Task.Delay(2000);
            }
        }

        private void Card_MouseUp(object sender, MouseButtonEventArgs e)
        {
            RaiseEvent(new RoutedEventArgs(SelectionChangedEvent, sender));
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            _IsActive = true;
            Task.Run(RefreshData);
        }

        private void UserControl_Unloaded(object sender, RoutedEventArgs e)
        {
            _IsActive = false;
        }
    }
}
