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

        /// <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 CountPowerRestriction { get => (int)GetValue(CountPowerRestrictionProperty); set => SetValue(CountPowerRestrictionProperty, 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; }

        private List<PVBlock> _PVBlocks;
        public PVStationBlock()
        {
            InitializeComponent();
        }

        public void UpdateData(object obj)
        {
            var datas = obj as Dictionary<string, PVInfo>;
            if (datas == null) return;

            var ls = datas.Values.Where(wi => wi.Station == StationInfo.Id).ToList();
            if (_PVBlocks == null)
            {
                InitBlocks(ls);
            }

            CountTotal = ls.Count;
            CountPowerRestriction = ls.Where(s => s.State == PVState.Restriction|| s.State==PVState.RestrictionShutdown).ToList().Count;
            CountConnect = ls.Where(s => s.State == PVState.GridConnected).ToList().Count;
            CountStandby = ls.Where(s => s.State == PVState.Standby).ToList().Count;
            CountMaintain = ls.Where(s => s.State == PVState.Maintain).ToList().Count;
            CountFault = ls.Where(s => s.State == PVState.Malfunction).ToList().Count;
            CountOffline = ls.Where(s => s.State == PVState.Offline).ToList().Count;

            if (_PVBlocks == null) return;

            foreach (var v in _PVBlocks)
            {
                if (!datas.ContainsKey(v.Id))
                {
                    continue;
                }
                v.Update(datas[v.Id]);
            }
        }

        private void InitBlocks(List<PVInfo> ls)
        {
            _PVBlocks = new List<PVBlock>();
            ls.Sort((x, y) => x.Index - y.Index);

            Dispatcher.Invoke(() =>
            {
                foreach (var v in ls)
                {
                    PVBlock pb = new PVBlock() { Id = v.Id, IsSmallModel = true };
                    _PVBlocks.Add(pb);
                    _UGMain.Children.Add(pb);
                }
            });
        }
    }
}
