using System.Collections;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace DragonISO.App.Converters;

/// <summary>
/// Maps a collection (or its count) to <see cref="Visibility"/>. Pass
/// <c>"empty"</c> as the converter parameter to invert the sense (Visible when
/// count == 0). Used to swap an empty-state placeholder in for the participants
/// DataGrid when no Teams sources are visible yet.
/// </summary>
public sealed class CountToVisibilityConverter : IValueConverter
{
    public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        var count = value switch
        {
            int n => n,
            ICollection c => c.Count,
            null => 0,
            _ => 1, // anything else: treat as non-empty
        };
        var invert = string.Equals(parameter as string, "empty", StringComparison.OrdinalIgnoreCase);
        var visible = invert ? count == 0 : count > 0;
        return visible ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
        throw new NotSupportedException();
}
