using NEIntelligentControl2.Service.Station;
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 ShapeAddWindow : Window
    {
        public ShapeInfo Info { get; set; }

        /// <summary>
        /// 编辑类型
        /// </summary>
        public BoostStationEditType EditType { get; set; }


        private StationManager _StationManager;
        public ShapeAddWindow(List<ShapeInfo> customizeShapes)
        {
            InitializeComponent();
            _StationManager = App.ServiceProvider.GetService(typeof(StationManager)) as StationManager;
            _esv.CustomizeShapes = customizeShapes;
            _esv.CustomizeEdited = OnCustomizeEdited;
        }

        public ShapeAddWindow(ShapeInfo info, List<ShapeInfo> customizeShapes) : this(customizeShapes)
        {
            Info = info;
            _esv.SwitchShapeInfo(info);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            switch (((Control)sender).Tag)
            {
                case "ok":          // 确认
                    Ok();
                    break;
                case "cancel":      // 取消
                    this.DialogResult = false;
                    break;
                case "output":
                    Output();
                    break;
                default: return;
            }
        }

        private void Output()
        {
            SVGViewer.Manager.SVGOutput2 so = new SVGViewer.Manager.SVGOutput2(_esv.Canvas, _esv.CustomizeShapes);
            Task.Run(output);

            void output()
            {
                var v = so.GetSvgFileInfo();
                var info = so.SvgInfo;
                string s = _StationManager.OutputSvgInfo(v, info);
                if (s == "success")
                {
                    Dispatcher.Invoke(() => MessageWindow.ShowMessage("导出成功！"));
                    return;
                }

                Dispatcher.Invoke(() => MessageWindow.ShowMessage(s));
            }
        }

        /// <summary>
        /// 自定义控件编辑
        /// </summary>
        private bool OnCustomizeEdited(ShapeInfo arg1, string arg2)
        {
            var v = _StationManager.AddOrRemoveCustomizeShape(arg1, arg2);
            if (v != "success")
            {
                MessageWindow.ShowMessage(v);
                return false;
            }
            return true;
        }

        private void Ok()
        {
            var info = _esv.SharpInfo;

            var v = _StationManager.AddOrUpdateStation(info, this.EditType);
            if (v != "success")
            {
                MessageWindow.ShowMessage(v);
                return;
            }

            Info = _esv.SharpInfo;
            this.DialogResult = true;
        }

        public static ShapeInfo Show(BoostStationEditType type, string title, List<ShapeInfo> customizeShapes, Func<ShapeInfo, string, bool> shapeAdding, ShapeInfo info = null)
        {
            ShapeAddWindow saw = info == null ? new ShapeAddWindow(customizeShapes) : new ShapeAddWindow(info, customizeShapes);
            saw.EditType = type;
            saw.Owner = Application.Current.MainWindow;
            saw.Title = title;
            //saw.CustomizeEdited = shapeAdding;
            var b = saw.ShowDialog();
            if (b == true)
            {
                return saw.Info;
            }
            return null;
        }

        private void Undo_CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            _esv.Undo();
        }

        private void Redo_CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            _esv.Redo();
        }
    }

    /// <summary>
    /// 升压站编辑类型
    /// </summary>
    public enum BoostStationEditType
    {
        /// <summary>
        /// 升压站编辑
        /// </summary>
        BoostStationEdit,
        /// <summary>
        /// 升压站添加
        /// </summary>
        BoostStationAdd,
        /// <summary>
        /// 二次图编辑
        /// </summary>
        SubStationEdit,
        /// <summary>
        /// 二次图添加
        /// </summary>
        SubStationAdd,
    }
}
