using NEIntelligentControl2.Models.Windturbine;
using NEIntelligentControl2.Models.WinForms;
using NEIntelligentControl2.Service.Windturbine;
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.Controls.Primitives;
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 WindturbineDetails : UserControl
    {
        private InfoManager _InfoManager;
        private Dictionary<string,GroupBox> _GroupBoxs;
        private List<PointTag> _PointTags;
        private IList<UniformCodeInfo> _UniformCodeInfos;
        public WindturbineInfo WindturbineInfo { get; set; }
        private bool _IsActived;
        private bool _IsLoaded;
        public WindturbineDetails()
        {
            InitializeComponent();
            _InfoManager = App.ServiceProvider.GetService(typeof(InfoManager)) as InfoManager;
            InitGroupBox();
        }

        private void InitGroupBox()
        {
            _GroupBoxs = new Dictionary<string, GroupBox>();
            foreach(var v in _GdMain.Children)
            {
                var gd = v as Grid;
                if (gd == null) continue;
                foreach(var vv in gd.Children)
                {
                    GroupBox gb = vv as GroupBox;
                    if(gb == null) continue;
                    _GroupBoxs.Add(gb.Tag as string, gb);
                }
            }
        }

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

        private async void RefreshData()
        {
            if (_UniformCodeInfos == null) return;
            string[] ucs = _UniformCodeInfos.Select(u => u.UniformCode).ToArray();
            while (_IsActived)
            {
                try
                {
                    var vs = _InfoManager.GetPointDatas(WindturbineInfo.WindturbineId, ucs);
                    Dispatcher.Invoke(() =>
                    {
                        foreach(var v in _PointTags)
                        {
                            v.UpdateData(vs);
                        }
                    });
                }
                catch { }
                await Task.Delay(2000);
            }
        }

        private void InitTags()
        {
            if (_IsLoaded) return;
            _PointTags = new List<PointTag>();
            var vs = _InfoManager.GetUniformCode(WindturbineInfo.StationId, WindturbineInfo.ModelId);
            if (vs == null) return;
            _UniformCodeInfos = vs;
            foreach (var v in vs)
            {
                if (!_GroupBoxs.ContainsKey(v.PartIndex)) continue;
                UniformGrid ug = _GroupBoxs[v.PartIndex].Content as UniformGrid;
                PointTag pt = new PointTag(v, WindturbineInfo.WindturbineId);
                ug.Children.Add(pt);
                _PointTags.Add(pt);
            }
            _IsLoaded = true;
        }

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