using NEIntelligentControl2.Models.Datas;
using NEIntelligentControl2.Models.Messages;
using NEIntelligentControl2.Models.Windturbine;
using NEIntelligentControl2.Service.Windturbine;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
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.Infos
{
    /// <summary>
    /// 测点详情
    /// </summary>
    public partial class PointDetails : UserControl
    {
        private bool _IsActived;
        private List<PointInfo> _AIPoints;
        private List<PointInfo> _DIPoints;
        private InfoManager _InfoManager;

        public WindturbineInfo WindturbineInfo { get; set; }
        public PointDetails()
        {
            InitializeComponent();
            _InfoManager = App.ServiceProvider.GetService(typeof(InfoManager)) as InfoManager;
        }

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

        private async void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            _IsActived = true;
            await InitPointTags();
            Task.Run(RefreshData);
        }

        private async void RefreshData()
        {
            var aiks = _AIPoints.Select(p => p.TagName).ToArray();
            var diks = _DIPoints.Select(p => p.TagName).ToArray();
            while (_IsActived)
            {
                try
                {
                    var vsai = _InfoManager.GetPointDatasByTags(aiks);
                    var vsdi = _InfoManager.GetPointDatasByTags(diks);
                    Dispatcher.Invoke(() =>
                    {
                        UpdateData(vsai, _AIPoints);
                        UpdateData(vsdi, _DIPoints);
                    });
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
                await Task.Delay(2000);
            }
        }

        private void UpdateData(Dictionary<string, TsData> vs, List<PointInfo> ps)
        {
            foreach(var v in ps)
            {
                if (!vs.ContainsKey(v.TagName)) continue;
                v.PointValue = vs[v.TagName].Value;
            }
        }

        private Task InitPointTags()
        {
            return Task.Run(() =>
            {
                if (_AIPoints != null && _AIPoints.Count > 0) return;
                try
                {
                    _AIPoints = _InfoManager.GetAIPoints(WindturbineInfo.WindturbineId);
                    _DIPoints = _InfoManager.GetDIPoints(WindturbineInfo.WindturbineId);
                    Dispatcher.Invoke(() =>
                    {
                        _DGAI.ItemsSource = _AIPoints;
                        _DGDI.ItemsSource = _DIPoints;
                    });
                }
                catch { }
            });
        }

        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Enter) return;
            Search();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            switch (((Control)sender).Tag)
            {
                case "ok":
                    Search();
                    break;
                default:return;
            }
        }

        private void Search()
        {
            string s = _TBSerach.Text;
            if (string.IsNullOrWhiteSpace(s))
            {
                _DGAI.ItemsSource = _AIPoints;
                _DGDI.ItemsSource = _DIPoints;
                return;
            }
            Task.Run(() =>
            {
                var vs1 = _AIPoints.Where(p => p.PointName.Contains(s)).ToList();
                var vs2 = _DIPoints.Where(p => p.PointName.Contains(s)).ToList();
                Dispatcher.Invoke(() =>
                {
                    _DGAI.ItemsSource = vs1;
                    _DGDI.ItemsSource = vs2;
                });
            });
        }
    }
}
