using System;
using System.Collections.Generic;
using System.Linq;
using Aitex.Core.RT.Log;
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;
using ExcelLibrary.SpreadSheet;
using Venus_MainPages.ViewModels;
//using Xceed.Wpf.DataGrid;
using System.IO;
using static Venus_MainPages.ViewModels.ProcessHistoryViewModel;
using System.Collections.ObjectModel;
using Aitex.Core.UI.View.Smart;
using System.Drawing;
using LiveCharts.Wpf;
namespace Venus_MainPages.Views
{
    /// 
    /// ProcessHistoryView.xaml 的交互逻辑
    /// 
    public partial class ProcessHistoryView : UserControl
    {
        public ProcessHistoryView()
        {
            InitializeComponent();
            this.DataContext = new ProcessHistoryViewModel();
        }
        private ProcessHistoryViewModel _viewModel;
        private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RecipeItem item = (dataGrid_RecipeList.SelectedItem) as RecipeItem;
            _viewModel = (ProcessHistoryViewModel)DataContext;
            _viewModel.UpdateData(item);
            ButtonExportData.IsEnabled = item != null;
            ButtonExportList.IsEnabled = item != null;
        }
        private void buttonLotListExport_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                _viewModel = (ProcessHistoryViewModel)DataContext;
                ObservableCollection DataRecipeList = _viewModel.Recipes;
                if (DataRecipeList == null || DataRecipeList.Count == 0) return;
                Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
                dlg.DefaultExt = ".xls"; // Default file extension 
                dlg.Filter = "数据表格文件|*.xls"; // Filter files by extension 
                dlg.FileName = string.Format("RecipeList{0}", _viewModel.StartDateTime.ToString("yyyyMMdd"));
                Nullable result = dlg.ShowDialog();// Show open file dialog box
                if (result != true) // Process open file dialog box results
                    return;
                if (File.Exists(dlg.FileName))
                {
                    File.Delete(dlg.FileName);
                }
                Workbook workbook = new Workbook();
                Worksheet worksheet = new Worksheet(_viewModel.StartDateTime.ToString("yyyyMMdd"));
                int col = 0;
                worksheet.Cells[0, col++] = new Cell("Start");
                worksheet.Cells[0, col++] = new Cell("End");
                worksheet.Cells[0, col++] = new Cell("Chamber");
                worksheet.Cells[0, col++] = new Cell("Guid");
                worksheet.Cells[0, col++] = new Cell("WaferID");
                worksheet.Cells[0, col++] = new Cell("Recipe");
                worksheet.Cells[0, col++] = new Cell("Status");
                for (int i = 0; i < DataRecipeList.Count; i++)
                {
                    int colCount = 0;
                    worksheet.Cells[i + 1, colCount++] = new Cell(DataRecipeList[i].StartTime);
                    worksheet.Cells[i + 1, colCount++] = new Cell(DataRecipeList[i].EndTime);
                    worksheet.Cells[i + 1, colCount++] = new Cell(DataRecipeList[i].Chamber);
                    worksheet.Cells[i + 1, colCount++] = new Cell(DataRecipeList[i].Guid);
                    worksheet.Cells[i + 1, colCount++] = new Cell(DataRecipeList[i].RecipeRunGuid);
                    worksheet.Cells[i + 1, colCount++] = new Cell(DataRecipeList[i].Recipe);
                    worksheet.Cells[i + 1, colCount++] = new Cell(DataRecipeList[i].Status);
                }
                workbook.Worksheets.Add(worksheet);
                workbook.Save(dlg.FileName);
            }
            catch (Exception ex)
            {
                LOG.WriteExeption(ex.Message, ex);
            }
        }
        private void OnChangeLineColor(object sender, RoutedEventArgs e)
        {
            var btn = (Button)sender;
            if (btn != null)
            {
                int dataId = (int)btn.Tag;
                var dlg = new System.Windows.Forms.ColorDialog();
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    var newColor = new System.Windows.Media.Color() { A = dlg.Color.A, B = dlg.Color.B, G = dlg.Color.G, R = dlg.Color.R };
                    System.Drawing.Color _newColor = System.Drawing.Color.FromArgb(newColor.A, newColor.R, newColor.G, newColor.B);
                    _viewModel = (ProcessHistoryViewModel)DataContext;
                    var item = _viewModel.PdKeyDataCollection.ToList().Find(t => t.UniqueId == dataId);
                    item.Color = new SolidColorBrush(newColor); 
                    _viewModel.OnSearchData();
                }
            }
        }
        private void buttonLotDetailsExport_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                RecipeItem log = (dataGrid_RecipeList.SelectedItem) as RecipeItem;
                if (log == null)
                {
                    MessageBox.Show("没有数据,先从列表中选择一个批次");
                    return;
                }
                _viewModel = (ProcessHistoryViewModel)DataContext;
                Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
                dlg.DefaultExt = ".xls"; // Default file extension 
                dlg.Filter = "数据表格文件|*.xls"; // Filter files by extension 
                dlg.FileName = string.Format("{0}-{1}", _viewModel.StartDateTime.ToString("yyyyMMdd"), log.Recipe.Replace(" ","").Replace("/", ""));
                Nullable result = dlg.ShowDialog();// Show open file dialog box
                if (result != true) // Process open file dialog box results
                    return;
                if (File.Exists(dlg.FileName))
                {
                    File.Delete(dlg.FileName);
                }
                Workbook workbook = new Workbook();
                Worksheet worksheet = new Worksheet(log.Recipe);
                Dictionary colIndex = new Dictionary();
                Dictionary rowIndex = new Dictionary();
                int colCount = 0;
                int rowCount = 0;
                foreach (var item in _viewModel.ProcessData)
                { 
                    if (!rowIndex.ContainsKey(item.dateTime.ToString("G")))
                    {
                        rowCount++;
                        rowIndex[item.dateTime.ToString("G")] = rowCount;
                        worksheet.Cells[rowCount, 0] = new Cell(item.dateTime.ToString("G"));
                    }
                    if (!colIndex.ContainsKey(item.dbName))
                    {
                        colCount++;
                        colIndex[item.dbName] = colCount;
                        worksheet.Cells[0, colCount] = new Cell(item.dbName);
                    }
                    worksheet.Cells[rowIndex[item.dateTime.ToString("G")], colIndex[item.dbName]] = new Cell(item.value);
                }
                workbook.Worksheets.Add(worksheet);
                workbook.Save(dlg.FileName);
            }
            catch (Exception ex)
            {
                
                LOG.WriteExeption(ex.Message, ex);
            }
        }
    }
}