using NEIntelligentControl2.Models.Datas;
using NEIntelligentControl2.Models.Messages;
using NEIntelligentControl2.Models.Pages;
using NEIntelligentControl2.Models.Windturbine;
using NEIntelligentControl2.Service.WebSocket;
using NEIntelligentControl2.Views.Matrix;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Security.Policy;
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.Home
{
    /// <summary>
    /// 风机AGC融合页面
    /// </summary>
    public partial class PageWindturbineAGC : Page
    {
        private readonly IPageAction _IPageAction;
        private readonly WEBHelper _WEBHelper;

        private MessageBridge _MessageBridge;// websocket

        private WindturbineInfoBridge _WindturbineInfoBridge;// 风机信息数据桥
        /// <summary>
        /// 矩阵块
        /// </summary>
        private List<WindBlock> _WindBlocks;

        private bool _IsLoaded;

        private string RFDLTag = "";
        private string MBGLTag = "";
        private string SSGLTag = "";

        private string YFDLTag = "";
        private string NFDLTag = "";
        private string LLGLTag = "";

        private string _Url = "";
        public PageWindturbineAGC(IPageAction action, MessageBridge mb, WEBHelper web)
        {
            InitializeComponent();
            _IPageAction = action;
            _MessageBridge = mb;
            _WEBHelper = web;
            _WindturbineInfoBridge = new WindturbineInfoBridge() { Messaged = OnWindturbineMessage };

            _tbtitle1.Text = "信\n友\n一\n线";
            _tbtitle2.Text = "信\n友\n二\n线";
            _tbtitle3.Text = "信\n友\n三\n线";
            _Url = ConfigurationManager.AppSettings["DataServicePathDebug"];
            RFDLTag = ConfigurationManager.AppSettings["RFDLTag"];
            MBGLTag = ConfigurationManager.AppSettings["MBGLTag"];
            SSGLTag = ConfigurationManager.AppSettings["SSGLTag"];

            YFDLTag = ConfigurationManager.AppSettings["YFDLTag"];
            NFDLTag = ConfigurationManager.AppSettings["NFDLTag"];
            LLGLTag = ConfigurationManager.AppSettings["LLGLTag"];
        }

        private void OnWindturbineMessage(Dictionary<string, WindturbineInfo> obj)
        {
            if (!_IsLoaded)
            {
                InitWindturbine(obj);
                return;
            }
            Dispatcher.Invoke(() =>
            {
                foreach (var v in _WindBlocks)
                {
                    if (!obj.ContainsKey(v.WindturbineId)) continue;
                    v.Update(obj[v.WindturbineId]);
                }
            });
        }

        private void InitWindturbine(Dictionary<string, WindturbineInfo> obj)
        {
            if (obj == null || obj.Count == 0) return;
            _IsLoaded = true;
            _WindBlocks = new List<WindBlock>();
            var ls = obj.Values.ToList();
            ls.Sort((x, y) => x.ID - y.ID);
            Dispatcher.Invoke(() =>
            {
                foreach (var v in ls)
                {
                    WindBlock bs = new WindBlock(v) { WindturbineId = v.WindturbineId };
                    bs.Update(v);
                    _WindBlocks.Add(bs);
                    if ((v.ID >= 1 && v.ID <= 5) || (v.ID >= 11 && v.ID <= 13))
                    {
                        pb1.AddWindturbine(bs);
                    }
                    else if ((v.ID >= 6 && v.ID <= 10) || (v.ID >= 16 && v.ID <= 19))
                    {
                        pb2.AddWindturbine(bs);
                    }
                    else
                    {
                        pb3.AddWindturbine(bs);
                    }
                }
            });
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            //var page = _IPageAction["Matrix.PageMatrix"];
            //_FrameMatrix.Navigate(page);

            var page2 = _IPageAction["AGC.PageAGC"];
            _FrameAGC.Navigate(page2);


            _MessageBridge.Register(_WindturbineInfoBridge);

            Task.Run(RefresData);
        }

        private async void RefresData()
        {
            string url = $"{_Url}/ts/latest?keys={SSGLTag},{MBGLTag},{YFDLTag},{NFDLTag},{LLGLTag}";
            while (true)
            {
                try
                {
                    //
                    var vs = _WEBHelper.HttpGetJSON<Dictionary<string, TsData>>(url);
                    var vs2 = _WEBHelper.HttpGetJSON<Dictionary<string, TsData>>($"{_Url}/ts/latest?keys={RFDLTag}");
                    var val = vs2.Values.Sum(t => t.Value);

                    var nf = _WEBHelper.HttpGetJSON<Dictionary<string, TsData>>($"{_Url}/ts/latest?keys={NFDLTag}");
                    var yf = _WEBHelper.HttpGetJSON<Dictionary<string, TsData>>($"{_Url}/ts/latest?keys={YFDLTag}");

                    if (vs == null || vs.Count == 0) continue;
                    Dispatcher.Invoke(() =>
                    {
                        foreach (var v in vs)
                        {
                            if (v.Key == SSGLTag)
                            {
                                tbps.Text = Math.Round(v.Value.Value, 2).ToString("0.00");
                            }
                            if (v.Key == MBGLTag)
                            {
                                tbpc.Text = Math.Round(v.Value.Value, 2).ToString("0.00");
                            }
                            if (v.Key == LLGLTag)
                            {
                                tbllgl.Text = Math.Round(v.Value.Value, 2).ToString("0.00");
                            }
                        }
                        var nval = nf.Values.Sum(t => t.Value);
                        tbly.Text = Math.Round(nval / 50, 2).ToString("0.00");
                        tbrf.Text = Math.Round(val, 2).ToString("0.00");

                        tbyf.Text = Math.Round(yf.Values.Sum(t => t.Value), 2).ToString("0.00");
                        tbnf.Text = Math.Round(nval, 2).ToString("0.00");

                        var date = DateTime.Parse("2023-2-18");
                        var time = (int)(DateTime.Now - date).TotalDays;
                        aqts.Text = $"{619 + time} 天";
                    });
                }
                catch { }
                await Task.Delay(1000);
            }
        }
    }
}
