using NEIntelligentControl2.Service.Station;
using NEIntelligentControl2.Views.BoostStation;
using SVGViewer.Models.Info;
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.Shapes;

namespace NEIntelligentControl2.Windows.BoostStation
{
    /// <summary>
    /// 二次图管理窗口
    /// </summary>
    public partial class SubStationManageWindow : Window
    {
        private StationManager _StationManager;

        private List<ShapeInfo> customizeShapes;
        /// <summary>
        /// 自定义控件
        /// </summary>
        private List<ShapeInfo> _CustomizeShapes
        {
            get
            {
                if (customizeShapes == null)
                {
                    customizeShapes = _StationManager.GetCustomizeShapes();
                }
                return customizeShapes;
            }
        }
        public SubStationManageWindow()
        {
            InitializeComponent();
            _StationManager = App.ServiceProvider.GetService(typeof(StationManager)) as StationManager;

            Init();
        }

        private async void Init()
        {
            List<ShapeInfo> ls = await _StationManager.GetSubStations();
            if (ls == null) return;
            foreach (var v in ls)
            {
                SubStationView ssv = new SubStationView(v);
                ssv.ContentMenuAction = ControlSubStation;
                _ug.Children.Add(ssv);
            }
        }

        /// <summary>
        /// 卡片右键菜单事件
        /// </summary>
        /// <param name="arg1">图形信息</param>
        /// <param name="arg2">操作类型</param>
        private void ControlSubStation(SubStationView sv, ShapeInfo arg1, string arg2)
        {
            if (arg1 == null) return;
            switch (arg2)
            {
                case "edit":        // 编辑
                    Edit(sv, arg1);
                    break;
                case "remove":      // 移除
                    Remove(sv, arg1);
                    break;
                default: return;
            }
        }

        /// <summary>
        /// 移除二次图
        /// </summary>
        private void Remove(SubStationView sv, ShapeInfo arg1)
        {
            var b = MessageWindow.ShowMessage("即将删除二次图！", "警告");
            if (!b) return;

            string val = _StationManager.RemoveSubStation(arg1);
            if (val == "success")
            {
                _ug.Children.Remove(sv);
                return;
            }

            MessageWindow.ShowMessage($"二次图删除失败：{val}");
        }

        /// <summary>
        /// 编辑二次图
        /// </summary>
        private void Edit(SubStationView sv, ShapeInfo arg1)
        {
            var info = ShapeAddWindow.Show(BoostStationEditType.SubStationEdit, "编辑二次图", _CustomizeShapes, CustomizeShapeEdit, arg1);
            if(info == null) return;
            sv.SwitchInfo(info);
        }

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


        public static void ShowWindow()
        {
            SubStationManageWindow ssmw = new SubStationManageWindow();
            ssmw.Owner = Application.Current.MainWindow;
            ssmw.ShowDialog();
        }
    }
}
