using CyberX8_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 CyberX8_Themes.UserControls
{
    /// <summary>
    /// RecipeLoadDose.xaml 的交互逻辑
    /// </summary>
    public partial class RecipeLoadDose : UserControl
    {
        public RecipeLoadDose()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty ModuleNameProperty = DependencyProperty.Register(
           "ModuleName", typeof(string), typeof(RecipeLoadDose), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// 模块名称
        /// </summary>
        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));
        /// <summary>
        /// Replen数量
        /// </summary>
        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));
        /// <summary>
        /// Recipe类型
        /// </summary>
        public string RecipeType
        {
            get
            {
                return (string)this.GetValue(RecipeTypeProperty);
            }
            set
            {
                this.SetValue(RecipeTypeProperty, value);
            }

        }

        public static readonly DependencyProperty RecipeNodesProperty = DependencyProperty.Register("RecipeNodes", typeof(ObservableCollection<RecipeNode>), typeof(RecipeLoadDose));
        /// <summary>
        /// Reicpe节点
        /// </summary>
        public ObservableCollection<RecipeNode> RecipeNodes
        {
            get
            {
                return (ObservableCollection<RecipeNode>)this.GetValue(RecipeNodesProperty); 
            }
            set 
            { 
                this.SetValue(RecipeNodesProperty, value); 
            }
        }

        public static readonly DependencyProperty RecipeLstProperty = DependencyProperty.Register(
           "RecipeLst", typeof(ObservableCollection<string>), typeof(RecipeLoadDose), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// Recipe列表
        /// </summary>
        public ObservableCollection<string> RecipeLst
        {
            get
            {
                return (ObservableCollection<string>)this.GetValue(RecipeLstProperty);
            }
            set
            {
                this.SetValue(RecipeLstProperty, value);
            }
        }
        public static readonly DependencyProperty ReplenDatasProperty = DependencyProperty.Register(
           "ReplenDatas", typeof(ObservableCollection<ReplenData>), typeof(RecipeLoadDose), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// ReplenDatas
        /// </summary>
        public ObservableCollection<ReplenData> ReplenDatas
        {
            get
            {
                return (ObservableCollection<ReplenData>)this.GetValue(ReplenDatasProperty);
            }
            set
            {
                this.SetValue(ReplenDatasProperty, value);
            }
        }
        /// <summary>
        /// 加载Recipe列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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();
                    }
                }
            }
        }

        /// <summary>
        /// DosingRecipe选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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);
            }
                
        }
        /// <summary>
        /// 更新recipe下拉列表
        /// </summary>
        private void UpdateRecipeList()
        {
            if (RecipeLst == null) RecipeLst = new ObservableCollection<string>();
            RecipeLst.Clear();
            foreach(var item in RecipeNodes[0].Children)
            {
                RecipeLst.Add(item.Name);
            }

        }

        
    }
}