using Caliburn.Micro;
using DocumentFormat.OpenXml.Drawing.Diagrams;
using MECF.Framework.UI.Client.RecipeEditorLib.RecipeModel;
using OpenSEMI.ClientBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;
using Xceed.Wpf.Toolkit;

namespace MECF.Framework.UI.Client.CenterViews.Editors.Recipe
{
    public class RecipeHistoryViewModel : DialogViewModel<string>
    {
        private RecipeHistory recipeMemory;

        public bool isCompare;

        public List<RecipeHistory> RecipeMemorieList { get; set; }

        public List<RecipeHistory> SelectRecipeMemorieList { get; set; }

        public RecipeHistory SelectRecipeHistory { get; set; }

        public RecipeHistoryViewModel(string recipeName)
        {
            recipeMemory = new RecipeHistory();
            RecipeMemorieList = recipeMemory.QueryDB(recipeName);
            SelectRecipeMemorieList = new List<RecipeHistory>() ;
        }

        public RecipeHistory GetXML(RecipeHistory recipeHistory)
        {
            return recipeHistory.QueryDB(recipeHistory) ?? null;
        }

        public void SelChanged(bool selItems)
        {
            if (selItems)
            {
                SelectRecipeMemorieList.Add(SelectRecipeHistory);
            }
            else
            {
                if (SelectRecipeMemorieList.Contains(SelectRecipeHistory))
                    SelectRecipeMemorieList.Remove(SelectRecipeHistory);
                
            }
        }

        public void OK()
        {
            if (SelectRecipeMemorieList.Count > 2)
            {
                MessageBox.Show("Only 2 comparisons are supported");
                return;
            }
            if (SelectRecipeMemorieList.Count>0)
            {
                for (int i = 0; i < SelectRecipeMemorieList.Count; i++)
                {
                    SelectRecipeMemorieList[i] = GetXML(SelectRecipeMemorieList[i]);
                }
            }
            IsCancel = false;
            TryClose(true);
        }

        public void Cancel()
        {
            //SelectRecipeMemorieList.Clear();
            IsCancel = true;
            TryClose(false);
        }
    }

    /// <summary>
    /// datagrid   自动生成行号
    /// </summary>
    public class RowToIndexConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            DataGridRow row = value as DataGridRow;
            if (row != null)
                return row.GetIndex() + 1;
            else
                return -1;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
