using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using C1.Silverlight;
using System.Reflection;

namespace PdfViewerSamples
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            var resource = Application.GetResourceStream(new Uri("/" + new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + ";component/C1XAPOptimizer.pdf", UriKind.Relative));
            pdfViewer.LoadDocument(resource.Stream);

            pdfViewer.RequestNavigate += (s, e) =>
            {
                if (HtmlPage.IsEnabled)
                {
                    try
                    {
                        // HtmlPage.PopupWindow doesn't work for email addresses.
                        // HtmlPage.Window.Navigate seems to work both for webpages and emails.

                        HtmlPage.Window.Navigate(e.Uri, "_blank");
                    }
                    catch { }
                }
            };
        }

        void LoadPdf(object sender, RoutedEventArgs args)
        {
            var dialog = new OpenFileDialog
            {
                Filter = "PDF files|*.pdf"
            };

            if (dialog.ShowDialog() == true)
            {
                using (var fileStream = dialog.File.OpenRead())
                {
                    try
                    {
                        pdfViewer.LoadDocument(fileStream);
                    }
                    catch (Exception ex)
                    {
                        C1MessageBox.Show("Could not load PDF file:\r\n" + ex.Message);
                    }
                }
            }
        }
    }
}
