using System;
using System.IO;
using System.Windows;
using InABox.Clients;
using InABox.Core;
using Syncfusion.Windows.Tools.Controls;
using Syncfusion.XlsIO;

namespace InABox.DynamicGrid
{
    public partial class DynamicSpreadsheet : RibbonWindow
    {
        public DynamicSpreadsheet()
        {
            InitializeComponent();


            var filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "user.xlsx");
            CreateSpreadsheet(filename);
            Spreadsheet.Open(filename);
            
            //Spreadsheet.Create(1);
            //Spreadsheet.ActiveSheet = Spreadsheet.Workbook.Worksheets[0];
            //var table = new Client<User>().Query().ToDataTable();
            //Spreadsheet.ActiveSheet.ImportDataTable(table,true,1,1);
            // Spreadsheet.ActiveGrid.InvalidateCells();

        }

        private void CreateSpreadsheet(String filename)
        {
            using (ExcelEngine excelEngine = new ExcelEngine())
            {
                IApplication application = excelEngine.Excel;
                application.DefaultVersion = ExcelVersion.Excel2013;
                IWorkbook workbook = application.Workbooks.Create(1);
                IWorksheet worksheet = workbook.Worksheets[0];
                
                var table = new Client<User>().Query().ToDataTable();
                worksheet.ImportDataTable(table, true, 1, 1);
                workbook.SaveAs(filename);
            }
        }
    }
}