using System;
using System.Globalization;
using Avalonia;
using Avalonia.Data.Converters;
using Avalonia.Media;
using ClaudeDo.Data.Models;

namespace ClaudeDo.Ui.Converters;

public sealed class WorkerLogLevelToBrushConverter : IValueConverter
{
    private static readonly IBrush SuccessBrush = new SolidColorBrush(Color.Parse("#4CAF50"));
    private static readonly IBrush WarnBrush    = new SolidColorBrush(Color.Parse("#FFA726"));
    private static readonly IBrush ErrorBrush   = new SolidColorBrush(Color.Parse("#EF5350"));
    private static readonly IBrush InfoFallback = new SolidColorBrush(Color.Parse("#888888"));

    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        if (value is not WorkerLogLevel level)
            return AvaloniaProperty.UnsetValue;

        return level switch
        {
            WorkerLogLevel.Success => SuccessBrush,
            WorkerLogLevel.Warn    => WarnBrush,
            WorkerLogLevel.Error   => ErrorBrush,
            WorkerLogLevel.Info    => ResolveInfoBrush(),
            _                      => AvaloniaProperty.UnsetValue,
        };
    }

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

    private static IBrush ResolveInfoBrush()
    {
        if (Application.Current is { } app &&
            app.Resources.TryGetResource("TextDimBrush", app.ActualThemeVariant, out var res) &&
            res is IBrush brush)
        {
            return brush;
        }
        return InfoFallback;
    }
}
