@namespace SufiChain.SufiBlazor.Components.Actions

<a href="@(Disabled ? null : Href)"
   target="@Target"
   rel="@ComputedRel"
   class="@CssClass"
   style="@Style"
   aria-disabled="@(Disabled ? "true" : null)"
   tabindex="@(Disabled ? -1 : 0)"
   @onclick="HandleClick"
   @onclick:preventDefault="@Disabled"
   @attributes="AdditionalAttributes">
    @ChildContent
    @if (External)
    {
        <span class="sb-link__external-icon sb-icon--mirror" aria-hidden="true">↗</span>
    }
</a>

@code {
    /// <summary>
    /// Link URL.
    /// </summary>
    [Parameter]
    public string? Href { get; set; }

    /// <summary>
    /// Link target.
    /// </summary>
    [Parameter]
    public string? Target { get; set; }

    /// <summary>
    /// Link rel attribute.
    /// </summary>
    [Parameter]
    public string? Rel { get; set; }

    /// <summary>
    /// Whether the link is disabled.
    /// </summary>
    [Parameter]
    public bool Disabled { get; set; }

    /// <summary>
    /// Whether to show an external link indicator.
    /// </summary>
    [Parameter]
    public bool External { get; set; }

    /// <summary>
    /// Link color.
    /// </summary>
    [Parameter]
    public SbColor Color { get; set; } = SbColor.Primary;

    /// <summary>
    /// Whether to show underline.
    /// </summary>
    [Parameter]
    public SbLinkUnderline Underline { get; set; } = SbLinkUnderline.Hover;

    /// <summary>
    /// Click event callback.
    /// </summary>
    [Parameter]
    public EventCallback<MouseEventArgs> OnClick { get; set; }

    /// <summary>
    /// Link content.
    /// </summary>
    [Parameter]
    public RenderFragment? ChildContent { get; set; }

    /// <summary>
    /// Additional CSS classes.
    /// </summary>
    [Parameter]
    public string? Class { get; set; }

    /// <summary>
    /// Inline styles.
    /// </summary>
    [Parameter]
    public string? Style { get; set; }

    /// <summary>
    /// Additional HTML attributes.
    /// </summary>
    [Parameter(CaptureUnmatchedValues = true)]
    public Dictionary<string, object>? AdditionalAttributes { get; set; }

    private string? ComputedRel
    {
        get
        {
            if (!string.IsNullOrEmpty(Rel))
            {
                return Rel;
            }

            if (Target == "_blank" || External)
            {
                return "noopener noreferrer";
            }

            return null;
        }
    }

    private string CssClass
    {
        get
        {
            var classes = new List<string> { "sb-link" };

            classes.Add($"sb-link--{Color.ToString().ToLowerInvariant()}");
            classes.Add($"sb-link--underline-{Underline.ToString().ToLowerInvariant()}");

            if (Disabled)
            {
                classes.Add("sb-link--disabled");
            }

            if (External)
            {
                classes.Add("sb-link--external");
            }

            if (!string.IsNullOrWhiteSpace(Class))
            {
                classes.Add(Class);
            }

            return string.Join(" ", classes);
        }
    }

    private async Task HandleClick(MouseEventArgs args)
    {
        if (!Disabled)
        {
            await OnClick.InvokeAsync(args);
        }
    }
}
