using System.Windows;
using System.Windows.Media;
using InABox.Core;
using Syncfusion.Windows.Shared;

namespace InABox.DynamicGrid
{
    public class DoubleEditorControl : DynamicEditorControl<double, DoubleEditor>
    {
        static DoubleEditorControl()
        {
            //DynamicEditorControlFactory.Register<DoubleEditorControl, DoubleEditor>();
        }
        
        private DoubleTextBox Editor;

        public DoubleEditorControl()
        {
            Width = 150;
        }

        protected override FrameworkElement CreateEditor()
        {
            var def = EditorDefinition as DoubleEditor;
            var digits = def != null ? def.Digits : 2;

            Editor = new DoubleTextBox
            {
                VerticalAlignment = VerticalAlignment.Stretch,
                VerticalContentAlignment = VerticalAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                HorizontalContentAlignment = HorizontalAlignment.Center,
                NumberDecimalDigits = digits
            };
            Editor.ValueChanged += (o, e) => CheckChanged();
            return Editor;
        }

        public override void Configure()
        {
            Editor.NumberDecimalDigits = EditorDefinition.Digits;
        }

        public override int DesiredHeight()
        {
            return 25;
        }

        public override int DesiredWidth()
        {
            return 150;
        }

        protected override double RetrieveValue()
        {
            return Editor.Value.HasValue ? Editor.Value.Value : default;
        }

        protected override void UpdateValue(double value)
        {
            Editor.Value = value;
        }

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

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