using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Xml;

namespace Valg.EVA.Skanning.UserInterfaceElements
{
    public static class TextBlockHelper
    {
        public static string GetFormattedText(DependencyObject obj)
        {
            return (string)obj.GetValue(FormattedTextProperty);
        }

        public static void SetFormattedText(DependencyObject obj, string value)
        {
            obj.SetValue(FormattedTextProperty, value);
        }

        public static readonly DependencyProperty FormattedTextProperty =
            DependencyProperty.RegisterAttached("FormattedText",
                typeof(string),
                typeof(TextBlockHelper),
                new UIPropertyMetadata("", FormattedTextChanged));

        private static void FormattedTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            string value = e.NewValue as string;

            TextBlock textBlock = sender as TextBlock;

            if (textBlock != null)
            {
                textBlock.Inlines.Clear();
                textBlock.Inlines.Add(Process(value));
            }
        }

        static Inline Process(string value)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(string.Format("<Inline>{0}</Inline>",value));

            Span span = new Span();
            InternalProcess(span, doc.ChildNodes[0]);

            return span;
        }

        private static void InternalProcess(Span span, XmlNode xmlNode)
        {
            foreach (XmlNode child in xmlNode)
            {
                if (child is XmlText)
                {
                    span.Inlines.Add(new Run(child.InnerText));
                }
                else if (child is XmlElement)
                {
                    switch (child.Name.ToUpper())
                    {
                        case "B":
                        case "BOLD":
                        {
                            Span boldSpan = new Span();
                            InternalProcess(boldSpan, child);
                            Bold bold = new Bold(boldSpan);
                            span.Inlines.Add(bold);
                            break;
                        }
                        case "I":
                        case "ITALIC":
                        {
                            Span italicSpan = new Span();
                            InternalProcess(italicSpan, child);
                            Italic bold = new Italic(italicSpan);
                            span.Inlines.Add(bold);
                            break;
                        }
                        case "U":
                        case "UNDERLINE":
                        {
                            Span underlineSpan = new Span();
                            InternalProcess(underlineSpan, child);
                            Underline bold = new Underline(underlineSpan);
                            span.Inlines.Add(bold);
                            break;
                        }
                    }
                }
            }
        }
    }
}

