using BDU.Models;
using BDU.Tools;
using BDU.Windows;
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.Navigation;
using System.Windows.Shapes;

namespace BDU.Views
{
    /// <summary>
    /// SingleStepEdit.xaml 的交互逻辑
    /// </summary>
    public partial class SingleStepEdit : UserControl
    {
        public StepModel SelectedStep => MainWindow.Instance.SelectedStep ?? new();

        public SingleStepEdit()
        {
            InitializeComponent();
        }

        private void SaveStep_Click(object sender, RoutedEventArgs e)
        {
            if (SelectedStep == null || (SelectedStep.Method == null && SelectedStep.SubProgram == null))
            {
                return;
            }
            if (SelectedStep.OKExpression != null)
            {
                Dictionary<string, object> paraDic = [];
                foreach (var item in MainWindow.Instance.Program.Parameters)
                {
                    paraDic.TryAdd(item.Name, item.Value!);
                }
                //if (!ExpressionEvaluator.IsExpressionValid(SelectedStep.OKExpression, paraDic))
                //{
                //    MessageBox.Show("合格条件表达式非法，请检查后重试");
                //    return;
                //}
            }
            if (StepsManager.Instance!.SelectedItem is StepModel originalStep)
            {
                originalStep.Name = SelectedStep.Name;
                originalStep.OKExpression = SelectedStep.OKExpression;
                originalStep.OKGotoStepID = SelectedStep.OKGotoStepID;
                originalStep.NGGotoStepID = SelectedStep.NGGotoStepID;
                originalStep.Description = SelectedStep.Description;

                // 更新参数
                if (originalStep.Method != null)
                {
                    if (originalStep.StepType == "循环开始")
                    {
                        try
                        {
                            originalStep.LoopCount = Convert.ToInt32(SelectedStep.Method!.Parameters[0].Value);
                        }
                        catch
                        {
                            Log.Error("循环指令参数设置错误：类型转换失败");
                        }
                    }
                    else
                    {
                        for (int i = 0; i < originalStep.Method.Parameters.Count; i++)
                        {
                            try
                            {
                                var editedParam = SelectedStep.Method!.Parameters[i];
                                var originalParam = originalStep.Method.Parameters[i];
                                if (editedParam.IsUseVar)
                                {
                                    originalParam.VariableName = editedParam.VariableName;
                                    originalParam.VariableID = MainWindow.Instance.Program.Parameters.FirstOrDefault(x => x.Name == editedParam.VariableName)!.ID;
                                }
                                //originalParam.VariableName = editedParam.VariableName;
                                //originalParam.VariableID = MainWindow.Instance.Program.Parameters.FirstOrDefault(x => x.Name == editedParam.VariableName)!.ID;
                                originalParam.Value = editedParam.Value;
                                originalParam.IsUseVar = editedParam.IsUseVar;
                                originalParam.LowerLimit = editedParam.LowerLimit;
                                originalParam.UpperLimit = editedParam.UpperLimit;
                                originalParam.IsOutputToReport = editedParam.IsOutputToReport;
                            }
                            catch(Exception ex)
                            {
                                Log.Error(ex.Message);
                            }
                        }
                    }
                }
                else if (originalStep.SubProgram != null)
                {
                    for (int i = 0; i < originalStep.SubProgram.Parameters.Count; i++)
                    {
                        var editedParam = SelectedStep.SubProgram!.Parameters[i];
                        var originalParam = originalStep.SubProgram.Parameters[i];
                        if (editedParam.IsUseVar)
                        {
                            originalParam.VariableName = editedParam.VariableName;
                            originalParam.VariableID = MainWindow.Instance.Program.Parameters.FirstOrDefault(x => x.Name == editedParam.VariableName)!.ID;
                        }
                        originalParam.Value = editedParam.Value;
                        originalParam.IsUseVar = editedParam.IsUseVar;
                        originalParam.LowerLimit = editedParam.LowerLimit;
                        originalParam.UpperLimit = editedParam.UpperLimit;
                        originalParam.IsOutputToReport = editedParam.IsOutputToReport;
                    }
                }
                Log.Success($"用户 [ {MainWindow.Instance.User.UserName} ] 修改步骤 [ {originalStep.Index} ] [ {originalStep.Name} ] ");
            }
            MainWindow.Instance.SelectedStep = null;
        }

        private void CancelStepEdit_Click(object sender, RoutedEventArgs e)
        {
            MainWindow.Instance.SelectedStep = null;
        }
    }
}
