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

namespace SharepointToolbox.Views.Converters;

/// <summary>Converts IndentLevel (int) to WPF Thickness for DataGrid indent.</summary>
[ValueConversion(typeof(int), typeof(Thickness))]
public class IndentConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int level = value is int i ? i : 0;
        return new Thickness(level * 16, 0, 0, 0);
    }

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

/// <summary>Converts byte count (long) to human-readable size string.</summary>
[ValueConversion(typeof(long), typeof(string))]
public class BytesConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        long bytes = value is long l ? l : 0L;
        if (bytes >= 1_073_741_824L) return $"{bytes / 1_073_741_824.0:F2} GB";
        if (bytes >= 1_048_576L)     return $"{bytes / 1_048_576.0:F2} MB";
        if (bytes >= 1024L)          return $"{bytes / 1024.0:F2} KB";
        return $"{bytes} B";
    }

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

/// <summary>Inverts a bool binding — used to disable controls while an operation is running.</summary>
[ValueConversion(typeof(bool), typeof(bool))]
public class InverseBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        => value is bool b && !b;

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        => value is bool b && !b;
}

/// <summary>
/// Converts a string to Visibility: Visible when non-empty, Collapsed when null or empty.
/// </summary>
[ValueConversion(typeof(string), typeof(Visibility))]
public class StringToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        => value is string s && !string.IsNullOrEmpty(s) ? Visibility.Visible : Visibility.Collapsed;

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

/// <summary>
/// Converts an enum value to bool by comparing it with the ConverterParameter.
/// Used for RadioButton IsChecked bindings to enum properties.
/// </summary>
public class EnumBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null || parameter == null) return false;
        return value.ToString() == parameter.ToString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is bool b && b && parameter != null)
            return Enum.Parse(targetType, parameter.ToString()!);
        return System.Windows.DependencyProperty.UnsetValue;
    }
}

/// <summary>
/// Converts a List&lt;string&gt; to a single semicolon-separated string for display in DataGrid cells.
/// </summary>
public class ListToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is IEnumerable<string> list)
            return string.Join("; ", list);
        return string.Empty;
    }

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

/// <summary>
/// Converts a <see cref="SharepointToolbox.Core.Models.StorageNodeKind"/> enum
/// to a localized display string via the translation source.
/// </summary>
[ValueConversion(typeof(SharepointToolbox.Core.Models.StorageNodeKind), typeof(string))]
public class StorageKindConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is not SharepointToolbox.Core.Models.StorageNodeKind kind) return string.Empty;
        var T = SharepointToolbox.Localization.TranslationSource.Instance;
        return kind switch
        {
            SharepointToolbox.Core.Models.StorageNodeKind.Library          => T["stor.kind.library"],
            SharepointToolbox.Core.Models.StorageNodeKind.HiddenLibrary    => T["stor.kind.hidden"],
            SharepointToolbox.Core.Models.StorageNodeKind.PreservationHold => T["stor.kind.preservation"],
            SharepointToolbox.Core.Models.StorageNodeKind.ListAttachments  => T["stor.kind.attachments"],
            SharepointToolbox.Core.Models.StorageNodeKind.RecycleBin       => T["stor.kind.recyclebin"],
            SharepointToolbox.Core.Models.StorageNodeKind.Subsite          => T["stor.kind.subsite"],
            _ => kind.ToString()
        };
    }

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

