using System;
using System.IO;
using System.Reflection;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

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

namespace PdfViewerSamples
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class FindText : UserControl
    {
        public FindText()
        {
            this.InitializeComponent();
            this.Loaded += FindText_Loaded;
            this.Unloaded += FindText_Unloaded;
        }

        void FindText_Unloaded(object sender, RoutedEventArgs e)
        {
            pdfViewer.CloseDocument();
        }

        void FindText_Loaded(object sender, RoutedEventArgs e)
        {
            Assembly asm = typeof(FindText).GetTypeInfo().Assembly;
            Stream stream = asm.GetManifestResourceStream("PdfViewerSamples.Files.C1XapOptimizer.pdf");
            pdfViewer.LoadDocument(stream);
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            pdfViewer.FindText = txtFindText.Text;
            pdfViewer.FindNextCommand.Execute(null);
        }

        private void btnPrevious_Click(object sender, RoutedEventArgs e)
        {
            pdfViewer.FindText = txtFindText.Text;
            pdfViewer.FindPreviousCommand.Execute(null);
        }

        private async void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.FileTypeFilter.Add(".pdf");

            StorageFile file = await openPicker.PickSingleFileAsync();
            if (file != null)
            {
                Stream stream = await file.OpenStreamForReadAsync();
                pdfViewer.LoadDocument(stream);
            }
        }
    }
}
