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 DFDateControl : DynamicFormFieldControl<DFLayoutDateField, DFLayoutDateFieldProperties, DateTime, DateTime?>
{
    private DateTimeEdit Date = null!; // late-initialising

    protected override FrameworkElement Create()
    {
        Date = new DateTimeEdit();
        Date.DateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat;
        Date.Pattern = DateTimePattern.ShortDate;
        var def = Field.Properties.Default;
        Date.DateTime = def.IsEmpty()
            ? DateTime.Now
            : def;
        Date.DateTimeChanged += (sender, e) => ChangeField();
        Date.EnableMouseWheelEdit = false;
        return Date;
    }

    public override DateTime? GetSerializedValue() => Date.DateTime ?? DateTime.MinValue;

    public override void SetSerializedValue(DateTime? value) => Date.DateTime = value;

    public override DateTime GetValue() => Date.DateTime ?? DateTime.MinValue;

    public override void SetValue(DateTime value) => Date.DateTime = value;

    protected override bool IsEmpty() => Date.DateTime == null || Date.DateTime == DateTime.MinValue;
}
