using NEIntelligentControl2.Models.BoostStation;
using NEIntelligentControl2.Models.Messages;
using NEIntelligentControl2.Models.Pages;
using NEIntelligentControl2.Models.Station;
using NEIntelligentControl2.Service.WebSocket;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
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.BoostStation
{
    /// <summary>
    /// 升压站页面
    /// </summary>
    public partial class PageBoostStation : Page
    {
        private Dictionary<string, PageStation> _Stations;
        private Dictionary<string, BoostStationInfo> _BoostStationInfos;
        /// <summary>
        /// 当前 选择的场站
        /// </summary>
        private string _CurrentStationId;
        /// <summary>
        /// 服务地址
        /// </summary>
        private string _Url;
        private WEBHelper _WEBHelper;
        private IPageAction _IPageAction;
        private TagManager _TagManager;
        private List<StationInfo> _StationInfos;
        private UrlManager _UrlManager;
        public PageBoostStation(WEBHelper web, IPageAction pa, TagManager tm, UrlManager um)
        {
            InitializeComponent();
            _WEBHelper = web;
            _IPageAction = pa;
            _TagManager = tm;
            _UrlManager = um;
            _Url = _UrlManager.ServicePath;
            InitBoostatationInfos();
        }

        private void InitBoostatationInfos()
        {
            _BoostStationInfos = new Dictionary<string, BoostStationInfo>();
            _Stations = new Dictionary<string, PageStation>();
            try
            {
                var infos = _WEBHelper.HttpGetJSON<Dictionary<string, BoostStationInfo>>($"{_Url}/api/station/boost_station/all");
                _BoostStationInfos = infos;
                var sis = from i in infos
                          orderby i.Value.Index
                          select new StationInfo()
                          {
                              Name = i.Value.TagName,
                              Id = i.Value.Id
                          };
                _StationInfos = sis.ToList();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"升压站信息加载出现错误：{ex.Message}");
            }
        }

        /// <summary>
        /// 场站选择改变
        /// </summary>
        /// <param name="stationId">场站ID</param>
        public void StationChanged(string stationId)
        {
            if (_CurrentStationId == stationId) return;
            if (!_Stations.ContainsKey(stationId))
            {
                BoostStationInfo bsi = new BoostStationInfo() { Id = stationId };
                if (_BoostStationInfos.ContainsKey(stationId))
                {
                    bsi = _BoostStationInfos[stationId];
                }
                Page sp = _IPageAction["BoostStation.PageStation"];
                (sp as PageStation).StationInfo = bsi;
                (sp as PageStation).SaveAction = SaveBoostStationInfos;
                _Stations.Add(stationId, sp as PageStation);
            }
            _CurrentStationId = stationId;

            _MainFrame.Navigate(_Stations[stationId]);

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

        /// <summary>
        /// 保存升压站信息
        /// </summary>
        private void SaveBoostStationInfos(BoostStationInfo obj)
        {
            if (_BoostStationInfos.ContainsKey(obj.Id))
            {
                _BoostStationInfos[obj.Id] = obj;
            }
            else
            {
                _BoostStationInfos.Add(obj.Id, obj);
            }
            try
            {
                _WEBHelper.HttpPostBodyString($"{_Url}/api/station/boost_station", obj);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"保存升压站信息出现错误：{ex.Message}");
            }
            try
            {
                string s = _WEBHelper.Serialize(_BoostStationInfos);
                File.WriteAllText(".\\BoostStationInfos.json", s);
            }
            catch { }
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            _TagManager.TagSwitched = TagSwitched;
            _TagManager.TagChanged?.Invoke(_StationInfos);
            _CurrentStationId = null;
        }

        private void TagSwitched(StationInfo obj)
        {
            StationChanged(obj.Id);
        }

        private void Page_Unloaded(object sender, RoutedEventArgs e)
        {
            _CurrentStationId = null;
        }
    }
}
