using System;
using System.Globalization;
using Avalonia.Data;
using Avalonia.Data.Converters;

namespace detect.gui.Converters;

public class IndexToBoolConverter : IValueConverter
{
    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        // 传入的参数是Index
        if (value is int selectedIndex && parameter is string indexString && int.TryParse(indexString, out int index))
        {
            return selectedIndex == index;
        }
        return false;
    }

    public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        // 仅当选中某个 RadioButton 时更新下标
        if (value is bool isChecked && isChecked && parameter is string indexString && int.TryParse(indexString, out int index))
        {
            return index;
        }
        return BindingOperations.DoNothing;
    }
}