using NEIntelligentControl2.Models;
using NEIntelligentControl2.Models.AGC;
using NEIntelligentControl2.Models.Datas;
using NEIntelligentControl2.Models.Messages;
using NEIntelligentControl2.Models.Pages;
using NEIntelligentControl2.Service.AGC;
using NEIntelligentControl2.Views.AGC;
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.AGC
{
    /// <summary>
    /// AGC页面
    /// </summary>
    public partial class PageAGC : Page
    {
        /// <summary>
        /// 页面是否活跃
        /// </summary>
        private bool _IsActived;
        private bool _IsLoaded;
        private TagManager _TagManager;
        private AGCManager _AGCManager;
        public PageAGC(TagManager tm, AGCManager am)
        {
            InitializeComponent();
            _TagManager = tm;
            _AGCManager = am;
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            _TagManager.TagChanged?.Invoke(new List<Models.Station.StationInfo>());
            if (!_IsLoaded)
            {
                InitCards();
                _IsLoaded = true;
            }
            _IsActived = true;
            Task.Factory.StartNew(RefreshData, TaskCreationOptions.LongRunning);
            Task.Factory.StartNew(RefreshLineData, TaskCreationOptions.LongRunning);
        }

        /// <summary>
        /// 曲线数据刷新
        /// </summary>
        private async void RefreshLineData()
        {
            while (_IsLoaded)
            {
                _AGCManager.RefreshLineData(this);
                await Task.Delay(30000);
            }
        }

        /// <summary>
        /// 数据刷新
        /// </summary>
        private async void RefreshData()
        {
            while (_IsActived)
            {
                _AGCManager.RefreshData(this);
                await Task.Delay(2000);
            }
        }

        private void InitCards()
        {
            int i = 0;
            var agcInfos = _AGCManager.AGCInfos;
            if (agcInfos == null) return;
            while (i < agcInfos.Count)
            {
                var vs = agcInfos.Where(ai => ai.Coordinate.X == i).ToList();
                if (vs.Count <= 0)
                {
                    break;
                }
                _GMain.RowDefinitions.Add(new RowDefinition());
                Grid gd = new Grid();
                foreach (var v in vs)
                {
                    gd.ColumnDefinitions.Add(new ColumnDefinition());
                    AGCCard2 ac = new AGCCard2() { Info = v };
                    gd.Children.Add(ac);
                    Grid.SetColumn(ac, v.Coordinate.Y);
                }
                if (gd.Children.Count < 3)
                {
                    gd.HorizontalAlignment = HorizontalAlignment.Center;
                }
                _GMain.Children.Add(gd);
                Grid.SetRow(gd, i);
                ++i;
            }
            //_GMain.Height = this.ActualHeight / 2 * i;
        }

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

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var bt = sender as Button;
            if (bt == null) return;
            if (bt.Content.ToString() == "光 伏")
            {
                bt.Content = "风 机";
                _sv.ScrollToEnd();
            }
            else
            {
                bt.Content = "光 伏";
                _sv.ScrollToTop();
            }
        }

        private void _sv_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            var v = e.VerticalOffset / e.ViewportHeight;
            _bt.Content = v <= 0.5 ? "光 伏" : "风 机";
        }
    }
}
