using InABox.Core;
using Syncfusion.Windows.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace InABox.DynamicGrid
{
    public class DFIntegerControl : DynamicFormFieldControl<DFLayoutIntegerField, DFLayoutIntegerFieldProperties, int, int?>
    {
        private IntegerTextBox Integer = null!; // late-initialising

        protected override FrameworkElement Create()
        {
            Integer = new IntegerTextBox();
            Integer.Value = Field.Properties.Default;
            Integer.HorizontalContentAlignment = HorizontalAlignment.Center;
            Integer.VerticalContentAlignment = VerticalAlignment.Center;
            Integer.IsScrollingOnCircle = false;
            Integer.VerticalAlignment = VerticalAlignment.Stretch;
            Integer.ValueChanged += (sender, e) => ChangeField();

            return Integer;
        }

        public override int? GetSerializedValue()
        {
            var value = Integer.Value;
            if (value != null)
                return Convert.ToInt32((long)value);
            return null;
        }

        public override void SetSerializedValue(int? value)
        {
            Integer.Value = value;
        }

        public override int GetValue()
        {
            var value = Integer.Value;
            if (value != null)
                return Convert.ToInt32((long)value);
            return 0;
        }

        public override void SetValue(int value)
        {
            Integer.Value = value;
        }
        protected override bool IsEmpty() => Integer.Value == null;
    }
}
