using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Telerik.Windows.Documents.FormatProviders;
using Telerik.Windows.Documents.Model;
using Telerik.Windows.Examples;

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            DataContext = new MyBusinessObjects().GetData(100);
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var saveDialog = new SaveFileDialog();
            saveDialog.DefaultExt = "*.pdf";
            saveDialog.Filter = "Adobe PDF Document (*.pdf)|*.pdf";

            if (saveDialog.ShowDialog() == true)
            {
                var table = new Table(1, RadGridView1.Columns.Count);

                var document = new RadDocument();
                document.Sections.Add(new Section());
                document.Sections.First.Blocks.Add(table);

                var headerCell = table.Rows.First.Cells.First;
                this.AddCellDataSpan(headerCell, "Name", Colors.Gray, Colors.Orange, Telerik.Windows.Documents.Layout.RadTextAlignment.Center);
                headerCell = (TableCell)headerCell.NextSibling;
                this.AddCellDataSpan(headerCell, "Date", Colors.Gray, Colors.Orange, Telerik.Windows.Documents.Layout.RadTextAlignment.Center);
                headerCell = (TableCell)headerCell.NextSibling;
                this.AddCellDataSpan(headerCell, "Unit Price", Colors.Gray, Colors.Orange, Telerik.Windows.Documents.Layout.RadTextAlignment.Center);
                headerCell = (TableCell)headerCell.NextSibling;
                this.AddCellDataSpan(headerCell, "Discontinued", Colors.Gray, Colors.Orange, Telerik.Windows.Documents.Layout.RadTextAlignment.Center);

                foreach (var item in RadGridView1.Items.OfType<MyBusinessObject>())
                {
                    var row = table.AddRow();

                    int index = 0;
                    foreach (var cell in row.Cells)
                    {
                        if (index == 0) this.AddCellDataSpan(cell, item.Name, Colors.Black);
                        else if (index == 1) this.AddCellDataSpan(cell, item.Date.ToShortDateString(), Colors.Green);
                        else if (index == 2) this.AddCellDataSpan(cell, string.Format("{0:0.00}", item.UnitPrice), Colors.Magenta);
                        else if (index == 3) this.AddCellDataSpan(cell, item.Discontinued ? "yes" : "no", item.Discontinued ? Colors.Red : Colors.Cyan);

                        index++;
                    }
                }

                document.LayoutMode = DocumentLayoutMode.Paged;

                document.Measure(RadDocument.MAX_DOCUMENT_SIZE);
                document.Arrange(new RectangleF(PointF.Empty, document.DesiredSize));

                var extension = System.IO.Path.GetExtension(saveDialog.SafeFileName);

                var provider = DocumentFormatProvidersManager.GetProviderByExtension(extension);

                if (provider == null)
                {
                    MessageBox.Show("Unsupported file format.");
                    return;
                }

                try
                {
                    using (var output = saveDialog.OpenFile())
                    {
                        provider.Export(document, output);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Unable to save the file: " + ex.ToString());
                }
            }
        }

        private void AddCellDataSpan(TableCell cell, string spanData, Color textColor)
        {
            this.AddCellDataSpan(cell, spanData, Colors.Transparent, textColor, Telerik.Windows.Documents.Layout.RadTextAlignment.Left);
        }

        private void AddCellDataSpan(TableCell cell, string spanData, Color cellBackgroundColor, Color textColor, Telerik.Windows.Documents.Layout.RadTextAlignment cellTextAligment)
        {
            var paragraph = new Paragraph();
            paragraph.TextAlignment = cellTextAligment;
            cell.Blocks.Add(paragraph);

            cell.Background = cellBackgroundColor;

            var span = new Span();
            span.Text = spanData;
            span.ForeColor = textColor;

            paragraph.Inlines.Add(span);
        }
    }
}
