using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Xps.Packaging;
using System.Xml;
using FileAccess = System.IO.FileAccess;
using FileMode = System.IO.FileMode;
using System.IO.Packaging;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Xps;
using System.Windows;
using System.Windows.Media.Imaging;

namespace CreateXpsDoc
{
    public class RawXps
    {
        public static void BuildRaw()
        {
            // Example 15-23. Generating an XPS file from scratch

            using (XpsDocument xpsDoc = new XpsDocument("Out.xps", FileAccess.Write))
            {
                IXpsFixedDocumentSequenceWriter sequenceWriter =
                    xpsDoc.AddFixedDocumentSequence();
                IXpsFixedDocumentWriter docWriter = sequenceWriter.AddFixedDocument();
                IXpsFixedPageWriter pageWriter = docWriter.AddFixedPage();

                XmlWriter pageXml = pageWriter.XmlWriter;
                pageXml.WriteStartElement("FixedPage");
                pageXml.WriteAttributeString("xmlns",
                        "http://schemas.microsoft.com/xps/2005/06");
                pageXml.WriteAttributeString("Width", "793.7");
                pageXml.WriteAttributeString("Height", "1122.5");
                pageXml.WriteAttributeString("xml:lang", "en-GB");

                pageXml.WriteStartElement("Path");
                pageXml.WriteAttributeString("Data",
                    "M 10,550 L 396,164 782,550 396,936 z");
                pageXml.WriteAttributeString("Fill", "#ffff0000");

                pageXml.WriteEndElement();

                pageXml.WriteEndElement();

                pageWriter.Commit();
                docWriter.Commit();
                sequenceWriter.Commit();
            }

            // End of Example 15-23.
        }

        public static void BuildMixed()
        {
            // Example 15-24. Using both XPS API styles

            using (Package xpsPackage = Package.Open("Out.xps", FileMode.Create,
                                                     FileAccess.ReadWrite))
            using (XpsDocument doc = new XpsDocument(xpsPackage))
            {
                FixedPage page = new FixedPage();
                Path p = new Path();
                p.Data = StreamGeometry.Parse("M 10,550 L 396,164 782,550 396,936 z");
                p.Fill = Brushes.Red;
                page.Children.Add(p);

                TextBlock text = new TextBlock();
                text.Text = "XPS Output";
                text.FontSize = 150;
                page.Children.Add(text);

                XpsDocumentWriter dw = XpsDocument.CreateXpsDocumentWriter(doc);
                dw.Write(page);

                doc.CoreDocumentProperties.Description = "Some text and a red square";
            }

            // End of Example 15-24.
        }

        public static void SetCoreProps(XpsDocument xpsOutput)
        {
            // Example 15-25. Setting core document properties

            PackageProperties props = xpsOutput.CoreDocumentProperties;

            props.Creator = "Ian Griffiths";
            props.Title = "XPS Output";
            props.Subject = "Example XPS output document";
            props.Description = "XPS document generated by example from " +
                "the book 'Programming WPF' published by O'Reilly";
            props.Category = "Demo output";
            props.ContentStatus = "Final";
            props.Keywords = "XPS; Demo; WPF";

            // End of Example 15-25.
        }


        void AddThumbnailToXpsDocument(XpsDocument xpsOutput)
        {
            Size thumbnailSize = new Size(256, 256);
            Visual thumbnailVisual = CreateThumbnailVisual(thumbnailSize);

            XpsThumbnail docThumbnail = xpsOutput.AddThumbnail(XpsImageType.JpegImageType);
            WriteVisualAsThumbnail(thumbnailSize, thumbnailVisual, docThumbnail);

        }


        // Example 15-26. Adding a thumbnail to an XPS document

        Visual CreateThumbnailVisual(Size thumbnailSize)
        {
            Grid myThumbnail = new Grid();
            myThumbnail.Background = new LinearGradientBrush(
                Colors.LightBlue, Colors.White, 90);

            TextBlock thumbText = new TextBlock();
            thumbText.FontSize = 50;
            thumbText.Text = "My XPS Document";
            thumbText.TextAlignment = TextAlignment.Center;
            thumbText.VerticalAlignment = VerticalAlignment.Center;
            thumbText.TextWrapping = TextWrapping.Wrap;
            myThumbnail.Children.Add(thumbText);

            myThumbnail.Measure(thumbnailSize);
            myThumbnail.Arrange(new Rect(new Point(), thumbnailSize));

            return myThumbnail;
        }

        void WriteVisualAsThumbnail(Size thumbnailSize, Visual thumbnailVisual,
                                    XpsThumbnail thumbnail)
        {

            RenderTargetBitmap bmp = new RenderTargetBitmap(
                (int) thumbnailSize.Width, (int) thumbnailSize.Height,
                96, 96, PixelFormats.Default);
            bmp.Render(thumbnailVisual);

            JpegBitmapEncoder encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(bmp));

            encoder.Save(thumbnail.GetStream());
        }

        // End of Example 15-26.

        private static XpsDocument FastCompression()
        {
            // Example 15-32. Choosing fast compression

            XpsDocument doc = new XpsDocument("out.xps", FileAccess.Write, CompressionOption.Fast);

            // End of Example 15-32.

            return doc;
        }

    }
}
