@namespace Lumeo
@inject Lumeo.Services.Localization.ILumeoLocalizer L

@if (!_isRemoved)
{
    @if (IsDot && Pulse)
    {
        <span class="relative inline-flex h-2 w-2" @attributes="AdditionalAttributes">
            <span class="animate-ping absolute inline-flex h-full w-full rounded-full opacity-75 @PulseColorClass"></span>
            <span class="relative inline-flex rounded-full h-2 w-2 @DotClasses @Class"></span>
        </span>
    }
    else if (IsDot)
    {
        <div class="@CssClass" @attributes="AdditionalAttributes"></div>
    }
    else
    {
        <div class="@CssClass" @attributes="AdditionalAttributes">
            @if (IconContent != null)
            {
                <span class="mr-1 inline-flex items-center">@IconContent</span>
            }
            @ChildContent
            @if (IsRemovable)
            {
                <button type="button" class="ml-1 rounded-full hover:bg-foreground/20 focus:outline-none" @onclick="HandleRemove" aria-label="@L["Badge.Remove"]">
                    <Blazicon Svg="Lucide.X" class="h-3 w-3" />
                </button>
            }
        </div>
    }
}

@code {
    [Parameter] public RenderFragment? ChildContent { get; set; }

    /// <summary>Icon slot. Named <c>IconContent</c> to avoid shadowing the <c>&lt;Icon&gt;</c> component.</summary>
    [Parameter] public RenderFragment? IconContent { get; set; }

    [Parameter] public BadgeVariant Variant { get; set; } = BadgeVariant.Default;
    [Parameter] public bool IsDot { get; set; }
    [Parameter] public bool Pulse { get; set; }
    [Parameter] public bool IsRemovable { get; set; }
    [Parameter] public EventCallback OnRemove { get; set; }

    /// <summary>
    /// When true, the badge plays a one-shot pop-in entrance animation
    /// (scale + opacity) on mount. Default is <c>false</c>.
    /// </summary>
    [Parameter] public bool Animated { get; set; }

    [Parameter] public string? Class { get; set; }
    [Parameter(CaptureUnmatchedValues = true)] public Dictionary<string, object>? AdditionalAttributes { get; set; }

    private bool _isRemoved;

    private string CssClass
    {
        get
        {
            var animatedClass = Animated ? "lumeo-badge-animated" : null;
            return IsDot
                ? string.Join(" ", new[] { "inline-flex h-2 w-2 rounded-full p-0", VariantClasses, animatedClass, Class }.Where(s => !string.IsNullOrWhiteSpace(s)))
                : string.Join(" ", new[] { BaseClasses, VariantClasses, animatedClass, Class }.Where(s => !string.IsNullOrWhiteSpace(s)));
        }
    }

    private const string BaseClasses = "inline-flex items-center rounded-md border px-2.5 py-0.5 text-xs font-semibold transition-colors focus:outline-none";

    private string DotClasses => VariantClasses;

    private string PulseColorClass => Variant switch
    {
        BadgeVariant.Default => "bg-primary",
        BadgeVariant.Secondary => "bg-secondary",
        BadgeVariant.Destructive => "bg-destructive",
        BadgeVariant.Outline => "bg-foreground",
        BadgeVariant.Success => "bg-success-text",
        BadgeVariant.Warning => "bg-warning-text",
        _ => "bg-primary"
    };

    private string VariantClasses => Variant switch
    {
        BadgeVariant.Default => "border-transparent bg-primary text-primary-foreground shadow",
        BadgeVariant.Secondary => "border-transparent bg-secondary text-secondary-foreground",
        BadgeVariant.Destructive => "border-transparent bg-destructive text-destructive-foreground shadow",
        BadgeVariant.Outline => "border-border/60 text-foreground",
        BadgeVariant.Success => "border-transparent bg-success-light text-success-text",
        BadgeVariant.Warning => "border-transparent bg-warning-light text-warning-text",
        _ => ""
    };

    private async Task HandleRemove()
    {
        _isRemoved = true;
        await OnRemove.InvokeAsync();
    }

    public enum BadgeVariant { Default, Secondary, Destructive, Outline, Success, Warning }
}
