using C1.Xaml;
using C1.Xaml.RichTextBox;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
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;

namespace RichTextBoxSamples.Tools
{
    public sealed partial class FontColorTool : UserControl
    {
        public Flyout Popup
        {
            get;
            set;
        }

        public C1RichTextBox RichTextBox
        {
            get;
            set;
        }

        public FontColorMode ColorMode
        {
            get;
            set;
        }

        public FontColorTool()
        {
            this.InitializeComponent();

            // init flyout
            Popup = new Flyout();
            Popup.Content = this;
            Popup.Placement = FlyoutPlacementMode.Bottom;

            ColorMode = FontColorMode.Foreground;
        }

        public void Show(FrameworkElement placementTarget)
        {
            Popup.ShowAt(placementTarget);
        }

        public void Close()
        {
            Popup.Hide();
        }

        private void colorListBox_ItemTapped(object sender, C1TappedEventArgs e)
        {
            C1ListBoxItem item = sender as C1ListBoxItem;
            Border b = item.Content as Border;
            if (ColorMode == FontColorMode.Background)
            {
                RichTextBox.Selection.InlineBackground = b.Background;
            }
            else
            {
                RichTextBox.Selection.Foreground = b.Background;
            }

            Close();
        }
    }

    public enum FontColorMode
    {
        Background,
        Foreground
    }
}
