using System.Windows.Controls;
using InABox.Core;
using Syncfusion.Windows.Shared;

namespace InABox.DynamicGrid;

public class DoubleValueNode : ValueNode
{

    private DoubleTextBox TextBox;

    public override object? Value {
        get => TextBox.Value; 
        set {
            if (value is double d)
                TextBox.Value = d;
            else if (value?.GetType().IsOrdinal() == true)
                TextBox.Value = (double)value;
            else if (value is string str && double.TryParse(str, out var num))
                TextBox.Value = num;
            else
                TextBox.Value = 0.0;
        }
    }
    
    public override bool IsEnabled
    {
        get => TextBox.IsEnabled;
        set
        {
            TextBox.IsEnabled = value;
            TextBox.IsReadOnly = !value;
        }
    }

    public DoubleValueNode()
    {
        TextBox = new DoubleTextBox
        {
            VerticalAlignment = System.Windows.VerticalAlignment.Stretch,
            VerticalContentAlignment = System.Windows.VerticalAlignment.Center,
            HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch,
            HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center,
            
        };
        Content = TextBox;
    }

}