@using JSMR.Application.Tags.Queries.Search.Contracts
@using JSMR.Application.VoiceWorks.Queries.Search
@using JSMR.UI.Blazor.Enums
@using JSMR.UI.Blazor.Filters
@using JSMR.UI.Blazor.Services
@using Microsoft.AspNetCore.WebUtilities

<Chip Graphic="Graphic.Tag" Color="@GetColor()" Varient="ElementVarient.Outlined" Tone="@GetTone()" Click="@OnClick">@Tag.Name</Chip>

@code {
    [Inject]
    protected NavigationManager NavigationManager { get; set; } = default!;

    [Parameter]
    public required VoiceWorkTagItem Tag { get; set; }

    private ColorVarient GetColor()
    {
        if (Tag.IsFavorite)
        {
            return ColorVarient.Mint;
        }

        if (Tag.IsBlacklisted)
        {
            return ColorVarient.Pink;
        }

        return ColorVarient.Secondary;
    }

    private ToneVarient GetTone()
    {
        if (Tag.IsFavorite || Tag.IsBlacklisted)
        {
            return ToneVarient.Tint;
        }

        return ToneVarient.None;
    }

    private void OnClick()
    {
        VoiceWorkFilterState state = new()
        {
            TagIds = [Tag.TagId]
        };

        string basePath = new Uri(NavigationManager.Uri).GetLeftPart(UriPartial.Authority);
        string uri = QueryHelpers.AddQueryString($"{basePath}/voiceworks", state.ToQuery());

        NavigationManager.NavigateTo(uri);
    }
}