using System;
using System.Windows.Controls;

namespace InABox.DynamicGrid;

public class EnumValueNode<TEnum> : ValueNode where TEnum : struct, Enum {

    private ComboBox ComboBox;

    public override object? Value {
        get => ComboBox.SelectedItem;
        set {
            if (value is TEnum val)
                ComboBox.SelectedItem = val;
            else if (value is string str && Enum.TryParse<TEnum>(str, out val))
                ComboBox.SelectedItem = val;
            else
                ComboBox.SelectedIndex = 0;
        }
    }
    
    public override bool IsEnabled
    {
        get => ComboBox.IsEnabled;
        set
        {
            ComboBox.IsEnabled = value;
            ComboBox.IsReadOnly = !value;
        }
    }

    public EnumValueNode()
    {
        ComboBox = new ComboBox
        {
            VerticalAlignment = System.Windows.VerticalAlignment.Stretch,
            VerticalContentAlignment = System.Windows.VerticalAlignment.Center
        };

        foreach(var value in Enum.GetValues<TEnum>())
        {
            ComboBox.Items.Add(value);
        }

        Content = ComboBox;
    }

}