using System.Windows;
using System.Windows.Media;
using Xceed.Wpf.Toolkit;

namespace InABox.DynamicGrid
{
    public class ColorEditorControl : DynamicEditorControl<string>
    {
        private ColorPicker Editor;

        protected override FrameworkElement CreateEditor()
        {
            Editor = new ColorPicker
            {
                HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalAlignment = VerticalAlignment.Stretch,
                VerticalContentAlignment = VerticalAlignment.Center
            };
            Editor.SelectedColorChanged += (o, e) => CheckChanged();
            return Editor;
        }

        public override int DesiredHeight()
        {
            return 25;
        }

        public override int DesiredWidth()
        {
            return 150;
        }

        protected override string RetrieveValue()
        {
            return Editor.SelectedColor.HasValue ? Editor.SelectedColor.Value.ToString() : "";
        }

        protected override void UpdateValue(string value)
        {
            if (!string.IsNullOrWhiteSpace(value))
                Editor.SelectedColor = (Color)ColorConverter.ConvertFromString(value);
            else
                Editor.SelectedColor = null;
        }

        public override void SetFocus()
        {
            Editor.Focus();
        }

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