@namespace SufiChain.SufiBlazor.Components.Actions

@if (!string.IsNullOrEmpty(Href))
{
    <a href="@Href"
       target="@Target"
       rel="@(Target == "_blank" ? "noopener noreferrer" : null)"
       class="@CssClass"
       style="@Style"
       aria-disabled="@(Disabled ? "true" : null)"
       tabindex="@(Disabled ? -1 : 0)"
       @onclick="HandleClick"
       @onclick:preventDefault="@Disabled"
       @attributes="AdditionalAttributes">
        @RenderContent
    </a>
}
else
{
    <button type="@Type"
            class="@CssClass"
            style="@Style"
            disabled="@IsDisabled"
            aria-busy="@(Loading ? "true" : null)"
            @onclick="HandleClick"
            @attributes="AdditionalAttributes">
        @RenderContent
    </button>
}

@code {
    /// <summary>
    /// Button variant.
    /// </summary>
    [Parameter]
    public SbButtonVariant Variant { get; set; } = SbButtonVariant.Solid;

    /// <summary>
    /// Button size.
    /// </summary>
    [Parameter]
    public SbSize Size { get; set; } = SbSize.Md;

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

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

    /// <summary>
    /// Whether the button is in a loading state.
    /// </summary>
    [Parameter]
    public bool Loading { get; set; }

    /// <summary>
    /// Whether the button takes full width.
    /// </summary>
    [Parameter]
    public bool FullWidth { get; set; }

    /// <summary>
    /// Button type (button/submit/reset).
    /// </summary>
    [Parameter]
    public string Type { get; set; } = "button";

    /// <summary>
    /// If set, renders as an anchor element.
    /// </summary>
    [Parameter]
    public string? Href { get; set; }

    /// <summary>
    /// Target for anchor links.
    /// </summary>
    [Parameter]
    public string? Target { get; set; }

    /// <summary>
    /// Icon to render at the start.
    /// </summary>
    [Parameter]
    public RenderFragment? StartIcon { get; set; }

    /// <summary>
    /// Icon to render at the end.
    /// </summary>
    [Parameter]
    public RenderFragment? EndIcon { get; set; }

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

    /// <summary>
    /// Button 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 bool IsDisabled => Disabled || Loading;

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

            classes.Add($"sb-button--{Variant.ToString().ToLowerInvariant()}");
            classes.Add($"sb-button--{Size.ToString().ToLowerInvariant()}");

            if (Color != SbColor.Default)
            {
                classes.Add($"sb-button--{Color.ToString().ToLowerInvariant()}");
            }

            if (IsDisabled)
            {
                classes.Add("sb-button--disabled");
            }

            if (Loading)
            {
                classes.Add("sb-button--loading");
            }

            if (FullWidth)
            {
                classes.Add("sb-button--full-width");
            }

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

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

    private RenderFragment RenderContent => builder =>
    {
        if (Loading)
        {
            builder.OpenElement(0, "span");
            builder.AddAttribute(1, "class", "sb-button__spinner");
            builder.CloseElement();
        }

        if (StartIcon != null)
        {
            builder.OpenElement(2, "span");
            builder.AddAttribute(3, "class", "sb-button__icon sb-button__icon--start");
            builder.AddContent(4, StartIcon);
            builder.CloseElement();
        }

        if (ChildContent != null)
        {
            builder.OpenElement(5, "span");
            builder.AddAttribute(6, "class", "sb-button__content");
            builder.AddContent(7, ChildContent);
            builder.CloseElement();
        }

        if (EndIcon != null)
        {
            builder.OpenElement(8, "span");
            builder.AddAttribute(9, "class", "sb-button__icon sb-button__icon--end");
            builder.AddContent(10, EndIcon);
            builder.CloseElement();
        }
    };

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