using System;
using System.Collections.Generic;
using System.IO;
using Windows.Storage;
using Windows.System;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using C1.Xaml.PdfViewer;

namespace ControlExplorer.Controls.PdfViewer
{
    public partial class DemoPDFViewer
        : UserControl, IExposeApplicationBarItems
    {
        AppBarButton btnZoomIn = new AppBarButton { Icon = new SymbolIcon(Symbol.ZoomIn) };
        AppBarButton btnZoomOut = new AppBarButton { Icon = new SymbolIcon(Symbol.ZoomOut) };

        AppBarButton btnNextPage = new AppBarButton { Icon = new FontIcon() { Glyph = ((char)(0xe1fd)).ToString() } };
        AppBarButton btnPrevPage = new AppBarButton { Icon = new FontIcon() { Glyph = ((char)(0xe110)).ToString() } };

        AppBarButton btnShowFindText = new AppBarButton { Label = "find text" };

        public DemoPDFViewer()
        {
            InitializeComponent();

            btnNextPage.Click += btnNextPage_Click;
            btnNextPage.Label = "next";

            btnPrevPage.Click += btnPrevPage_Click;
            btnPrevPage.Label = "previous";

            btnZoomIn.Click += btnZoomIn_Click;
            btnZoomIn.Label = "zoom in";

            btnZoomOut.Click += btnZoomOut_Click;
            btnZoomOut.Label = "zoom out";

            btnShowFindText.Click += btnShowFindText_Click;

            btnClose_Click(null, new RoutedEventArgs());

            Loaded += DemoPDFViewer_Loaded;

            pdfViewer.RequestNavigate += async (s, e) =>
            {
                var dialog = new MessageDialog("The document is requesting to navigate to " + e.Uri, "Navigate");
                await dialog.ShowAsync();
                // navigate to urls loading web browser control
                await Launcher.LaunchUriAsync(e.Uri);
            };
        }


        private async void DemoPDFViewer_Loaded(object sender, RoutedEventArgs e)
        {
            var resource = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Resources/Pdfs/Presentation.pdf"));
            pdfViewer.LoadDocument(await resource.OpenStreamForReadAsync());
            txtLoading.Visibility = Visibility.Collapsed;         
        }

        void btnZoomOut_Click(object sender, RoutedEventArgs e)
        {
            pdfViewer.DecreaseZoomCommand.Execute(null);
        }

        void btnZoomIn_Click(object sender, RoutedEventArgs e)
        {
            pdfViewer.IncreaseZoomCommand.Execute(null);
        }

        void btnPrevPage_Click(object sender, RoutedEventArgs e)
        {
            pdfViewer.PreviousPageCommand.Execute(null);
        }

        void btnNextPage_Click(object sender, RoutedEventArgs e)
        {
            pdfViewer.NextPageCommand.Execute(null);
        }

        private async void listFiles_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var fileName = (string)listFiles.SelectedItem;
            if (fileName != null)
            {
                var folder = ApplicationData.Current.TemporaryFolder;
                var file = await folder.GetFileAsync(fileName);

                using (var stream = await file.OpenStreamForReadAsync())
                {
                    this.pdfViewer.LoadDocument(stream);
                    this.pdfViewer.ViewMode = C1.Xaml.PdfViewer.ViewMode.Fitted;
                }

                gridViewer.Visibility = Visibility.Visible;
                gridPickFiles.Visibility = Visibility.Collapsed;
            }
        }

        private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            btnDoSearch.IsEnabled = (txtSearch.Text.Trim().Length > 0);
        }

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

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            panelFindText.Visibility = Visibility.Collapsed;
            btnShowFindText.IsEnabled = true;
        }

        void btnShowFindText_Click(object sender, RoutedEventArgs e)
        {
            panelFindText.Visibility = Visibility.Visible;
            btnShowFindText.IsEnabled = false;
        }

        #region IExposeApplicationBarItems Members

        public IEnumerable<ICommandBarElement> ApplicationBarItems
        {
            get
            {
                yield return btnZoomOut;
                yield return btnZoomIn;

                yield return btnPrevPage;
                yield return btnNextPage;

                yield return btnShowFindText;
            }
        }

        #endregion

    }
}
