using Microsoft.Extensions.DependencyInjection;
using Quartz.Util;
using SlnMesnac.Business;
using SlnMesnac.Model.domain;
using SlnMesnac.WPF.Model;
using SlnMesnac.WPF.ViewModel;
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 SlnMesnac.WPF.Page
{
    /// <summary>
    /// 
    /// </summary>
    public partial class RecipeManageSetWindow : Window
    {
        private RecipeManageCache recipeManageCache = RecipeManageCache.Instance;

        private int recipeKey;
        public RecipeManageSetWindow(int key)
        {
            recipeKey = key;
            InitializeComponent();
            Init(key);
        }

        public void Init(int key)
        {
            RecipeManage recipeManage = recipeManageCache.recipeManageList.FirstOrDefault(x => x.RecipeKey == key);

            if (recipeManage != null)
            {
                UnpackSpiral1Txt.Text = recipeManage.UnpackSpiral1.ToString();
                UnpackSpiral2Txt.Text = recipeManage.UnpackSpiral2.ToString();
                DryerSpiralTxt.Text = recipeManage.DryerSpiral.ToString();
                Spiral1Txt.Text = recipeManage.Spiral1.ToString();
                Spiral2Txt.Text = recipeManage.Spiral2.ToString();
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void ConfirmButton_Click(object sender, RoutedEventArgs e)
        {
          
            if (IsValidInt(UnpackSpiral1Txt.Text, out int unpackSpiral1) &&
                IsValidInt(UnpackSpiral2Txt.Text, out int unpackSpiral2) &&
                IsValidInt(DryerSpiralTxt.Text, out int dryerSpiral) &&
                IsValidInt(Spiral1Txt.Text, out int spiral1) &&
                IsValidInt(Spiral2Txt.Text, out int spiral2))
            {
                RecipeManage recipeManage = recipeManageCache.recipeManageList.FirstOrDefault(x => x.RecipeKey == recipeKey);
                if (recipeManage != null)
                {
                    recipeManage.UnpackSpiral1 = unpackSpiral1;
                    recipeManage.UnpackSpiral2 = unpackSpiral2;
                    recipeManage.DryerSpiral = dryerSpiral;
                    recipeManage.Spiral1 = spiral1;
                    recipeManage.Spiral2 = spiral2;
                    recipeManageCache.UpdateRecipeManage(recipeManage);
                    MessageBox.Show("修改成功");
                    this.Close();
                }
            }
        }
        private bool IsValidInt(string value, out int result)
        {
           
            bool isValid = int.TryParse(value, out result) && result > 0;

            if (!isValid)
            {
                MessageBox.Show("请输入正整数", $"{value}输入错误", MessageBoxButton.OK, MessageBoxImage.Warning);
            }

            return isValid;
        }

    }
}
