using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace InABox.DynamicGrid
{
    public class MemoEditorControl : DynamicEditorControl<string>
    {
        private TextBox Editor;

        private bool IsChanged;

        protected override FrameworkElement CreateEditor()
        {
            MinHeight = 50;
            Editor = new TextBox
            {
                VerticalAlignment = VerticalAlignment.Stretch,
                VerticalContentAlignment = VerticalAlignment.Top,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
                TextWrapping = TextWrapping.Wrap,
                AcceptsReturn = true
            };
            Editor.TextChanged += (o, e) =>
            {
                IsChanged = true;
                //CheckChanged();
            };
            Editor.LostFocus += (o, e) =>
            {
                if (IsChanged)
                    CheckChanged();
            };
            return Editor;
        }

        public override int DesiredHeight()
        {
            return int.MaxValue;
        }

        public override int DesiredWidth()
        {
            return int.MaxValue;
        }

        protected override string RetrieveValue()
        {
            return Editor.Text;
        }

        protected override void UpdateValue(string value)
        {
            if (value != Editor.Text)
                Editor.Text = value;
        }

        public override void SetFocus()
        {
            Editor.Focus();
            Editor.CaretIndex = string.IsNullOrEmpty(Value) ? 0 : Value.Length;
        }

        public override void SetColor(Color color)
        {
            Editor.Background = new SolidColorBrush(color);
        }
    }
}