using NEIntelligentControl2.Models.AGC;
using NEIntelligentControl2.Models.Messages;
using NEIntelligentControl2.Service.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.Views.AGC
{
    /// <summary>
    /// 动态AGC卡片页面
    /// </summary>
    public partial class DynamicAGCCard : UserControl
    {
        private Dictionary<int, AGCCard2> _AGCCardsPairt;     // AGC卡片集合
        private AGCCard2 _CurrentCard;                        // 当前卡片
        private int _CurrentIndex;                                  // 当前序列
        private bool _IsActived;                                     // 是否正在运行
        private AGCManager _AGCManager;
        private bool _IsLoaded;
        public DynamicAGCCard()
        {
            InitializeComponent();

            _AGCCardsPairt = new Dictionary<int, AGCCard2>();
            _AGCManager = App.ServiceProvider.GetService(typeof(AGCManager)) as AGCManager;

        }

        private void InitCard()
        {
            var agcInfos = _AGCManager.AGCInfos;
            if (agcInfos == null) return;
            int i = 0;
            foreach(var v in agcInfos)
            {
                AGCCard2 ac = new AGCCard2() { Info = v, Visibility = Visibility.Collapsed };
                _GDMain.Children.Add(ac);
                _AGCCardsPairt.Add(i, ac);
                ++i;
            }

            _DVMain.Count = agcInfos.Count;
            _DVMain.Check(0);
        }

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

        /// <summary>
        /// 自动切换AGC卡片
        /// </summary>
        private async void AutoCheck()
        {
            while (_IsActived)
            {
                await Task.Delay(7000);
                Dispatcher.Invoke(() => Button_Click(_BTRight, null));
            }
        }

        public void ChangeCard(int index)
        {
            if (index < 0 || index >= _DVMain.Count) return;
            _CurrentIndex = index;
            _DVMain.Check(index);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            int i = ((Control)sender).Tag as string == "leftbutton" ? -1 : 1;
            _CurrentIndex += i;
            if (_CurrentIndex < 0) _CurrentIndex = _DVMain.Count - 1;
            if (_CurrentIndex >= _DVMain.Count) _CurrentIndex = 0;
            _DVMain.Check(_CurrentIndex);
        }

        /// <summary>
        /// 小圆点选择改变事件（此处引用虽数然为0，但这个函数处于使用状态，除非您知道在做什么，否则请勿删！）
        /// </summary>
        private void DotView_Changed(object sender, DotChangedRoutedEventArgs e)
        {
            if (_CurrentCard != null)
            {
                _CurrentCard.Visibility = Visibility.Collapsed;
            }
            if (_AGCCardsPairt.ContainsKey(e.Index))
            {
                _AGCCardsPairt[e.Index].Visibility = Visibility.Visible;
                _CurrentCard = _AGCCardsPairt[e.Index];
                _CurrentIndex = e.Index;
            }
        }

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

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            _IsActived = true;
            if (!_IsLoaded)
            {
                InitCard();
            }
            //Task.Factory.StartNew(AutoCheck, TaskCreationOptions.LongRunning);          // 自动切换AGC卡片
            _IsLoaded = true;
            Task.Factory.StartNew(RefreshData, TaskCreationOptions.LongRunning);        // 自动刷新AGC数据
            Task.Factory.StartNew(RefreshLineData, TaskCreationOptions.LongRunning);   // 自动刷新AGC图表数据
        }
    }
}
