using C1.Xaml;
using C1.Xaml.PdfViewer;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Windows.Foundation;
using Windows.Foundation.Collections;
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 User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace PdfViewerSamples
{
    public sealed partial class BookmarksDemo : UserControl
    {
        public BookmarksDemo()
        {
            this.InitializeComponent();
            this.Loaded += BookmarksDemo_Loaded;
            this.Unloaded += BookmarksDemo_Unloaded;
        }

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

        void BookmarksDemo_Loaded(object sender, RoutedEventArgs e)
        {
            Assembly asm = typeof(PdfViewerDemoPage).GetTypeInfo().Assembly;
            Stream stream = asm.GetManifestResourceStream("PdfViewerSamples.Files.TableOfContents.pdf");
            pdfViewer.LoadDocument(stream);
            List<Bookmark> list = new List<Bookmark>();
            foreach (Bookmark mak in pdfViewer.Bookmarks)
            {
                list.Add(mak);
            }
            treeView.DataContext = list;
        }

        private void C1TreeView_ItemClick(object sender, SourcedEventArgs e)
        {
            if (e.Source == null) return;
            var treeViewItem = e.Source as C1TreeViewItem;
            if (treeViewItem == null) return;
            var bookmark = treeViewItem.Header as Bookmark;
            if (bookmark == null) return;
            pdfViewer.GoToBookmark(bookmark.Title);
        }
    }
}
