using log4net;
using NEIntelligentControl2.Models.Matrix;
using NEIntelligentControl2.Models.Messages;
using NEIntelligentControl2.Models.Pages;
using NEIntelligentControl2.Models.PV;
using NEIntelligentControl2.Models.Station;
using NEIntelligentControl2.Models.Windturbine;
using NEIntelligentControl2.Service.WebSocket;
using NEIntelligentControl2.Service.Station;
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.Pages.Matrix
{
    /// <summary>
    /// 矩阵页面
    /// </summary>
    public partial class PageMatrix : Page
    {
        private ILog log = LogManager.GetLogger("File");
        private MessageBridge _MessageBridge;// websocket

        private WindturbineInfoBridge _WindturbineInfoBridge;// 风机信息数据桥
        private PVInfoBridge _PVInfoBridge;// 光伏信息
        private Page _CurrentPage; // 当前页面
        private IPageAction _IPageAction;           // 页面动作
        private Dictionary<string, Page> _Pages = new Dictionary<string, Page>();
        private TagManager _TagManager;
        private StationManager _StationManager;

        private List<StationInfo> _StationInfos;    // 场站信息
        private StationInfo _CurrentStationInfo;// 当前场站信息
        public PageMatrix(MessageBridge msbridge, WEBHelper web, IPageAction action, TagManager tm, StationManager sm)
        {
            InitializeComponent();
            _MessageBridge = msbridge;
            _IPageAction = action;
            _TagManager = tm;
            _StationManager = sm;
            _WindturbineInfoBridge = new WindturbineInfoBridge() { Messaged = OnWindturbineMessage };
            _PVInfoBridge = new PVInfoBridge() { Messaged = OnPVMessage };
        }

        /// <summary>
        /// 光伏基本信息
        /// </summary>
        private void OnPVMessage(Dictionary<string, PVInfo> obj)
        {
            if (obj == null) return;
            (_CurrentPage as IStationInfo)?.UpdateData(obj);
        }

        /// <summary>
        /// 风机基本信息
        /// </summary>
        private void OnWindturbineMessage(Dictionary<string, WindturbineInfo> obj)
        {
            if (obj == null) return;
            (_CurrentPage as IStationInfo)?.UpdateData(obj);
        }

        private Page GetPage(StationInfo si)
        {
            if (_Pages.ContainsKey(si.Id))
            {
                return _Pages[si.Id];
            }
            Page page = null;
            if (si.Type == StationType.Other)
            {
                page = _IPageAction["Matrix.PageMatrixAll"];
            }
            else
            {
                page = _IPageAction["Matrix.PageMatrixStation"];
            }
            _Pages.Add(si.Id, page);

            return page;
        }

        /// <summary>
        /// 显示页面
        /// </summary>
        private void ShowPage(Page page)
        {
            _FrameMain.Navigate(page);

            if (!_FrameMain.NavigationService.CanGoBack) return;
            var en = _FrameMain.NavigationService.RemoveBackEntry();
            while (en != null)
            {
                en = _FrameMain.NavigationService.RemoveBackEntry();
            }
        }

        /// <summary>
        /// 切换注册消息
        /// </summary>
        private void RegisterMessage()
        {
            if (_CurrentStationInfo == null || _CurrentStationInfo.Id == "WIND_ALL" || _CurrentStationInfo.Type == StationType.Wind)
            {
                _MessageBridge.Register(_WindturbineInfoBridge);
                _MessageBridge.Unregister(_PVInfoBridge);
            }
            else
            {
                _MessageBridge.Register(_PVInfoBridge);
                _MessageBridge.Unregister(_WindturbineInfoBridge);
            }
        }

        private void Page_Unloaded(object sender, RoutedEventArgs e)
        {
            _MessageBridge.Unregister(_WindturbineInfoBridge);
            _MessageBridge.Unregister(_PVInfoBridge);
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (_StationInfos == null || _StationInfos.Count <= 0)
                {
                    _StationInfos = _StationManager.GetStationInfos();
                }
            }
            catch (Exception ex)
            {
                log.Error("读取场站信息出错！", ex);
            }
            _TagManager.TagSwitched = TagSwitched;
            _TagManager.TagChanged?.Invoke(_StationInfos);

            RegisterMessage();
        }

        /// <summary>
        /// 切换页面
        /// </summary>
        /// <param name="si"></param>
        private void TagSwitched(StationInfo si)
        {
            if (_CurrentStationInfo != null && (si == null || si.Id == _CurrentStationInfo.Id)) return;
            _CurrentStationInfo = si;
            RegisterMessage();
            Page page = GetPage(si);
            _CurrentPage = page;
            (_CurrentPage as IStationInfo).StationInfo = si;
            si.Tag = _StationInfos;
            ShowPage(page);
        }
    }
}
