@using JSMR.UI.Blazor.Enums

@if (string.IsNullOrWhiteSpace(Url))
{
    <div class="@GetClasses()" @onclick="@OnClickAsync">
        @if (Graphic != null && Graphic != Enums.Graphic.None)
        {
            <Icon Graphic="@Graphic.Value"
                  Varient="@(IconVarient ?? Enums.IconVarient.None)"
                  Size="@(IconSize ?? Enums.SizeVarient.Small)"
                  UseCurrentColor>
            </Icon>
        }
        <span>@ChildContent</span>
    </div>
}
else
{
    <a class="@GetClasses()" href="@Url" target="@Target">
        @if (Graphic != null)
        {
            <Icon
                Graphic="@Graphic.Value"
                Varient="@(IconVarient ?? Enums.IconVarient.None)"
                Size="@(IconSize ?? Enums.SizeVarient.Small)"
                UseCurrentColor>
            </Icon>
        }
        <span>@ChildContent</span>
    </a>
}


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

    [Parameter]
    public Graphic? Graphic { get; set; }

    [Parameter]
    public IconVarient? IconVarient { get; set; }

    [Parameter]
    public SizeVarient? IconSize { get; set; }

    [Parameter]
    public ColorVarient Color { get; set; } = ColorVarient.Primary;

    [Parameter]
    public ElementVarient Varient { get; set; } = ElementVarient.None;

    [Parameter]
    public ToneVarient Tone { get; set; } = ToneVarient.None;

    [Parameter]
    public string? Url { get; set; }

    [Parameter]
    public string? Target { get; set; }

    [Parameter]
    public EventCallback Click { get; set; }

    private string GetClasses()
    {
        string color = Color.ToString().ToLower();

        List<string> classNames =
        [
            $"j-chip",
            $"color-{color}"
        ];

        switch (Varient)
        {
            case ElementVarient.Filled:
                classNames.Add($"varient-filled");
                //classNames.Add($"background-color-{color}");
                break;
            case ElementVarient.Outlined:
                classNames.Add($"varient-outlined");
                //classNames.Add($"border-color-{color}");
                break;
        }

        switch (Tone)
        {
            case ToneVarient.Solid:
                classNames.Add($"tone-solid");
                break;
            case ToneVarient.Tint:
                classNames.Add($"tone-tint");
                break;
        }

        if (Varient == ElementVarient.Filled && Tone == ToneVarient.Tint)
        {
            classNames.Add($"varient-tint");
        }

        if (Click.HasDelegate || string.IsNullOrWhiteSpace(Url) == false)
        {
            classNames.Add("is-clickable");
        }

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

    private async Task OnClickAsync()
    {
        if (Click.HasDelegate)
        {
            await Click.InvokeAsync();
        }
    }
}