using System;
using System.Collections.Generic;
using System.Linq;
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;
using C1.Silverlight.RichTextBox;
using C1.Silverlight.SpellChecker;
using System.IO;
using C1.Silverlight.RichTextBox.Documents;
using System.Diagnostics;
using System.Reflection;

namespace RichTextBoxSamples
{
    public partial class DemoRichTextBox : UserControl
    {
        public event EventHandler<RequestNavigateEventArgs> RequestNavigate;

        public DemoRichTextBox()
        {
            InitializeComponent();
            
            var spellChecker = new C1SpellChecker();
            richTB.SpellChecker = spellChecker;
            spellChecker.MainDictionary.LoadAsync("C1Spell_en-US.dct");
            
            var stream = Application.GetResourceStream(new Uri("/" + new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + ";component/Resources/dickens.htm", UriKind.Relative)).Stream;
            var html = new StreamReader(stream).ReadToEnd();
            richTB.HtmlFilter.ConvertingHtmlNode += OnConvertingHtmlNode;
            richTB.Html = html;
            richTB.NavigationMode = NavigationMode.Always;
            richTB.RequestNavigate += OnRequestNavigate;
            richTB.LayoutUpdated += new EventHandler(richTB_LayoutUpdated);
        }

        void OnConvertingHtmlNode(object sender, ConvertingHtmlNodeEventArgs e)
        {
            var element = e.HtmlNode as C1HtmlElement;
            if (element != null && element.Name == "img")
            {
                string src;
                if (element.Attributes.TryGetValue("src", out src) && src.StartsWith("images"))
                {
                    element.Attributes["src"] = "/" + new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + ";component/Resources/" + src;
                }
            }
        }

        bool firstLayout = true;
        void richTB_LayoutUpdated(object sender, EventArgs e)
        {
            if (firstLayout && richTB.PrintPageLayout != null)
            {
                firstLayout = false;

                var pageWidth = richTB.PrintPageLayout.Width + richTB.PrintPageLayout.Padding.Left + richTB.PrintPageLayout.Padding.Right;
                richTB.Zoom = Math.Max(Math.Min(richTB.ActualWidth / pageWidth, 1), 0.3);
            }
        }

        void OnRequestNavigate(object sender, RequestNavigateEventArgs e)
        {
            if (RequestNavigate != null)
            {
                RequestNavigate(sender, e);
            }

            var uri = e.Hyperlink.NavigateUri != null ? e.Hyperlink.NavigateUri.ToString() : "";
            if (uri.StartsWith("http://"))
            {
                System.Windows.Browser.HtmlPage.Window.Eval(string.Format("open('{0}')", uri));
            }
            else if (uri.StartsWith("#") && uri.Length > 1)
            {
                var id = uri.Substring(1);
                var element = richTB.Document.EnumerateSubtree().FirstOrDefault(el => el.Id == id);
                if (element != null)
                {
                    richTB.ScrollTo(element.ContentStart.ClosestCaret);
                }
            }
        }

        public bool IsReadOnly
        {
            get
            {
                return richTB.IsReadOnly;
            }
            set
            {
                richTB.IsReadOnly = value;
            }
        }

        public bool AcceptsReturn
        {
            get
            {
                return richTB.AcceptsReturn;
            }
            set
            {
                richTB.AcceptsReturn = value;
            }
        }

        public NavigationMode NavigationMode
        {
            get
            {
                return richTB.NavigationMode;
            }
            set
            {
                richTB.NavigationMode = value;
            }
        }

        public Brush SelectionBackground
        {
            get { return richTB.SelectionBackground; }
            set { richTB.SelectionBackground = value; }
        }

        public Brush SelectionForeground
        {
            get { return richTB.SelectionForeground; }
            set { richTB.SelectionForeground = value; }
        }

    }
}
