using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using C1.Silverlight.RichTextBox;
using C1.Silverlight.RichTextBox.Documents;
using System.Windows.Media.Imaging;
using C1.Silverlight.Imaging;
using System.Reflection;

namespace HtmlFilterCustomization
{
    public partial class MainPage : UserControl
    {
        string _baseUri = "/" + new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + ";component/";
        public MainPage()
        {
            InitializeComponent();

            RichTextBox.HtmlFilter.ConvertingHtmlNode += HtmlFilter_ConvertingHtmlNode;
            RichTextBox.HtmlFilter.ConvertingTextElement += HtmlFilter_ConvertingTextElement;

            RichTextBox.Html =
                @"<p style='text-align:center; background-color:#FFFFCC'>This sample demonstrates how to customize C1RichTextBox's HtmlFilter to load GIF images using C1.Silverlight.Imaging.</p>
                  <p>This is a GIF image loaded from the application resources: <img src='ball.gif' /></p>";
            Source.Text = RichTextBox.Html;
        }

        void HtmlFilter_ConvertingTextElement(object sender, ConvertingTextElementEventArgs e)
        {
            var inlineContainer = e.TextElement as C1InlineUIContainer;
            if (inlineContainer != null)
            {
                var src = GetImageSource(inlineContainer.Content as DependencyObject);
                if (src != null)
                {
                    var element = new C1HtmlElement("img");
                    element.Attributes["src"] = src;
                    e.Parent.Add(element);
                    e.Handled = true;
                }
            }
        }

        void HtmlFilter_ConvertingHtmlNode(object sender, ConvertingHtmlNodeEventArgs e)
        {
            var htmlElement = e.HtmlNode as C1HtmlElement;
            if (htmlElement != null && htmlElement.Name == "img")
            {
                string src;
                if (htmlElement.Attributes.TryGetValue("src", out src))
                {
                    var uri = new Uri(_baseUri + src, UriKind.Relative);
                    var resource = Application.GetResourceStream(uri);
                    if(resource != null)
                    {
                        var imageSource = new C1Bitmap(resource.Stream).ImageSource;
                        var image = new Image { Source = imageSource };
                        SetImageSource(image, src);
                        e.Parent.Children.Add(new C1InlineUIContainer { Child = image });
                        e.Handled = true;
                    }
                }
            }
        }

        private void ToHtml(object sender, RoutedEventArgs e)
        {
            Source.Text = RichTextBox.Html;
        }

        private void FromHtml(object sender, RoutedEventArgs e)
        {
            RichTextBox.Html = Source.Text;
        }

        public static string GetImageSource(DependencyObject obj)
        {
            return (string)obj.GetValue(ImageSourceProperty);
        }

        public static void SetImageSource(DependencyObject obj, string value)
        {
            obj.SetValue(ImageSourceProperty, value);
        }

        public static readonly DependencyProperty ImageSourceProperty =
            DependencyProperty.RegisterAttached("ImageSource", typeof(string), typeof(MainPage), new PropertyMetadata(null));
    }
}
