using NEIntelligentControl2.Models.BoostStation;
using NEIntelligentControl2.Models.Messages;
using NEIntelligentControl2.Models.Pages;
using NEIntelligentControl2.Models.Station;
using NEIntelligentControl2.Service.Station;
using NEIntelligentControl2.Windows.BoostStation;
using SVGViewer.Models.Info;
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.BoostStation
{
    /// <summary>
    /// 组态升压站页面
    /// </summary>
    public partial class PageBoostStation2 : Page
    {

        private Dictionary<string, Page> _Stations;

        private IPageAction _IPageAction;
        private TagManager _TagManager;
        private StationManager _StationManager;

        /// <summary>
        /// 当前 选择的场站
        /// </summary>
        private string _CurrentStationId;


        private List<StationInfo> _StationInfos;

        private List<ShapeInfo> customizeShapes;
        /// <summary>
        /// 自定义控件
        /// </summary>
        private List<ShapeInfo> _CustomizeShapes
        {
            get
            {
                if (customizeShapes == null)
                {
                    customizeShapes = _StationManager.GetCustomizeShapes();
                }
                return customizeShapes;
            }
        }
        private Dictionary<string, ShapeInfo> _BoostStationShapes;

        public PageBoostStation2(StationManager sm, IPageAction pa, TagManager tm)
        {
            InitializeComponent();
            _StationManager = sm;
            _IPageAction = pa;
            _TagManager = tm;

            _Stations = new Dictionary<string, Page>();
            InitBoostatationInfos();
        }

        private void InitBoostatationInfos()
        {
            _StationInfos = _StationManager.GetBoostStationInfos();

            var sis = _StationManager.GetBoostStationShapes();
            _BoostStationShapes = sis.ToDictionary(i => i.Id, i => i);

            _Stations.Add("ALL", new PageStationAll(sis) { StationChanged = StationChanged2 });
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            switch (((Control)sender).Tag)
            {
                case "edit":        // 编辑
                    EditBoostStation();
                    break;
                case "add":         // 添加升压站
                    AddBoostStation();
                    break;
                case "addsub":      // 添加二次图
                    AddSubStation();
                    break;
                case "remove":      // 删除升压站
                    Remove();
                    break;
                case "manage":      // 管理二次图
                    SubStationManageWindow.ShowWindow();
                    break;
                case "big":
                case "medium":
                case "small":
                    SwitchSize(((Control)sender).Tag as string);
                    break;
                default: return;
            }
        }

        /// <summary>
        /// 切换缩略图大小
        /// </summary>
        private void SwitchSize(string v)
        {
            int i = 3;
            switch (v)
            {
                case "big":
                    i = 3;
                    break; ;
                case "medium":
                    i = 4;
                    break;
                case "small":
                    i = 5;
                    break;
                default:
                    break;
            }
            (_Stations["ALL"] as PageStationAll).SwitchSize(i);
        }

        /// <summary>
        /// 添加二次图
        /// </summary>
        private void AddSubStation()
        {
            var info = ShapeAddWindow.Show(BoostStationEditType.SubStationAdd, "添加二次图", _CustomizeShapes, CustomizeShapeEdit);
        }

        /// <summary>
        /// 删除升压站
        /// </summary>
        private void Remove()
        {
            var b = MessageWindow.ShowMessage("即将删除升压站！", "警告");
            if (!b) return;
            if (string.IsNullOrWhiteSpace(_CurrentStationId)) return;
            var shape = _BoostStationShapes[_CurrentStationId];
            string val = _StationManager.RemoveBoostStation(shape);
            if (val == "success")
            {
                var v = _StationInfos.Where(i => i.Id == shape.Id).FirstOrDefault();
                if (v == null) return;
                _StationInfos.Remove(v);
                _TagManager.TagChanged?.Invoke(null);
                _TagManager.TagChanged?.Invoke(_StationInfos);
                _BoostStationShapes.Remove(_CurrentStationId);
                return;
            }

            MessageWindow.ShowMessage($"升压站删除失败：{val}");
        }

        /// <summary>
        /// 编辑升压站
        /// </summary>
        private async void EditBoostStation()
        {
            if (string.IsNullOrWhiteSpace(_CurrentStationId) || _CurrentStationId == "ALL") return;

            var shape = _BoostStationShapes[_CurrentStationId];

            var info = ShapeAddWindow.Show(BoostStationEditType.BoostStationEdit, "升压站编辑", _CustomizeShapes, CustomizeShapeEdit, shape);

            if (info == null) return;
            ShapeInfo si = await _StationManager.GetNewStationInfo(_CurrentStationId);
            (_Stations[_CurrentStationId] as PageStation2).ShapeInfo = si;
        }

        /// <summary>
        /// 添加升压站
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void AddBoostStation()
        {
            var info = ShapeAddWindow.Show(BoostStationEditType.BoostStationAdd, "添加升压站", _CustomizeShapes, CustomizeShapeEdit);

            if (info == null) return;

            _StationInfos.Add(new StationInfo() { Name = info.Name, Id = info.Id });
            _BoostStationShapes.Add(info.Id, info);
            _TagManager.TagChanged?.Invoke(null);
            _TagManager.TagChanged?.Invoke(_StationInfos);
        }

        /// <summary>
        /// 添加自定义控件
        /// </summary>
        /// <param name="info">控件信息</param>
        /// <param name="addOrRemove">添加或移除</param>
        private bool CustomizeShapeEdit(ShapeInfo info, string type)
        {
            var v = _StationManager.AddOrRemoveCustomizeShape(info, type);
            if (v != "success")
            {
                MessageWindow.ShowMessage(v);
                return false;
            }
            return true;
        }


        /// <summary>
        /// 场站选择改变
        /// </summary>
        /// <param name="stationId">场站ID</param>
        public void StationChanged(string stationId)
        {
            if (_CurrentStationId == stationId) return;
            if (!_Stations.ContainsKey(stationId))
            {
                var bsp = _BoostStationShapes[stationId];

                Page sp = _IPageAction["BoostStation.PageStation2"];
                (sp as PageStation2).ShapeInfo = bsp;
                _Stations.Add(stationId, sp as PageStation2);
            }
            _CurrentStationId = stationId;

            _MainFrame.Navigate(_Stations[stationId]);

            m1.Visibility = _CurrentStationId == "ALL" ? Visibility.Collapsed : Visibility.Visible;
            m2.Visibility = _CurrentStationId == "ALL" ? Visibility.Visible : Visibility.Collapsed;

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


        private void StationChanged2(string obj)
        {
            _TagManager.ChangeTag?.Invoke(obj);
        }

        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;
        }
    }
}
