using C1.Util;
using C1.Xaml.RichTextBox;
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 Windows.UI.Xaml.Input;

namespace ControlExplorer.Controls.RichTextBox
{
    public partial class DemoRichTextBox
        : UserControl, IExposeApplicationBarItems
    {
        C1BoldTool btnBold = new C1BoldTool() { Label = "bold", Icon = new SymbolIcon(Symbol.Bold) };
        C1ItalicTool btnItalic = new C1ItalicTool() { Label = "italic", Icon= new SymbolIcon(Symbol.Italic) };
        C1UnderlineTool btnUnderline = new C1UnderlineTool() { Label = "underline", Icon= new SymbolIcon(Symbol.Underline) };
        AppBarButton btnHyperlink = new AppBarButton { Label = "hyperlink", Icon = new SymbolIcon(Symbol.Link) };
        HyperlinkPopup linkPopup;

        public DemoRichTextBox()
        {
            InitializeComponent();
            btnHyperlink.Click += btnHyperlink_Click;
            linkPopup = new HyperlinkPopup(rtb);
            Loaded += DemoRichTextBox_Loaded;
        }

        private async void DemoRichTextBox_Loaded(object sender, RoutedEventArgs e)
        {
            var resource = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Resources/RichTextBox/simple.htm"));
            var stream = await resource.OpenStreamForReadAsync();
            var html = new StreamReader(stream).ReadToEnd();
            rtb.Html = html;
            rtb.NavigationMode = NavigationMode.Always;
        }

        #region IExposeApplicationBarItems Members

        public IEnumerable<ICommandBarElement> ApplicationBarItems
        {
            get
            {
                btnBold.RichTextBox = rtb;
                yield return btnBold;

                btnItalic.RichTextBox = rtb;
                yield return btnItalic;

                btnUnderline.RichTextBox = rtb;
                yield return btnUnderline;

                yield return btnHyperlink;
            }
        }

        #endregion

        async void btnHyperlink_Click(object sender, RoutedEventArgs e)
        {
            // close keyboard from a UserControl
            var parentPage = Util.GetParentOfType<Page>(this);
            if (parentPage != null)
            {
                parentPage.Focus(FocusState.Programmatic);
            }
            linkPopup.Closed += linkPopup_Closed;
            // show popup
            linkPopup.ShowPopup();
            UpdateAppBarVisibility(Visibility.Collapsed);
        }

        void linkPopup_Closed(object sender, object e)
        {
            UpdateAppBarVisibility(Visibility.Visible);
        }

        private async void rtb_RequestNavigate(object sender, RequestNavigateEventArgs e)
        {
            bool? result = true;
            var dialog = new MessageDialog("The document is requesting to navigate to " + e.Hyperlink.NavigateUri, "Navigate");
            dialog.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler((cmd) => result = true)));
            dialog.Commands.Add(new UICommand("Cancel", new UICommandInvokedHandler((cmd) => result = false)));

            var command = await dialog.ShowAsync();

            if (result == true)
            {
                await Launcher.LaunchUriAsync(e.Hyperlink.NavigateUri);
            }
            else
            {
                // select hyperlink for editing
                rtb.Select(e.Hyperlink.ContentStart.TextOffset, e.Hyperlink.ContentRange.Text.Length);
            }


        }

        void UpdateAppBarVisibility(Visibility visibility)
        {
            btnBold.Visibility = btnItalic.Visibility = btnUnderline.Visibility = btnHyperlink.Visibility = visibility;
        }
    }
}

