using C1.Xaml.Pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace PdfSamples.Samples
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class BasicTextPage : Page
    {
        C1PdfDocument pdf;

        public BasicTextPage()
        {
            this.InitializeComponent();
            this.Loaded += BasicText_Loaded;

            pdf = new C1PdfDocument(PaperKind.Letter);
            pdf.Clear();
        }

        async void BasicText_Loaded(object sender, RoutedEventArgs e)
        {
            progressRing.IsActive = true;
            CreateDocumentText(pdf);

            await c1PdfViewer1.LoadDocumentAsync(PdfUtils.SaveToStream(pdf));
            progressRing.IsActive = false;
        }

        /// <summary>
        /// Shows how to position and align text
        /// </summary>
        static void CreateDocumentText(C1PdfDocument pdf)
        {
            // use landscape for more impact
            pdf.Landscape = true;

            // measure and show some text 
            var text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmood tempor incididunt ut labore magna aliqua. Ut enim ad minim veniam, quis nostrud excecitation ullmanco laboris. nisi ut alquip ex ea commodo consequat.";
            var font = new Font("Segoe UI Light", 14, PdfFontStyle.Italic);

            // create StringFormat used to set text alignment and line spacing
            var fmt = new StringFormat();
            fmt.LineSpacing = -1.5; // 1.5 char height
            fmt.Alignment = HorizontalAlignment.Center;

            // measure it
            var sz = pdf.MeasureString(text, font, 72 * 3, fmt);
            var rc = new Rect(pdf.PageRectangle.Width / 2, 72, sz.Width, sz.Height);
            rc = PdfUtils.Offset(rc, 110, 0);

            // draw a rounded frame
            rc = PdfUtils.Inflate(rc, 0, 0);
            pdf.FillRectangle(Colors.Teal, rc, new Size(0, 0));
            //pdf.DrawRectangle(new Pen(Colors.DarkGray, 5), rc, new Size(0, 0));
            rc = PdfUtils.Inflate(rc, -10, -10);

            // draw the text
            pdf.DrawString(text, font, Colors.White, rc, fmt);

            // now draw some text rotating about the center of the page
            rc = pdf.PageRectangle;
            rc = PdfUtils.Offset(rc, rc.Width / 2.2, rc.Height / 2.5);

            // build StringFormat used to rotate the text
            fmt = new StringFormat();

            // rotate the string in small increments
            var step = 6;
            text = "C1PDF works in Windows Runtime!";
            for (int i = 0; i <= 360; i += step)
            {
                fmt.Angle = i;
                font = new Font("Courier New", 8 + i / 30.0, PdfFontStyle.Bold);
                byte b = (byte)(255 * (1 - i / 360.0));
                pdf.DrawString(text, font, Color.FromArgb(0xff, b, b, b), rc, fmt);
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            PdfUtils.Save(pdf);
        }
    }
}
