using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Globalization;
using Aitex.Core.RT.Log;
using Aitex.Core.Common;
using MECF.Framework.UI.Client.CenterViews.Editors.Recipe;
using System.Collections.ObjectModel;
using Caliburn.Micro;
using MECF.Framework.Common.OperationCenter;
using MECF.Framework.UI.Client.CenterViews.Editors.Sequence;
using MECF.Framework.UI.Client.ClientBase;
using OpenSEMI.ClientBase;

namespace MECF.Framework.UI.Client.CenterViews.Editors
{

    /// <summary>
    /// Interaction logic for InputDialogBox.xaml
    /// </summary>
    public partial class SlotEditorDialogBox : Window
    {
        public SlotEditorDialogBox()
        {
            InitializeComponent();

            DataContext = this;
        }

        public static readonly DependencyProperty ModuleIDProperty = DependencyProperty.Register(
                               "ModuleID", typeof(string), typeof(SlotEditorDialogBox),
                               new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
        public static readonly DependencyProperty RecipeNameProperty = DependencyProperty.Register(
                                "RecipeName", typeof(string), typeof(SlotEditorDialogBox),
                                new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
        public static readonly DependencyProperty TrayProcessCountProperty = DependencyProperty.Register(
                             "TrayProcessCount", typeof(int), typeof(SlotEditorDialogBox),
                             new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender));
        public static readonly DependencyProperty SlotIDProperty = DependencyProperty.Register(
                             "SlotID", typeof(int), typeof(SlotEditorDialogBox),
                             new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender));

       

        public string TrayBelongTo { get; set; }

        public string ModuleID
        {
            get
            {
                return (string)this.GetValue(ModuleIDProperty);
            }
            set
            {
                this.SetValue(ModuleIDProperty, value);
            }
        }

        public int TrayProcessCount
        {
            get
            {
                return (int)this.GetValue(TrayProcessCountProperty);
            }
            set
            {
                this.SetValue(TrayProcessCountProperty, value);
            }
        }

        public string RecipeName
        {
            get
            {
                return (string)this.GetValue(RecipeNameProperty);
            }
            set
            {
                this.SetValue(RecipeNameProperty, value);
            }
        }

        public int SlotID
        {
            get
            {
                return (int)this.GetValue(SlotIDProperty);
            }
            set
            {
                this.SetValue(SlotIDProperty, value);
            }
        }


        private void RecipeSelect_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!TrayBelongTo.Contains("PM"))
                {
                    DialogBox.ShowDialog(DialogButton.OK, DialogType.WARNING,
                        "Recipe follow PM,tray not set PM, Please set TrayInfo");
                    return;
                }
                var dialog = new RecipeSelectDialogViewModel(TrayBelongTo);
                dialog.DisplayName = "Select Recipe";

                var wm = new WindowManager();
                var bret = wm.ShowDialog(dialog);
                if ((bool)bret)
                {
                    RecipeName = dialog.DialogResult;
                }
            }
            catch (Exception ex)
            {
                LOG.Error(ex.Message);
            }
        }


        private void ButtonSet_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                int processCount = TrayProcessCount;
                Int32.TryParse(txtTrayProcessCount.Text, out processCount);

                InvokeClient.Instance.Service.DoOperation("AlterWaferInfo", ModuleID, SlotID, RecipeName, processCount);
                this.DialogResult = true;
            }
            catch (Exception ex)
            {
                LOG.Error(ex.Message);
            }
        }


        private void ButtonCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            this.txtRecipeName.Text = RecipeName;
            this.txtTrayProcessCount.Text = TrayProcessCount.ToString();
            base.OnRender(drawingContext);
        }
    }
}
