using System;
using Syncfusion.Windows.Shared;

namespace InABox.DynamicGrid;

public class TimeSpanValueNode : ValueNode {

    private DateTimeEdit Editor;

    public override object? Value {
        get
        {
            var result = new TimeSpan(0);

            if (Editor.DateTime.HasValue)
                result = Editor.DateTime.Value.TimeOfDay;

            result = new TimeSpan(result.Hours, result.Minutes, 0);

            return result;
        }
        set
        {
            if (value is TimeSpan time)
                Editor.DateTime = DateTime.MinValue + time;
            else
                Editor.DateTime = DateTime.MinValue;
        }
    }
    
    public override bool IsEnabled
    {
        get => Editor.IsEnabled;
        set
        {
            Editor.IsEnabled = value;
            Editor.IsReadOnly = !value;
        }
    }

    public TimeSpanValueNode()
    {
        Editor = new DateTimeEdit
        {
            Pattern = DateTimePattern.CustomPattern,
            CustomPattern = "HH:mm",
            VerticalAlignment = System.Windows.VerticalAlignment.Stretch,
            VerticalContentAlignment = System.Windows.VerticalAlignment.Center,
            HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch,
            HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center,

            DropDownView = DropDownViews.Clock,
            IsButtonPopUpEnabled = false
        };

        Content = Editor;
    }

}