using BDU.Windows;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace BDU.Converters
{
    internal class ParameterToGotoSettingStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is Guid stepID && stepID == MainWindow.Instance.SelectedStep!.ID)
            {
                if (MainWindow.Instance.SelectedStep == null)
                {
                    return "";
                }
                if (MainWindow.Instance.SelectedStep!.OKGotoStepID == null && MainWindow.Instance.SelectedStep!.NGGotoStepID == null)
                {
                    return "0/0";
                }
                else
                {
                    string gotoString = "";
                    if (MainWindow.Instance.SelectedStep!.OKGotoStepID != null)
                    {
                        var OKGotoStep = MainWindow.Instance.Program.StepCollection.FirstOrDefault(x => x.ID == MainWindow.Instance.SelectedStep!.OKGotoStepID);
                        if (OKGotoStep != null)
                        {
                            gotoString = OKGotoStep.Index.ToString() + "/";
                        }
                        else
                        {
                            gotoString = "0/";
                        }
                    }
                    else
                    {
                        gotoString = "0/";
                    }
                    if (MainWindow.Instance.SelectedStep!.NGGotoStepID != null)
                    {
                        var NGGotoStep = MainWindow.Instance.Program.StepCollection.FirstOrDefault(x => x.ID == MainWindow.Instance.SelectedStep!.NGGotoStepID);
                        if (NGGotoStep != null)
                        {
                            gotoString += NGGotoStep.Index.ToString();
                        }
                        else
                        {
                            gotoString += "0";
                        }
                    }
                    else
                    {
                        gotoString += "0";
                    }
                    return gotoString;
                }
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is string gotoSettingstring)
            {
                gotoSettingstring = gotoSettingstring.Replace(" ", "").Replace("/n", "").Replace("/r", "");
                if (gotoSettingstring == "0/0")
                {
                    MainWindow.Instance.SelectedStep!.OKGotoStepID = null;
                    MainWindow.Instance.SelectedStep!.NGGotoStepID = null;
                }
                else
                {
                    try
                    {
                        var list = gotoSettingstring.Split("/");
                        var okindex = System.Convert.ToInt32(list[0]);
                        var ngindex = System.Convert.ToInt32(list[1]);
                        if (okindex == 0)
                        {
                            MainWindow.Instance.SelectedStep!.OKGotoStepID = null;
                        }
                        else
                        {
                            if (okindex > MainWindow.Instance.Program.StepCollection.Count)
                            {
                                throw new Exception("步骤序号超出最大值");
                            }
                            MainWindow.Instance.SelectedStep!.OKGotoStepID = MainWindow.Instance.Program.StepCollection.FirstOrDefault(x => x.Index == okindex)?.ID;
                        }
                        if (ngindex == 0)
                        {
                            MainWindow.Instance.SelectedStep!.NGGotoStepID = null;
                        }
                        else
                        {
                            if (ngindex > MainWindow.Instance.Program.StepCollection.Count)
                            {
                                throw new Exception("步骤序号超出最大值");
                            }
                            MainWindow.Instance.SelectedStep!.NGGotoStepID = MainWindow.Instance.Program.StepCollection.FirstOrDefault(x => x.Index == ngindex)?.ID;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"跳转表达式错误：{ex.Message}");
                    }
                }
            }
            return MainWindow.Instance.SelectedStep!.ID;
        }
    }
}
