using PunkHPX8_Core;
using MECF.Framework.Common.CommonData.Reservoir;
using MECF.Framework.Common.OperationCenter;
using MECF.Framework.Common.RecipeCenter;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
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 PunkHPX8_Themes.UserControls
{
    /// 
    /// RecipeLoadDose.xaml 的交互逻辑
    /// 
    public partial class RecipeLoadDose : UserControl
    {
        public RecipeLoadDose()
        {
            InitializeComponent();
        }
        public static readonly DependencyProperty ModuleNameProperty = DependencyProperty.Register(
           "ModuleName", typeof(string), typeof(RecipeLoadDose), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// 模块名称
        /// 
        public string ModuleName
        {
            get
            {
                return (string)this.GetValue(ModuleNameProperty);
            }
            set
            {
                this.SetValue(ModuleNameProperty, value);
            }
        }
        public static readonly DependencyProperty ReplenNumProperty = DependencyProperty.Register(
           "ReplenNum", typeof(int), typeof(RecipeLoadDose), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// Replen数量
        /// 
        public int ReplenNum
        {
            get
            {
                return (int)this.GetValue(ReplenNumProperty);
            }
            set
            {
                this.SetValue(ReplenNumProperty, value);
            }
        }
        public static readonly DependencyProperty RecipeTypeProperty = DependencyProperty.Register("RecipeType", typeof(string), typeof(RecipeLoadDose));
        /// 
        /// Recipe类型
        /// 
        public string RecipeType
        {
            get
            {
                return (string)this.GetValue(RecipeTypeProperty);
            }
            set
            {
                this.SetValue(RecipeTypeProperty, value);
            }
        }
        public static readonly DependencyProperty RecipeNodesProperty = DependencyProperty.Register("RecipeNodes", typeof(ObservableCollection), typeof(RecipeLoadDose));
        /// 
        /// Reicpe节点
        /// 
        public ObservableCollection RecipeNodes
        {
            get
            {
                return (ObservableCollection)this.GetValue(RecipeNodesProperty); 
            }
            set 
            { 
                this.SetValue(RecipeNodesProperty, value); 
            }
        }
        public static readonly DependencyProperty RecipeLstProperty = DependencyProperty.Register(
           "RecipeLst", typeof(ObservableCollection), typeof(RecipeLoadDose), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// Recipe列表
        /// 
        public ObservableCollection RecipeLst
        {
            get
            {
                return (ObservableCollection)this.GetValue(RecipeLstProperty);
            }
            set
            {
                this.SetValue(RecipeLstProperty, value);
            }
        }
        public static readonly DependencyProperty ReplenDatasProperty = DependencyProperty.Register(
           "ReplenDatas", typeof(ObservableCollection), typeof(RecipeLoadDose), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// ReplenDatas
        /// 
        public ObservableCollection ReplenDatas
        {
            get
            {
                return (ObservableCollection)this.GetValue(ReplenDatasProperty);
            }
            set
            {
                this.SetValue(ReplenDatasProperty, value);
            }
        }
        /// 
        /// 加载Recipe列表
        /// 
        /// 
        /// 
        private void Self_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                bool result = (bool)e.NewValue;
                if (result)
                {
                    if (!string.IsNullOrEmpty(RecipeType))
                    {
                        RecipeNodes = RecipeClient.Instance.Service.GetRecipesByType(RecipeType);
                        UpdateRecipeList();
                    }
                }
            }
        }
        /// 
        /// DosingRecipe选择
        /// 
        /// 
        /// 
        private void SelectRecipe1_Click(object sender, RoutedEventArgs e)
        {
            RecipeNode recipeNode = (RecipeNode)comboBox1.SelectedItem;
            if (recipeNode != null)
            {
                InvokeClient.Instance.Service.DoOperation($"{ModuleName}.LoadDosingRecipe", recipeNode.RecipeFullFileName, "Replen1", recipeNode.FileName);
            }
              
        }
        private void SelectRecipe2_Click(object sender, RoutedEventArgs e)
        {
            RecipeNode recipeNode = (RecipeNode)comboBox2.SelectedItem;
            if(recipeNode != null)
            {
                InvokeClient.Instance.Service.DoOperation($"{ModuleName}.LoadDosingRecipe", recipeNode.RecipeFullFileName, "Replen2", recipeNode.FileName);
            }          
        }
        private void SelectRecipe3_Click(object sender, RoutedEventArgs e)
        {
            RecipeNode recipeNode = (RecipeNode)comboBox3.SelectedItem;
            if (recipeNode != null)
            {
                InvokeClient.Instance.Service.DoOperation($"{ModuleName}.LoadDosingRecipe", recipeNode.RecipeFullFileName, "Replen3", recipeNode.FileName);
            }              
        }
        private void SelectRecipe4_Click(object sender, RoutedEventArgs e)
        {
            RecipeNode recipeNode = (RecipeNode)comboBox4.SelectedItem;
            if (recipeNode != null)
            {
                InvokeClient.Instance.Service.DoOperation($"{ModuleName}.LoadDosingRecipe", recipeNode.RecipeFullFileName, "Replen4", recipeNode.FileName);
            }
                
        }
        /// 
        /// 更新recipe下拉列表
        /// 
        private void UpdateRecipeList()
        {
            if (RecipeLst == null) RecipeLst = new ObservableCollection();
            RecipeLst.Clear();
            foreach(var item in RecipeNodes[0].Children)
            {
                RecipeLst.Add(item.Name);
            }
        }
        
    }
}