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

namespace InABox.DynamicGrid
{
    public class DFTimeControl : DynamicFormFieldControl<DFLayoutTimeField, DFLayoutTimeFieldProperties, TimeSpan, TimeSpan?>
    {
        private DateTimeEdit Time = null!; // late-initialising

        protected override FrameworkElement Create()
        {
            Time = new DateTimeEdit();
            Time.DateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat;
            Time.Pattern = DateTimePattern.ShortTime;
            Time.DateTime = DateTime.MinValue.Add(Field.Properties.Default);
            Time.DateTimeChanged += (sender, e) => ChangeField();
            Time.EnableMouseWheelEdit = false;
            return Time;
        }

        public override TimeSpan? GetSerializedValue()
        {
            return Time.DateTime?.TimeOfDay;
        }

        public override void SetSerializedValue(TimeSpan? value)
        {
            Time.DateTime = value != null ? DateTime.MinValue.Add(value.Value) : null;
        }

        public override TimeSpan GetValue() => Time.DateTime?.TimeOfDay ?? TimeSpan.Zero;

        public override void SetValue(TimeSpan value) => Time.DateTime = DateTime.MinValue.Add(value);

        protected override bool IsEmpty() => Time.DateTime == null;
    }
}