using NEIntelligentControl2.Models.Matrix;
using NEIntelligentControl2.Models.Station;
using NEIntelligentControl2.Service.Windturbine;
using NEIntelligentControl2.Views.Matrix;
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.Pages.Matrix
{
    /// <summary>
    /// 全部矩阵页面
    /// </summary>
    public partial class PageMatrixAll : Page, IStationInfo
    {
        private bool _IsWindturbine;
        private bool _IsLoaged;
        public StationInfo StationInfo { get; set; }
        private CacheManager _CacheManager;
        public PageMatrixAll(CacheManager cm)
        {
            InitializeComponent();
            _CacheManager = cm;
        }

        public void UpdateData(object data)
        {
            Dispatcher.Invoke(() =>
            {
                foreach (var v in _SPMain.Children)
                {
                    var si = v as IStationInfo;
                    if (si == null) continue;
                    si.UpdateData(data);
                }
            });
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            if (_IsLoaged || StationInfo == null) return;

            List<StationInfo> infos = StationInfo.Tag as List<StationInfo>;
            if (infos == null) return;
            _SPMain.Children.Clear();
            _IsWindturbine = StationInfo.Id == "WIND_ALL";
            if (_IsWindturbine)
            {
                infos = infos.Where(i => i.Type == StationType.Wind).ToList();
                foreach (var v in infos)
                {
                    _SPMain.Children.Add(new WindStationBlock(_CacheManager) { StationInfo = v });
                }
            }
            else
            {
                infos = infos.Where(i => i.Type == StationType.PV).ToList();
                foreach(var v in infos)
                {
                    _SPMain.Children.Add(new PVStationBlock() { StationInfo = v });
                }
            }

            _IsLoaged = true;
        }
    }
}
