using System.IO;
using System.Reflection;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using C1.Xaml.RichTextBox;
using System;
using Windows.UI.Popups;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace RichTextBoxSamples
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            btnBold.RichTextBox = rtb;
            btnItalic.RichTextBox = rtb;
            btnUnderline.RichTextBox = rtb;

            Assembly asm = typeof(MainPage).GetTypeInfo().Assembly;
            Stream stream = asm.GetManifestResourceStream("RichTextBoxSamples.Resources.simple.htm");
            var html = new StreamReader(stream).ReadToEnd();
            rtb.Html = html;
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void rtb_RequestNavigate(object sender, RequestNavigateEventArgs e)
        {
            var md = new MessageDialog("The document is requesting to navigate to " + e.Hyperlink.NavigateUri, "Navigate");

            md.Commands.Add(new UICommand("OK", (UICommandInvokedHandler) =>
            {
                Windows.System.Launcher.LaunchUriAsync(e.Hyperlink.NavigateUri);
            }));

            md.Commands.Add(new UICommand("Cancel", (UICommandInvokedHandler) =>
            {
                rtb.Select(e.Hyperlink.ContentStart.TextOffset, e.Hyperlink.ContentRange.Text.Length);
            }));

            md.ShowAsync();
        }

        private void btnCopy_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            rtb.ClipboardCopy();
        }

        private void btnCut_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            if (rtb.IsReadOnly)
                rtb.ClipboardCopy();
            else
                rtb.ClipboardCut();
        }

        private void btnPaste_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            if (!rtb.IsReadOnly)
            {
                rtb.ClipboardPaste();
            }
        }
    }
}
