using System.Globalization;
using Avalonia;
using Avalonia.Data.Converters;
using Avalonia.Media;
using Avalonia.Styling;
using ClaudeDo.Ui.ViewModels.Islands;

namespace ClaudeDo.Ui.Converters;

public sealed class LogKindForegroundConverter : IValueConverter
{
    private static IBrush? Resolve(string key)
    {
        if (Application.Current is { } app &&
            app.Resources.TryGetResource(key, app.ActualThemeVariant, out var res) &&
            res is IBrush brush)
        {
            return brush;
        }
        return null;
    }

    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        var key = value is LogKind kind ? kind switch
        {
            LogKind.Sys    => "TextMuteBrush",
            LogKind.Tool   => "SageBrush",
            LogKind.Claude => "TextBrush",
            LogKind.Stdout => "TextDimBrush",
            LogKind.Stderr => "BloodBrush",
            LogKind.Done   => "MossBrightBrush",
            LogKind.Msg    => "TextDimBrush",
            LogKind.User   => "AccentBrush",
            _              => "TextDimBrush",
        } : "TextDimBrush";

        return Resolve(key) ?? AvaloniaProperty.UnsetValue;
    }

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