using NEIntelligentControl2.Models.Datas;
using NEIntelligentControl2.Models.Messages;
using NEIntelligentControl2.Service.Station;
using NEIntelligentControl2.Service.WebSocket;
using SVGViewer.Models.Info;
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.Shapes;

namespace NEIntelligentControl2.Windows.BoostStation
{
    /// <summary>
    /// 升压站二次图窗口
    /// </summary>
    public partial class SubStationWindow : Window
    {
        /// <summary>
        /// 二次图id
        /// </summary>
        private string _SubId;
        private StationManager _StationManager;
        private UrlManager _UrlManager;
        private WEBHelper _WEBHelper;
        private Dictionary<string, List<TagInfo>> _TagInfos = new Dictionary<string, List<TagInfo>>();
        private string _Url;
        private bool _IsLoaded;
        private Dictionary<string, ShapeInfo> _CustomizeShapes;
        public SubStationWindow(string subid)
        {
            InitializeComponent();
            _SubId = subid;
            _StationManager = App.ServiceProvider.GetService(typeof(StationManager)) as StationManager;
            _UrlManager = App.ServiceProvider.GetService(typeof(UrlManager)) as UrlManager;
            _WEBHelper = App.ServiceProvider.GetService(typeof(WEBHelper)) as WEBHelper;
            Init();
        }

        private async void Init()
        {
            if (string.IsNullOrWhiteSpace(_SubId)) return;
            ShapeInfo si = await _StationManager.GetSubBoostStation(_SubId);
            if (si == null) return;

            _CustomizeShapes = _StationManager.GetCustomizeShapesDic();


            _svg.InitInfo(si, _CustomizeShapes);
            this.Title = si.Name;

            var taginfos = si.GetAllTagInfos();
            _TagInfos = taginfos.GroupBy(v => v.Tag).ToDictionary(v => v.Key, v => v.ToList());

            var keys = string.Join(",", _TagInfos.Keys);
            _Url = _UrlManager.DataServicePath + $"/ts/latest?keys={keys}";
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _IsLoaded = true;
            Task.Factory.StartNew(RefreshData);
        }


        private async void RefreshData()
        {
            while (_IsLoaded)
            {
                try
                {
                    var vs = _WEBHelper.HttpGetJSON<Dictionary<string, TsData>>(_Url);
                    if (vs != null && vs.Count > 0)
                    {
                        Dispatcher?.Invoke(() =>
                        {
                            foreach (var v in vs)
                            {
                                if (!_TagInfos.ContainsKey(v.Key)) continue;
                                var tgs = _TagInfos[v.Key];
                                foreach (var tg in tgs)
                                {
                                    tg.Value = v.Value.Value;
                                }
                            }
                        });
                    }
                }
                catch { }

                await Task.Delay(2000);
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _IsLoaded = false;
        }
    }
}
