@inherits DataFilterBaseComponent<DateTimePickerControl>
@using System.Windows.Forms

<div class="datetimepicker" name="@Control.Name" style="@GetDateTimePickerDivStyle">
    @switch (Control.Format)
    {
        case DateTimePickerFormat.Short:
            <input style="@GetDateTimePickerStyle"
                   type="date"
                   @bind="DateValue"
                   disabled="@IsDisabled"
                   required />
            break;
        case DateTimePickerFormat.Time:
            <input style="@GetDateTimePickerStyle min-width:5rem;"
                   type="time"
                   @bind="TimeValue"
                   step="1"
                   disabled="@IsDisabled"
                   required />
            break;
        default:
            <input style="@GetDateTimePickerStyle border-right-width: 0px; padding-right:0px"
                   type="date"
                   @bind="DateValue"
                   disabled="@IsDisabled"
                   required />
            <input style="@GetDateTimePickerStyle border-left-width: 0px; padding-left:0px; min-width:5rem;"
                   type="time"
                   @bind="TimeValue"
                   step="1"
                   disabled="@IsDisabled"
                   required />
            break;
    }
</div>

@code {

    private DateTime DateValue
    {
        get
        {
            return Control.Value;
        }
        set
        {
            DateTime oldValue = Control.Value;
            DateTime newDateTime = new DateTime(
                value.Year,
                value.Month,
                value.Day,
                oldValue.Hour,
                oldValue.Minute,
                oldValue.Second,
                oldValue.Kind);
            Control.Value = newDateTime;

            DateTimePickerChange();
        }
    }

    private DateTime TimeValue
    {
        get
        {
            return Control.Value;
        }
        set
        {
            DateTime oldValue = Control.Value;
            DateTime newDateTime = new DateTime(
                oldValue.Year,
                oldValue.Month,
                oldValue.Day,
                value.Hour,
                value.Minute,
                value.Second,
                oldValue.Kind);
            Control.Value = newDateTime;

            DateTimePickerChange();
        }
    }

    private void DateTimePickerChange()
    {
        Control.OnValueChanged(null);

        Refresh();
    }

    private string GetDateTimePickerDivStyle
        => $"{GetControlPosition()} display:inline-flex";

    private string GetDateTimePickerStyle
        => $"{GetControlFont()} {GetBackColor()}";
}
