using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using SlnMesnac.Business;
using SlnMesnac.Business.@base;
using SlnMesnac.Model.domain;
using SlnMesnac.Plc;
using SlnMesnac.WPF.Model;
using SlnMesnac.WPF.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
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;
using TouchSocket.Core;

namespace SlnMesnac.WPF.Page
{
    /// <summary>
    /// DevMonitorPage.xaml 的交互逻辑
    /// </summary>
    public partial class RecipeManagePage : UserControl
    {
        private BaseBusiness baseBusiness = null;
        private RecipeManageCache recipeManageCache = RecipeManageCache.Instance;

        public RecipeManagePage()
        {
            InitializeComponent();
            this.DataContext = new RecipeManageViewModel();
            InitWeightInfo();
        }

        /// <summary>
        /// 加载重量及时间
        /// </summary>
        private void InitWeightInfo()
        {
            highSpeed1.Text = recipeManageCache.recipeManageList.FirstOrDefault(x => x.RecipeName == "高速配方").LowWeight.ToString();
            highSpeed2.Text = recipeManageCache.recipeManageList.FirstOrDefault(x => x.RecipeName == "高速配方").HightWeight.ToString();

            midiumSpeed1.Text = recipeManageCache.recipeManageList.FirstOrDefault(x => x.RecipeName == "中速配方").LowWeight.ToString();
            midiumSpeed2.Text = recipeManageCache.recipeManageList.FirstOrDefault(x => x.RecipeName == "中速配方").HightWeight.ToString();

            lowSpeed1.Text = recipeManageCache.recipeManageList.FirstOrDefault(x => x.RecipeName == "低速配方").LowWeight.ToString();
            lowSpeed2.Text = recipeManageCache.recipeManageList.FirstOrDefault(x => x.RecipeName == "低速配方").HightWeight.ToString();

            UnpackToHotTime.Text = recipeManageCache.UnpackToHotTime;
            HotToSpiralTime.Text = recipeManageCache.HotToSpiralTime;
            SpiralTwoTime.Text = recipeManageCache.SpiralTwoTime;
        }

        private void updateWeight_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // 创建变量来存储转换后的整数值
                int highSpeed1Value, highSpeed2Value;
                int midiumSpeed1Value, midiumSpeed2Value;
                int lowSpeed1Value, lowSpeed2Value;

                // 从文本框中获取文本并尝试转换为整数
                if (int.TryParse(highSpeed1.Text, out highSpeed1Value) &&
                    int.TryParse(highSpeed2.Text, out highSpeed2Value) &&
                    int.TryParse(midiumSpeed1.Text, out midiumSpeed1Value) &&
                    int.TryParse(midiumSpeed2.Text, out midiumSpeed2Value) &&
                    int.TryParse(lowSpeed1.Text, out lowSpeed1Value) &&
                    int.TryParse(lowSpeed2.Text, out lowSpeed2Value))
                {
                    // 校验是否所有的整数值都大于0
                    if (highSpeed1Value >= 0 && highSpeed2Value >= 0 &&
                        midiumSpeed1Value >= 0 && midiumSpeed2Value >= 0 &&
                        lowSpeed1Value >= 0 && lowSpeed2Value >= 0)
                    {
                        RecipeManage highRecipe = recipeManageCache.recipeManageList.FirstOrDefault(x => x.RecipeName == "高速配方");
                        RecipeManage midiumRecipe = recipeManageCache.recipeManageList.FirstOrDefault(x => x.RecipeName == "中速配方");
                        RecipeManage lowRecipe = recipeManageCache.recipeManageList.FirstOrDefault(x => x.RecipeName == "低速配方");
                        highRecipe.LowWeight = highSpeed1Value;
                        highRecipe.HightWeight = highSpeed2Value;
                        recipeManageCache.UpdateRecipeManage(highRecipe);
                        midiumRecipe.LowWeight = midiumSpeed1Value;
                        midiumRecipe.HightWeight = midiumSpeed2Value;
                        recipeManageCache.UpdateRecipeManage(midiumRecipe);
                        midiumRecipe.LowWeight = midiumSpeed1Value;
                        lowRecipe.LowWeight = lowSpeed1Value;
                        lowRecipe.HightWeight = lowSpeed2Value;
                        recipeManageCache.UpdateRecipeManage(lowRecipe);
                        MessageBox.Show("更新成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                    else
                    {

                        MessageBox.Show("所有的重量值必须大于等于0", "输入错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
                else
                {
                    // 转换失败，提示用户
                    MessageBox.Show("请确保所有输入值都是有效的整数", "输入错误", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void updateSpiralTime_Click(object sender, RoutedEventArgs e)
        {
           
            int unpackToHotTimeValue;
            int hotToSpiralTimeValue;
            int spiralTwoTimeValue;

            
            bool unpackToHotTimeValid = int.TryParse(UnpackToHotTime.Text, out unpackToHotTimeValue);
            bool hotToSpiralTimeValid = int.TryParse(HotToSpiralTime.Text, out hotToSpiralTimeValue);
            bool spiralTwoTimeValid = int.TryParse(SpiralTwoTime.Text, out spiralTwoTimeValue);

            
            if (unpackToHotTimeValid && hotToSpiralTimeValid && spiralTwoTimeValid &&
                unpackToHotTimeValue >= 0 && hotToSpiralTimeValue >= 0 && spiralTwoTimeValue >= 0)
            {
                recipeManageCache.UnpackToHotTime = UnpackToHotTime.Text;
                recipeManageCache.HotToSpiralTime = HotToSpiralTime.Text;
                recipeManageCache.SpiralTwoTime = SpiralTwoTime.Text;
                MessageBox.Show("更新成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
               
                MessageBox.Show("请输入有效的正整数值（大于等于0）", "输入错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
