using NEIntelligentControl2.Models.BoostStation;
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.Configuration;
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.Pages.BoostStation
{
    /// <summary>
    /// 升压站页面组态版
    /// </summary>
    public partial class PageStation2 : Page
    {
        private ShapeInfo shapeInfo;
        private bool _IsLoaded;
        /// <summary>
        /// 数据地址
        /// </summary>
        private string _Url = "";
        public ShapeInfo ShapeInfo { get => shapeInfo; internal set { shapeInfo = value; InitShape(); } }

        private Dictionary<string, List<TagInfo>> _TagInfos = new Dictionary<string, List<TagInfo>>();

        private WEBHelper _WEBHelper;
        private UrlManager _UrlManager;
        private StationManager _StationManager;
        public PageStation2(WEBHelper web, UrlManager um, StationManager sm)
        {
            InitializeComponent();
            _WEBHelper = web;
            _UrlManager = um;
            _StationManager = sm;
        }

        /// <summary>
        /// 初始化图形
        /// </summary>
        private void InitShape()
        {
            _svg.InitInfo(shapeInfo, _StationManager.GetCustomizeShapesDic(), false);

            _svg.Canvas.SwitchEditer(new BoostStationEventEditer());

            var taginfos = shapeInfo.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 Page_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;
                                }
                            }
                        });
                    }
                    Console.WriteLine($"刷新：{shapeInfo.Id}");
                }
                catch { }

                await Task.Delay(2000);
            }
        }

        private void Page_Unloaded(object sender, RoutedEventArgs e)
        {
            _IsLoaded = false;
        }
    }
}
