using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Xml.Linq;

using C1.Silverlight;
using C1.Silverlight.Pdf;
using C1.Silverlight.DataGrid;
using C1.Silverlight.DataGrid.Excel;

namespace C1DataGrid_Demo2010
{
    public partial class ExportFormats : System.Windows.Controls.UserControl
    {
        public ExportFormats()
        {
            InitializeComponent();

            DataContext = Person.Generate(100);
        }

        #region Excel

        private void btnExcel_Click(object sender, RoutedEventArgs e)
        {
            var options = new ExcelSaveOptions()
            {
                FileFormat = ExcelFileFormat.Xlsx, // change this to offer a different Excel format
                KeepColumnWidths = true,
                KeepRowHeights = true
            };

            var excelExt = options.FileFormat.ToString();

            var dialog = new System.Windows.Controls.SaveFileDialog()
            {
                DefaultExt = "*." + excelExt,
                Filter = "Excel " + excelExt + " (*." + excelExt + ")|*." + excelExt  + "|All files (*.*)|*.*",
            };

            if (dialog.ShowDialog() == false) return;

            using (var stream = dialog.OpenFile())
            {
                grid.Save(stream, options);
            }
        }

        #endregion

        #region Pdf

        private void btnPdf_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new System.Windows.Controls.SaveFileDialog();

            dialog.DefaultExt = "*.pdf";
            dialog.Filter = "Portable document format (*.pdf)|*.pdf|All files (*.*)|*.*";

            if (dialog.ShowDialog() == false) return;

            using (var s = dialog.OpenFile())
            {
                var pdfDoc = new C1PdfDocument();
                pdfDoc.DocumentInfo.Title = "Sample DataGrid";
                //pdfDoc.PageSize = new Size(100, 100);
                //pdfDoc.PageRectangle = new Rect(10, 10, 80, 80);
                grid.ToPdf(pdfDoc);
                pdfDoc.Save(s);
            }
        }

        #endregion

        #region Html

        private void btnHtml_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new System.Windows.Controls.SaveFileDialog();

            dialog.DefaultExt = "*.html";
            dialog.Filter = "Web Page (*.html)|*.html|All files (*.*)|*.*";

            if (dialog.ShowDialog() == false) return;

            using (var sw = new StreamWriter(dialog.OpenFile()))
            {
                var htmlContent = grid.ToHtml("Sample DataGrid");
                sw.Write(htmlContent);
            }
        }

        #endregion

        private void printMenuItem_Click(object sender, SourcedEventArgs e)
        {
            ScaleMode scaleMode = (ScaleMode)Enum.Parse(typeof(ScaleMode), ((C1MenuItem)sender).Tag.ToString(), 
                false);
            grid.Print("C1DataGrid", scaleMode, new Thickness(20), 10);
        }

    }

}
