using System;
using System.IO;
using System.Threading.Tasks;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Graphics.Printing;
using Windows.Graphics.Printing.OptionDetails;
using FooEditEngine.UWP;

// 空白ページのアイテム テンプレートについては、http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 を参照してください

namespace Test
{
    /// <summary>
    /// それ自体で使用できる空白ページまたはフレーム内に移動できる空白ページ。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        MainViewModel vm = new MainViewModel();
        public MainPage()
        {
            this.InitializeComponent();
            this.DataContext = this.vm;
            this.fooTextBox.Document = this.vm.CurrentDocument;
            PrintManager.GetForCurrentView().PrintTaskRequested += MainPage_PrintTaskRequested;
        }

        void MainPage_PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
        {
            IAsyncAction async = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                var source = new DocumentSource(this.fooTextBox.Document, new FooEditEngine.Padding(20, 20, 20, 20), this.fooTextBox.FontFamily.Source, this.fooTextBox.FontSize);
                source.ParseHF = (s,e)=> { return e.Original; };
                source.Header = "header";
                source.Fotter = "footer";
                source.Forground = this.fooTextBox.Foreground;
                source.Keyword1 = this.fooTextBox.Keyword1;
                source.Keyword2 = this.fooTextBox.Keyword2;
                source.Literal = this.fooTextBox.Literal;
                source.Comment = this.fooTextBox.Comment;
                source.Url = this.fooTextBox.URL;

                PrintTask task = null;
                task = args.Request.CreatePrintTask("test", (e) =>
                {
                    e.SetSource(source);
                });
                task.Completed += async (s, e) => {
                    source.Dispose();
                    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
                        System.Diagnostics.Debug.WriteLine("finished printing");
                    });
                };
                PrintOptionBuilder<DocumentSource> builder = new PrintOptionBuilder<DocumentSource>(source);
                builder.BuildPrintOption(PrintTaskOptionDetails.GetFromPrintTaskOptions(task.Options));
            });
            Task t = WindowsRuntimeSystemExtensions.AsTask(async);
            t.Wait();
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            var filepicker = new FileOpenPicker();
            filepicker.FileTypeFilter.Add(".txt");
            var file = await filepicker.PickSingleFileAsync();
            if(file != null)
            {
                using (var ws = await file.OpenAsync(FileAccessMode.Read))
                using (var fs = new StreamReader(ws.AsStream()))
                {
                    await this.fooTextBox.LoadFileAsync(fs, null);
                }
            }
        }

        private async void Print_Button_Click(object sender, RoutedEventArgs e)
        {
            await PrintManager.ShowPrintUIAsync();
        }
    }
}
