using Aitex.Core.RT.RecipeCenter;
using MECF.Framework.Common.OperationCenter;
using MECF.Framework.Common.RecipeCenter;
using CyberX8_Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
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;
using MECF.Framework.Common.Equipment;
namespace CyberX8_Themes.UserControls
{
    /// 
    /// RecipeControlMetal.xaml 的交互逻辑
    /// 
    public partial class RecipeControlMetal : UserControl
    {
        public RecipeControlMetal()
        {
            InitializeComponent();
            CheckBoxA.IsChecked = false;
            CheckBoxB.IsChecked = false;
        }
        #region 属性
        public static readonly DependencyProperty ModuleNameProperty = DependencyProperty.Register(
            "ModuleName", typeof(string), typeof(RecipeControlMetal),
                new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// 模块名称
        /// 
        public string ModuleName
        {
            get
            {
                return (string)this.GetValue(ModuleNameProperty);
            }
            set
            {
                this.SetValue(ModuleNameProperty, value);
            }
        }
        public static readonly DependencyProperty ModuleTitleProperty = DependencyProperty.Register(
           "ModuleTitle", typeof(string), typeof(RecipeControlMetal),
               new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// 模块名称
        /// 
        public string ModuleTitle
        {
            get
            {
                return (string)this.GetValue(ModuleTitleProperty);
            }
            set
            {
                this.SetValue(ModuleTitleProperty, value);
            }
        }
        public static readonly DependencyProperty InputCycleTimesProperty = DependencyProperty.Register(
          "InputCycleTimes", typeof(int), typeof(RecipeControlMetal), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// InputCycleTimes
        /// 
        public int InputCycleTimes
        {
            get
            {
                return (int)this.GetValue(InputCycleTimesProperty);
            }
            set
            {
                this.SetValue(InputCycleTimesProperty, value);
            }
        }
        public static readonly DependencyProperty AchievedCycleTimesProperty = DependencyProperty.Register(
      "AchievedCycleTimes", typeof(int), typeof(RecipeControlMetal), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// AchievedCycleTimes
        /// 
        public int AchievedCycleTimes
        {
            get
            {
                return (int)this.GetValue(AchievedCycleTimesProperty);
            }
            set
            {
                this.SetValue(AchievedCycleTimesProperty, value);
            }
        }
        public static readonly DependencyProperty NumberOfSelectedRecipeScansProperty = DependencyProperty.Register(
            "NumberOfSelectedRecipeScans", typeof(int), typeof(RecipeControlMetal), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// NumberOfSelectedRecipeScans
        /// 
        public int NumberOfSelectedRecipeScans
        {
            get
            {
                return (int)this.GetValue(NumberOfSelectedRecipeScansProperty);
            }
            set
            {
                this.SetValue(NumberOfSelectedRecipeScansProperty, value);
            }
        }
        public static readonly DependencyProperty SelectedRecipeNodeProperty = DependencyProperty.Register(
            "SelectedRecipeNode", typeof(RecipeNode), typeof(RecipeControlMetal), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(SelectedRecipeNodeChanged)));
        /// 
        /// 当前选中recipe节点
        /// 
        public RecipeNode SelectedRecipeNode
        {
            get
            {
                return (RecipeNode)this.GetValue(SelectedRecipeNodeProperty);
            }
            set
            {
                this.SetValue(SelectedRecipeNodeProperty, value);
            }
        }
        public static readonly DependencyProperty RecipeTypeProperty = DependencyProperty.Register(
            "RecipeType", typeof(string), typeof(RecipeControlMetal));
        /// 
        /// Recipe类型
        /// 
        public string RecipeType
        {
            get
            {
                return (string)this.GetValue(RecipeTypeProperty);
            }
            set
            {
                this.SetValue(RecipeTypeProperty, value);
            }
        }
        public static readonly DependencyProperty LoadEnabledProperty = DependencyProperty.Register(
            "LoadEnabled", typeof(bool), typeof(RecipeControlMetal), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// 按钮可用性
        /// 
        public bool LoadEnabled
        {
            get
            {
                return (bool)this.GetValue(LoadEnabledProperty);
            }
            set
            {
                this.SetValue(LoadEnabledProperty, value);
            }
        }
        /// 
        /// 选中Recipe文件的触发的方法
        /// 
        /// 
        /// 
        private static void SelectedRecipeNodeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                d.SetValue(LoadEnabledProperty, true);
            }
            else
            {
                d.SetValue(LoadEnabledProperty, false);
            }
        }
        public static readonly DependencyProperty CurrentOperationModeProperty = DependencyProperty.Register(
           "CurrentOperationMode", typeof(string), typeof(RecipeControlMetal));
        /// 
        /// CurrentOperationMode
        /// 
        public string CurrentOperationMode
        {
            get
            {
                return (string)this.GetValue(CurrentOperationModeProperty);
            }
            set
            {
                this.SetValue(CurrentOperationModeProperty, value);
            }
        }
        public static readonly DependencyProperty IsEngineeringProperty = DependencyProperty.Register(
   "IsEngineering", typeof(bool), typeof(RecipeControlMetal), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// IsEngineering
        /// 
        public bool IsEngineering
        {
            get
            {
                return (bool)this.GetValue(IsEngineeringProperty);
            }
            set
            {
                this.SetValue(IsEngineeringProperty, value);
            }
        }
        public static readonly DependencyProperty IsProductionProperty = DependencyProperty.Register(
    "IsProduction", typeof(bool), typeof(RecipeControlMetal), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// IsProduction
        /// 
        public bool IsProduction
        {
            get
            {
                return (bool)this.GetValue(IsProductionProperty);
            }
            set
            {
                this.SetValue(IsProductionProperty, value);
            }
        }
        public static readonly DependencyProperty RecipeModeValueProperty = DependencyProperty.Register(
           "RecipeModeValue", typeof(string), typeof(RecipeControlMetal), new FrameworkPropertyMetadata("", new PropertyChangedCallback(OnItemsSourceChanged)));
        /// 
        /// OpertationModeValue
        /// 
        public string RecipeModeValue
        {
            get
            {
                return (string)this.GetValue(RecipeModeValueProperty);
            }
            set
            {
                this.SetValue(RecipeModeValueProperty, value);
            }
        }
       
        public static readonly DependencyProperty SelectedSideProperty = DependencyProperty.Register(
    "SelectedSide", typeof(string), typeof(RecipeControlMetal), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// SelectedSide
        /// 
        public string SelectedSide
        {
            get
            {
                return (string)this.GetValue(SelectedSideProperty);
            }
            set
            {
                this.SetValue(SelectedSideProperty, value);
            }
        }
        private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                string currentMode = (string)e.NewValue;
                switch (currentMode)
                {
                    case "Engineering":
                        d.SetValue(IsEngineeringProperty, true);
                        d.SetValue(IsProductionProperty, false);
                        break;
                    case "Production":
                        d.SetValue(IsEngineeringProperty, false);
                        d.SetValue(IsProductionProperty, true);
                        break;
                    default:
                        break;
                }
            }
        }
        #endregion
       
        private void Pause_Click(object sender, RoutedEventArgs e)
        {
            if ("Manual".Equals(CurrentOperationMode))
            {
                InvokeClient.Instance.Service.DoOperation($"{ModuleName}.PauseWaferRecipe", SelectedRecipeNode.RecipeFullFileName, InputCycleTimes);
            }
            else
            {
                MessageBox.Show("Current OperationMode Can't Perform This Action", $"Current OperationMode is {CurrentOperationMode}", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        private void Resume_Click(object sender, RoutedEventArgs e)
        {
            if ("Manual".Equals(CurrentOperationMode))
            {
                InvokeClient.Instance.Service.DoOperation($"{ModuleName}.ResumeWaferRecipe", SelectedRecipeNode.RecipeFullFileName, InputCycleTimes);
            }
            else
            {
                MessageBox.Show("Current OperationMode Can't Perform This Action", $"Current OperationMode is {CurrentOperationMode}", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        private void Abort_Click(object sender, RoutedEventArgs e)
        {
            if ("Manual".Equals(CurrentOperationMode))
            {
                InvokeClient.Instance.Service.DoOperation($"{ModuleName}.Abort");
            }
            else
            {
                if (RecipeModeValue != SelectedRecipeNode.RecipeLocation)
                {
                    MessageBox.Show("Recipe OperationMode is not match!", $"Current Recipe OperationMode is {RecipeModeValue}", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                {
                    MessageBox.Show("Current OperationMode Can't Perform This Action", $"Current OperationMode is {CurrentOperationMode}", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }
        private void Edit_Click(object sender, RoutedEventArgs e)
        {
            string _recipeName = "";
            string pattern1 = @"^Metal\d$";
            Regex regex1 = new Regex(pattern1);
            switch (ModuleName)
            {               
                case string str when regex1.IsMatch(str):
                    _recipeName = "DepRecipe";
                    break;
                default:
                    break;
            }
            if (!"Manual".Equals(CurrentOperationMode))
            {
                MessageBox.Show("Current OperationMode Can't Perform This Action", $"Current OperationMode is {CurrentOperationMode}", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                if (RecipeModeValue != SelectedRecipeNode.RecipeLocation)
                {
                    MessageBox.Show("Recipe OperationMode is not match!", $"Current Recipe OperationMode is {RecipeModeValue}", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                {
                    GlobalEvents.OnSwitchFixedTabItem("Recipe", "", _recipeName);
                }
            }
        }
        private void RunRecipe_Click(object sender, RoutedEventArgs e)
        {
            if ("Manual".Equals(CurrentOperationMode))
            {
                if (InputCycleTimes == 0)
                {
                    MessageBox.Show("Please Enter CycleTimes First!", "Current Cycle is 0", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                {
                    if (RecipeModeValue != SelectedRecipeNode.RecipeLocation)
                    {
                        MessageBox.Show("Recipe OperationMode is not match!", $"Current Recipe OperationMode is {RecipeModeValue}", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    else
                    {                        
                        InvokeClient.Instance.Service.DoOperation($"{ModuleName}.CycleManualProcessRecipe", SelectedRecipeNode.RecipeFullFileName, SelectedSide, InputCycleTimes);
                    }
                }
            }
            else
            {
                MessageBox.Show("Current OperationMode Can't Perform This Action", $"Current OperationMode is {CurrentOperationMode}", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        private void Side_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox checkBox = (sender as CheckBox);
            if ((bool)CheckBoxA.IsChecked && (bool)!CheckBoxB.IsChecked)
            {
                SelectedSide = "SideA";
            }
            else if ((bool)!CheckBoxA.IsChecked && (bool)CheckBoxB.IsChecked)
            {
                SelectedSide = "SideB";
            }else
            {
                SelectedSide = "";
            }
        }
    }
}