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;
using C1.Silverlight.RichTextBox.Documents;
using System.IO;
using System.Reflection;

namespace Annotations
{
    public partial class MainPage : UserControl
    {
        AnnotationService annotations;
        
        public MainPage()
        {
            InitializeComponent();

            richTextBox.Html = new StreamReader(Application.GetResourceStream(new Uri("/" + new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + ";component/w3c.htm", UriKind.Relative)).Stream).ReadToEnd();
            richTextBox.Document.Blocks.Insert(0, new C1Paragraph 
            {
                Children = 
                {
                    new C1Run 
                    {
                        Text = "Select some text and right click to highlight or add notes.",
                        FontWeight = FontWeights.Bold,
                        FontSize = 16,
                        Padding = new Thickness(2),
                    },
                },
                Margin = new Thickness(20),
            });

            annotations = new AnnotationService(richTextBox);
            
            annotations.AddHighlight(richTextBox.Document.Blocks.First().Children.First().ContentRange);
            var str = "Bobroskie";
            var start = richTextBox.Text.IndexOf(str);
            var note = annotations.AddTextNote(new C1TextRange(richTextBox.Document, start, str.Length));
            note.Text = "This is a note.\n\nYou can drag it and it will always keep its relative position to the annotated text.";

            richTextBox.SelectionChanged += (s, e) => UpdateMenu();
            UpdateMenu();
        }

        void UpdateMenu()
        {
            addNote.IsEnabled = addHighlight.IsEnabled = !richTextBox.Selection.IsEmpty;
        }

        private void AddHighlight(object sender, SourcedEventArgs e)
        {
            annotations.AddHighlight(richTextBox.Selection);
        }

        private void AddNote(object sender, SourcedEventArgs e)
        {
            annotations.AddTextNote(richTextBox.Selection);
        }

        private void RemoveHighlights(object sender, SourcedEventArgs e)
        {
            annotations.RemoveHighlights(richTextBox.Selection);
        }

        private void RemoveNotes(object sender, SourcedEventArgs e)
        {
            annotations.RemoveNotes(richTextBox.Selection);
        }

        private void AddHighlightButton(object sender, RoutedEventArgs e)
        {
            annotations.AddHighlight(richTextBox.Selection);
            richTextBox.Focus();
        }

        private void AddNoteButton(object sender, RoutedEventArgs e)
        {
            annotations.AddTextNote(richTextBox.Selection);
            richTextBox.Focus();
        }
    }
}
