using System;
using System.Windows.Controls;
using Xceed.Wpf.Toolkit;

namespace InABox.DynamicGrid;

public class DateTimeValueNode : ValueNode {

    private DateTimePicker Editor;

    public override object? Value
    {
        get => Editor.Value;
        set
        {
            if (value is DateTime dateTime)
                Editor.Value = dateTime;
            else if (value is string str && DateTime.TryParse(str, out var date))
                Editor.Value = date;
            else
                Editor.Value = DateTime.Today;
        }
    }
    
    public override bool IsEnabled
    {
        get => Editor.IsEnabled;
        set
        {
            Editor.IsEnabled = value;
            Editor.IsReadOnly = !value;
        }
    }

    public DateTimeValueNode()
    {
        Editor = new DateTimePicker
        {
            Format = DateTimeFormat.Custom,
            FormatString = "dd MMM yyyy HH:mm",
            VerticalAlignment = System.Windows.VerticalAlignment.Stretch,
            VerticalContentAlignment = System.Windows.VerticalAlignment.Center,
            HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center,
            CalendarDisplayMode = CalendarMode.Month,
            ShowButtonSpinner = false
        };
        Content = Editor;
    }
}