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

namespace InABox.DynamicGrid;

public class OperatorNode : ComboBox
{
    public delegate void OperatorChangedHandler(Operator op);

    public event OperatorChangedHandler? OperatorChanged;

    public Operator SelectedOperator => (Operator)SelectedItem;

    public OperatorNode(Operator op)
    {
        SetResourceReference(StyleProperty, typeof(ComboBox));

        VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
        VerticalContentAlignment = System.Windows.VerticalAlignment.Center;

        foreach (var value in Enum.GetValues<Operator>())
        {
            Items.Add(value);
        }
        SelectedItem = op;
        SelectionChanged += ComboBox_SelectionChanged;
    }
    public void Clear()
    {
        SelectedIndex = 0;
    }

    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var selected = (Operator?)SelectedItem ?? Operator.IsEqualTo;
        OperatorChanged?.Invoke(selected);
    }
}