using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Streams;
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.Media.Imaging;
using Windows.UI.Xaml.Navigation;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Graphics.Imaging;
using Windows.UI.Core;

using C1.Xaml.Word;
using C1.Xaml.Word.Objects;
using C1.Xaml.RichTextBox;
using C1.Xaml.RichTextBox.Documents;

namespace WordSamples
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class ImagesPage : Page
    {
        C1WordDocument word;

        public ImagesPage()
        {
            this.InitializeComponent();
            this.Loaded += ImagesPage_Loaded;

            word = new C1WordDocument(PaperKind.Letter);
            word.Clear();
        }

        async void ImagesPage_Loaded(object sender, RoutedEventArgs e)
        {
            progressRing.IsActive = true;
            await CreateDocumentImages(word);
            WordUtils.SetDocumentInfo(word, Strings.TableOfContentsDocumentTitle);
            //c1RichTextBox1.Document = new RtfFilter().ConvertToDocument(word.ToRtfText());
            progressRing.IsActive = false;
        }
        
        async Task CreateDocumentImages(C1WordDocument word)
        {
            // calculate page rect (discounting margins)
            Rect rcPage = WordUtils.PageRectangle(word);
            InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
            // load image into writeable bitmap
            WriteableBitmap wb = new WriteableBitmap(880, 660);
            var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///WordSamplesLib/Assets/pic.jpg"));
            
            wb.SetSource(await file.OpenReadAsync());
            Rect rcPic = new Rect(new Point(0, 0), new Point(word.PageSize.Width, word.PageSize.Height));

            // draw on page preserving aspect ratio
            word.DrawImage(wb, rcPic);
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            WordUtils.Save(word);
        }
    }
}
