using System;
using System.Net;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using C1.Silverlight.DataGrid;
using C1.Silverlight.RichTextBox;
using System.Reflection;

namespace C1DataGrid_Demo2010
{
    public class DataGridRichTextColumn : DataGridBoundColumn
    {
        public DataGridRichTextColumn()
        {
            Initialize();
        }

        public DataGridRichTextColumn(PropertyInfo property)
            : base(property)
        {
            Initialize();
        }

        protected void Initialize()
        {
            IsReadOnly = false;
        }

        public override object GetCellContentRecyclingKey(DataGridRow row)
        {
            return "RichTextBox";
        }

        public override FrameworkElement CreateCellContent(DataGridRow row)
        {
            var richTextBox = new C1RichTextBox();
            richTextBox.IsReadOnly = true;
            richTextBox.IsHitTestVisible = false;
            richTextBox.BorderThickness = new Thickness(0);
            richTextBox.Margin = new Thickness(1);
            richTextBox.Background = new SolidColorBrush(Colors.Transparent);
            richTextBox.HorizontalScrollBarVisibility = System.Windows.Controls.ScrollBarVisibility.Disabled;
            richTextBox.VerticalScrollBarVisibility = System.Windows.Controls.ScrollBarVisibility.Disabled;
            return richTextBox;
        }

        public override void BindCellContent(FrameworkElement cellContent, DataGridRow row)
        {
            var richTextBox = (C1RichTextBox)cellContent;
            var binding  = CopyBinding(Binding);
            richTextBox.DataContext = row.DataItem;
            richTextBox.SetBinding(C1RichTextBox.HtmlProperty, binding);
        }

        public override void UnbindCellContent(FrameworkElement cellContent, DataGridRow row)
        {
            var richTextBox = (C1RichTextBox)cellContent;
            richTextBox.DataContext = null;
        }

        public override FrameworkElement GetCellEditingContent(DataGridRow row)
        {
            var richTextBox = new C1RichTextBox();
            var binding = CopyBinding(Binding);
            richTextBox.SetBinding(C1RichTextBox.HtmlProperty, binding);
            return richTextBox;
        }

        public override void EndEdit(FrameworkElement editingElement)
        {
            base.EndEdit(editingElement);

            var richTextBox = (C1RichTextBox) editingElement;
            var tmp = richTextBox.Html;  // force execution of calculated dependency property
        }
    }
}
