using NEIntelligentControl2.Models.Windturbine;
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.Navigation;
using System.Windows.Shapes;

namespace NEIntelligentControl2.Views.Matrix
{
    /// <summary>
    /// 关注风机
    /// </summary>
    public partial class FollowedMatrix : UserControl
    {
        private bool _IsActive;                                 // 此页面是否活跃
        private Dictionary<string, WindBlockSmall> _BlockPairs;  // 数据集合
        public FollowedMatrix()
        {
            InitializeComponent();
            _BlockPairs = new Dictionary<string, WindBlockSmall>();
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        internal void UpdateData(Dictionary<string, WindturbineInfo> vs)
        {
            try
            {
                Dispatcher.Invoke(() =>
                {
                    foreach (var v in _BlockPairs)
                    {
                        if (!vs.ContainsKey(v.Key)) continue;
                        v.Value.Update(vs[v.Key]);
                    }
                });
            }
            catch { }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            _IsActive = true;
            Task.Factory.StartNew(RefreshData, TaskCreationOptions.LongRunning);
        }

        /// <summary>
        /// 刷新数据
        /// </summary>
        private async void RefreshData()
        {
            while (_IsActive)
            {
                try
                {
                    Refresh();
                    await Task.Delay(3000);
                }
                catch { }
            }
        }

        /// <summary>
        /// 刷新数据
        /// </summary>
        private void Refresh()
        {
            try
            {
                /*var vs = RestfulClient.GetWindturbineFollows();
                if (vs == null) return;
                HashSet<string> set = new HashSet<string>();
                foreach (var v in vs)
                {
                    set.Add(v.WindturbineID);
                    if (!_BlockPairs.ContainsKey(v.WindturbineID))
                    {
                        this.Dispatcher.Invoke(() =>
                        {
                            BlockSmallWPF bs = new BlockSmallWPF();
                            _BlockPairs.Add(v.WindturbineID, bs);
                            _UGMain.Children.Add(bs);
                        });
                    }
                }
                List<BlockSmallWPF> ll = new List<BlockSmallWPF>();
                foreach (var v in _BlockPairs)
                {
                    if (v.Value.WindturbineInfo != null && !set.Contains(v.Value.WindturbineInfo.WindturbineId))
                    {
                        ll.Add(v.Value);
                    }
                }
                this.Dispatcher.Invoke(() =>
                {
                    foreach (var v in ll)
                    {
                        _BlockPairs.Remove(v.WindturbineInfo.WindturbineId);
                        _UGMain.Children.Remove(v);
                    }
                });*/
            }
            catch { }
        }

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

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            switch (((Control)sender).Tag)
            {
                case "removefollow":// 取消关注
                    RemoveFollow();
                    break;
            }
        }

        /// <summary>
        /// 取消关注
        /// </summary>
        private void RemoveFollow()
        {
            try
            {
                var ls = GetSelectedIds();
                //var s = ls.GetString(v => v.WindturbineInfo.WindturbineId);
                //RestfulClient.RemoveWindturbineFollows(s);
                Refresh();
            }
            catch (Exception e)
            {
                Console.WriteLine($"取消关注出现错误：{e.Message}");
            }
        }

        /// <summary>
        /// 获取被选中的风机
        /// </summary>
        /// <returns></returns>
        private List<WindBlockSmall> GetSelectedIds()
        {
            List<WindBlockSmall> ls = new List<WindBlockSmall>();
            foreach (var v in _BlockPairs)
            {
                if (v.Value.IsSelected)
                {
                    ls.Add(v.Value);
                }
            }
            return ls;
        }
    }
}
