using NEIntelligentControl2.Models.Matrix;
using NEIntelligentControl2.Models.Pages;
using NEIntelligentControl2.Models.Station;
using NEIntelligentControl2.Models.Windturbine;
using NEIntelligentControl2.Windows;
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.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 WindStationBlock : UserControl, IStationInfo
    {
        // --------------依赖属性----------------
        public static DependencyProperty CountTotalProperty = DependencyProperty.Register("CountTotal", typeof(int), typeof(WindStationBlock));
        public static DependencyProperty CountConnectProperty = DependencyProperty.Register("CountConnect", typeof(int), typeof(WindStationBlock));
        public static DependencyProperty CountStandbyProperty = DependencyProperty.Register("CountStandby", typeof(int), typeof(WindStationBlock));
        public static DependencyProperty CountStoppedProperty = DependencyProperty.Register("CountStopped", typeof(int), typeof(WindStationBlock));
        public static DependencyProperty CountMaintainProperty = DependencyProperty.Register("CountMaintain", typeof(int), typeof(WindStationBlock));
        public static DependencyProperty CountFaultProperty = DependencyProperty.Register("CountFault", typeof(int), typeof(WindStationBlock));
        public static DependencyProperty CountOfflineProperty = DependencyProperty.Register("CountOffline", typeof(int), typeof(WindStationBlock));

        /// <summary>
        /// 总接入台数
        /// </summary>
        public int CountTotal { get => (int)GetValue(CountTotalProperty); set => SetValue(CountTotalProperty, value); }
        /// <summary>
        /// 并网台数
        /// </summary>
        public int CountConnect { get => (int)GetValue(CountConnectProperty); set => SetValue(CountConnectProperty, value); }
        /// <summary>
        /// 待机台数
        /// </summary>
        public int CountStandby { get => (int)GetValue(CountStandbyProperty); set => SetValue(CountStandbyProperty, value); }
        /// <summary>
        /// 停机台数
        /// </summary>
        public int CountStopped { get => (int)GetValue(CountStoppedProperty); set => SetValue(CountStoppedProperty, value); }
        /// <summary>
        /// 维护台数
        /// </summary>
        public int CountMaintain { get => (int)GetValue(CountMaintainProperty); set => SetValue(CountMaintainProperty, value); }
        /// <summary>
        /// 故障台数
        /// </summary>
        public int CountFault { get => (int)GetValue(CountFaultProperty); set => SetValue(CountFaultProperty, value); }
        /// <summary>
        /// 离线台数
        /// </summary>
        public int CountOffline { get => (int)GetValue(CountOfflineProperty); set => SetValue(CountOfflineProperty, value); }

        /// <summary>
        /// 场站信息
        /// </summary>
        public StationInfo StationInfo { get; set; }

        /// <summary>
        /// 矩阵块
        /// </summary>
        private List<WindBlockSmall> _Blocks;
        private Service.Windturbine.CacheManager _CacheManager;

        public WindStationBlock(Service.Windturbine.CacheManager cm)
        {
            InitializeComponent();
            _CacheManager = cm;
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        public void UpdateData(object obj)
        {
            var datas = obj as Dictionary<string, WindturbineInfo>;
            if (datas == null) return;

            var ls = datas.Values.Where(wi => wi.StationId == StationInfo.Id).ToList();

            if (_Blocks == null)
            {
                InitBlocks(ls);
            }

            CountTotal = ls.Count;
            CountStopped = ls.Where(s => (int)s.Status == 0).ToList().Count;
            CountConnect = ls.Where(s => (int)s.Status == 4).ToList().Count;
            CountStandby = ls.Where(s => (int)s.Status == 2 || s.Status == 3 || s.Status == 1).ToList().Count;
            CountMaintain = ls.Where(s => (int)s.Status == 6).ToList().Count;
            CountFault = ls.Where(s => (int)s.Status == 5).ToList().Count;
            CountOffline = ls.Where(s => (int)s.Status == 7).ToList().Count;

            if (_Blocks == null) return;

            var lockInfo = _CacheManager.CustomLockInfos;
            foreach (var v in _Blocks)
            {
                if (!datas.ContainsKey(v.WindturbineId)) continue;
                if (lockInfo.ContainsKey(v.WindturbineId))
                {
                    v.CustomLockInfo = lockInfo[v.WindturbineId];
                }
                else
                {
                    v.CustomLockInfo = null;
                }
                v.Update(datas[v.WindturbineId]);
            }
        }

        private void InitBlocks(List<WindturbineInfo> ls)
        {
            _Blocks = new List<WindBlockSmall>();
            ls.Sort((x, y) => x.ID - y.ID);
            Dispatcher.Invoke(() =>
            {
                foreach (var v in ls)
                {
                    WindBlockSmall bs = new WindBlockSmall(v) { Title =v.ID.ToString(), WindturbineId = v.WindturbineId };
                    _Blocks.Add(bs);
                    _OPUGMain.Add(bs);
                }
            });
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            string tag = ((Control)sender).Tag as string;
            switch (tag)
            {
                case "Start":       // 启动
                case "Stop":        // 停机
                case "Reset":       // 复位
                case "Maintain":          // 维护
                case "UnMaintain":    // 取消维护
                    SendControl(tag);
                    break;
                case "compar":      // 参数对比
                    Compar();
                    break;
                default: return;
            }
        }

        private void MenuItem_Click_Lock(object sender, RoutedEventArgs e)
        {
            // lockValue
            var type = ((Control)sender).Tag as string;
            if (type == null) return;
            bool b1 = Enum.TryParse(type, out HungType hungType);
            if (!b1) return;

            var bks = GetSelectedItems();
            if (bks.Count <= 0) return;

            string customValue = null;
            if(hungType == HungType.CustomerLock)
            {
                customValue = CustomValueWindow.ShowWindow();
                if (customValue == null) return;
            }

            var w = App.ServiceProvider.GetService(typeof(Windows.ConfirmWindow)) as Windows.ConfirmWindow;

            var ls = bks.Select(item => item.Value.WindturbineInfo).ToList();

            bool b = w.Show(hungType, ls, customValue);


            if (!b) return;


            //确认操作后,将所有选中的风机修改为非选中状态
            foreach (var item in bks)
            {
                item.Value.IsSelected = false;
            }
        }

        /// <summary>
        /// 参数对比
        /// </summary>
        private void Compar()
        {
            var vs = GetSelectedItems();
            foreach (var item in vs)
            {
                item.Value.IsSelected = false;
            }
            var ws = vs.Select(x=>x.Value.WindturbineInfo).ToList();
            Windows.ParameterComparisonWindow.ShowWindow(ws);
        }


        /// <summary>
        /// 发送控制命令
        /// </summary>
        public void SendControl(string tag)
        {
            OperateStyle os = (OperateStyle)Enum.Parse(typeof(OperateStyle), tag);

            var bks = GetSelectedItems();
            if (bks.Count <= 0) return;


            var w = App.ServiceProvider.GetService(typeof(Windows.ConfirmWindow)) as Windows.ConfirmWindow;

            var ls = bks.Select(item => item.Value.WindturbineInfo).ToList();

            bool b = w.Show(os, ls);


            if (!b) return;


            //确认操作后,将所有选中的风机修改为非选中状态
            foreach (var item in bks)
            {
                item.Value.IsSelected = false;
            }
        }

        /// <summary>
        /// 获取控制数据
        /// </summary>
        private WindBlockInfo GetSendData(WindBlockSmall item)
        {
            WindBlockInfo data = new WindBlockInfo();
            data.OperatedTime = item.OperatedTime;
            data.ID = item.WindturbineInfo.WindturbineId;
            data.WindturbineId = item.WindturbineInfo.WindturbineId;
            data.Status = item.WindturbineInfo.Status;
            data.ModelId = item.WindturbineInfo.ModelId;

            data.StationId = item.WindturbineInfo.StationId;
            data.ProjectId = item.WindturbineInfo.ProjectId;

            return data;
        }

        /// <summary>
        /// 获取选中的矩阵
        /// </summary>
        private Dictionary<string, WindBlockSmall> GetSelectedItems()
        {
            Dictionary<string, WindBlockSmall> bsw = new Dictionary<string, WindBlockSmall>();
            foreach (var v in _Blocks)
            {
                if (v.IsSelected && v.WindturbineInfo != null && !bsw.ContainsKey(v.WindturbineInfo.WindturbineId))
                {
                    bsw.Add(v.WindturbineInfo.WindturbineId, v);
                }
            }

            return bsw;
        }
    }
}
