using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Security;
using C1.Silverlight.Pdf;
using C1.Silverlight.RichTextBox.PdfFilter;
using C1.Silverlight.RichTextBox.Documents;
using C1.Silverlight.RichTextBox;
using System.IO;
using C1.Silverlight;
using System.Windows.Printing;
using System.Reflection;

namespace Printing
{
    public partial class MainPage : UserControl
    {
        public PaperKind PaperKind { get; set; }
        public Thickness PrintMargin { get; set; }

        public MainPage()
        {
            InitializeComponent();
            
            var stream = Application.GetResourceStream(new Uri("/" + new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + ";component/Resources/dickens.htm", UriKind.Relative)).Stream;
            rtb.Html = new StreamReader(stream).ReadToEnd();
            rtb.Document.Blocks.Insert(0, new C1Paragraph
            {
                Children = 
                {
                    new C1Run 
                    {
                        Text = "Note that pdf export only supports Arial, Times New Roman and Courier New fonts. Also, it doesn't yet support lists, images or Unicode characters.",
                        FontWeight = FontWeights.Bold,
                        FontSize = 16,
                        Padding = new Thickness(2),
                    },
                },
                TextAlignment = C1TextAlignment.Center,
                Background = new SolidColorBrush(Color.FromArgb(255, 255, 255, 204)),
                Margin = new Thickness(20),
            });

            toolbar.RichTextBox = rtb;
            toolbar.FontFamilies.Clear();
            toolbar.FontFamilies.Add(new FontFamily("Arial"));
            toolbar.FontFamilies.Add(new FontFamily("Times New Roman"));
            toolbar.FontFamilies.Add(new FontFamily("Courier New"));
         
            rtb.LayoutUpdated += new EventHandler(richTB_LayoutUpdated);

            PrintMargin = new Thickness(72);
        }

        bool firstLayout = true;
        void richTB_LayoutUpdated(object sender, EventArgs e)
        {
            if (firstLayout && rtb.PrintPageLayout != null)
            {
                firstLayout = false;

                var pageWidth = rtb.PrintPageLayout.Width + rtb.PrintPageLayout.Padding.Left + rtb.PrintPageLayout.Padding.Right;
                rtb.Zoom = Math.Max(Math.Min(rtb.ActualWidth / pageWidth, 1), 0.3);
            }
        }

        private void PdfSave(object sender, RoutedEventArgs e)
        {
            var d = new SaveFileDialog();
            d.Filter = "Pdf Files | *.pdf";
            d.DefaultExt = "pdf";
            bool showDialog;
            try
            {
                showDialog = d.ShowDialog() == true;
            }
            catch (SecurityException)
            {
                return;
            }

            if (!showDialog)
            {
                return;
            }

            var pdf = new C1PdfDocument(PaperKind.Custom);
            pdf.ConformanceLevel = PdfAConformanceLevel.PdfA2b;
            PdfFilter.PrintDocument(rtb.Document, pdf, PrintMargin);
            using (var stream = d.OpenFile())
            {
                pdf.Save(stream);
            }

        }

        private void Print(object sender, RoutedEventArgs e)
        {
            var viewManager = new C1RichTextViewManager
            {
                Document = rtb.Document,
                PresenterInfo = rtb.ViewManager.PresenterInfo
            };
            var print = new PrintDocument();
            int presenter = 0;
            print.PrintPage += (s, printArgs) =>
            {
                var element = (FrameworkElement)printTemplate.LoadContent();
                element.DataContext = viewManager.Presenters[presenter];
                printArgs.PageVisual = element;
                printArgs.HasMorePages = ++presenter < viewManager.Presenters.Count;
            };
            print.Print("A Christmas Carol");
        }
    }
}
