using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml;
using Aitex.Core.Account;
using Aitex.Core.RT.Log;
using Aitex.Core.UI.View.Common;
using Aitex.UI.RecipeEditor.View;
using ExcelLibrary.SpreadSheet;
using MECF.Framework.Common.DataCenter;
using MECF.Framework.Common.Equipment;
using MECF.Framework.UI.Core.Accounts;
namespace VirgoUI.Client.Models.PMs
{
    /// 
    /// Interaction logic for RecipeEditorView.xaml
    /// 
    public partial class RecipeEditorView : UserControl
    {
        private string _culture;
        private string tragetFilePath = "";
        private object OriginalItem;
        private RecipeEditorViewModel _viewModel;
        private int MenuPermission;
        private ViewPermission _permission;
        private Point _lastMouseDown;
        public RecipeEditorView()
        {
            InitializeComponent();
            this.buttonSaveTableRecipe.ContextMenu = null;
            this.DataContext = _viewModel = new RecipeEditorViewModel(new UiRecipeManager(), ModuleName.System.ToString());
            this.Loaded += new RoutedEventHandler(RecipeEditorView_Loaded);
            this.Unloaded += new RoutedEventHandler(RecipeEditorView_Unloaded);
            //VirgoUiSystem.Instance.CultureChanged += CultureChanged;
        }
        private void CultureChanged(string s)
        {
            _culture = s;
            this.tableRecipeGrid.UpdateCultureResource(s);
        }
        static DependencyObject VisualUpwardSearch(DependencyObject source)
        {
            while (source != null && source.GetType() != typeof(T))
                source = VisualTreeHelper.GetParent(source);
            return source;
        }
        void RecipeEditorView_Unloaded(object sender, RoutedEventArgs e)
        {
        }
        void RecipeEditorView_Loaded(object sender, RoutedEventArgs e)
        {
            buttonSaveTableRecipe.Click += (obj, args) => { SetMenu.IsOpen = true; };
            SaveToSingle.Header = $"Save To {((RecipeEditorViewModel)this.DataContext).SystemName}";
            //SaveToDouble.Header = "Save To PMA And PMB";
            _viewModel.ChamberId = ((RecipeEditorViewModel)this.DataContext).SystemName;
            MenuPermission = ClientApp.Instance.GetPermission($"Recipe{_viewModel.ChamberId}");
            if (_culture != Thread.CurrentThread.CurrentCulture.Name)
            {
                _culture = Thread.CurrentThread.CurrentCulture.Name;
                this.tableRecipeGrid.UpdateCultureResource(_culture);
            }
            _viewModel.UpdateAllConfig();
            tableRecipeGrid.SetEnableBarcode(_viewModel.IsBarcodeEnabled);
            tableRecipeGrid.SetUser(ClientApp.Instance.UserContext.LoginName);
            tableRecipeGrid.SetEndPointDefaultValue((string)QueryDataClient.Instance.Service.GetConfig("System.EndPoint.EndPointDefaultValue"));
            UpdateRecipeFileList();
            UpdateProcessRecipeFileList();
            UpdateCleanRecipeFileList();
            this.Name = "recipeEditor";
            _permission = ViewPermission.FullyControl;//this.GetPermission();
            switch (_permission)
            {
                case ViewPermission.Readonly:
                    buttonReloadTableRecipe.IsEnabled = false;
                    tableRecipeGrid.IsEnabled = false;
                    buttonSaveTableRecipe.IsEnabled = false;
                    break;
                default:
                    break;
            }
        }
        private void treeViewRcpList_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs