using C1.Xaml;
using C1.Xaml.RichTextBox;
using C1.Xaml.RichTextBox.Documents;
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;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace RichTextBoxSamples
{
    public sealed partial class DemoRtfFilter : UserControl
    {
        public DemoRtfFilter()
        {
            this.InitializeComponent();

            richTextBox.Html = html;
        }

        private void C1TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var oldItem = e.RemovedItems.OfType<C1TabItem>().FirstOrDefault();
                if (oldItem == null) return; // items are null the first time around because InitializeComponent is running

                if (htmlTab == null)
                    htmlTab = this.FindName("htmlTab") as C1TabItem;
                if (rtfTab == null)
                    rtfTab = this.FindName("rtfTab") as C1TabItem;
                if (htmlBox == null)
                    htmlBox = this.FindName("htmlBox") as C1RichTextBox;
                if (rtfBox == null)
                    rtfBox = this.FindName("rtfBox") as C1RichTextBox;

                if (oldItem == richTextBoxTab)
                {
                    htmlBox.Text = richTextBox.Html;
                    rtfBox.Text = new RtfFilter().ConvertFromDocument(richTextBox.Document);
                }
                else if (oldItem == htmlTab)
                {
                    richTextBox.Html = htmlBox.Text;
                    rtfBox.Text = new RtfFilter().ConvertFromDocument(richTextBox.Document);
                }
                else if (oldItem == rtfTab)
                {
                    richTextBox.Document = new RtfFilter().ConvertToDocument(rtfBox.Text);
                    htmlBox.Text = richTextBox.Html;
                }
            }
            catch { }
        }

        string html =
            @"<html>
                <head>
                    <style>
                        td
                        {
                            border: solid 1px Black;
                            padding: 2px;
                        }
                    </style>
                </head>
                <body style=""font-family: Verdana; font-size: medium;"">
                <p>
                <b>C1RichTextBox</b> supports importing and exporting to <span style='text-decoration: underline'>HTML</span> and <span style='text-decoration: underline'>RTF</span>.
                </p>
                <p style='text-align:center; background-color:#FFFFCC; font-size:9pt'>
                    Switch the tabs to see the underying HTML and RTF formats of this document.
                </p>
                </body>
            </html>";
    }
}
