@using JSMR.Application.VoiceWorks.Queries.Search
@using JSMR.Domain.Enums
@using JSMR.UI.Blazor.Components.Chips
@using JSMR.UI.Blazor.Enums
@using JSMR.UI.Blazor.Filters
@using JSMR.UI.Blazor.Services
@using System.Globalization
@using Microsoft.AspNetCore.WebUtilities

<div class=@GetCardClasses(Product)>
    <div class="j-voice-work-image-container">
        <JImage OverlayClass="j-voice-work-image-overlay" ImageClass="j-voice-work-image" Source="@ImageUrlProvider.GetImageUrl(Product, "main")" FallbackSource="@ImageUrlProvider.GetImageUrl(Product, "main", "webp")"></JImage>
    </div>
    <div class="j-voice-work-content">
        <div class="j-product-title">
            <a href="@Product.ProductUrl" target="_blank">@Product.ProductName</a>
        </div>
        <div class="j-product-contributors">
            <span class="j-circle">
                <MudChip T="string"
                         Href=@($"https://www.dlsite.com/maniax/circle/profile/=/maker_id/{Product.MakerId}.html")
                         Target="_blank"
                         Variant="MudBlazor.Variant.Filled"
                         Icon="@Icons.Material.Outlined.Circle">@Product.Maker</MudChip>
                @* <CircleChip Circle="@Product.Circle"></CircleChip> *@
                @foreach (var creator in Product.Creators)
                {
                    <MudChip T="string"
                             Href=@($"https://www.dlsite.com/maniax/fsr/=/keyword_creater/{creator.Name}")
                             Target="_blank"
                             Variant="MudBlazor.Variant.Filled"
                             Icon="@Icons.Material.Filled.Person">@creator.Name</MudChip>
                    @* <CreatorChip Creator="@creator"></CreatorChip> *@
                }
            </span>
        </div>
        <div class="j-product-description">@Product.Description</div>
        <div class="j-tags">
            @foreach (var tag in Product.Tags)
            {
               @*  <div class="j-tag">@tag.Name</div> *@
                <ProductTag Tag="tag"></ProductTag>
            }
        </div>
       @*  <div class="j-tags">
            @foreach (var tag in Product.Tags)
            {
                <TagChip Tag="tag"></TagChip>
            }
        </div> *@
    </div>
    <div class="j-voice-work-info">
        <div class="j-release-date-container">
            <span class="j-icon j-icon-calendar"></span>
            <span>@GetReleaseDateText(Product)</span>
        </div>
        <div class="j-wishlist-container">
            <span class="j-icon j-icon-star j-icon-color-yellow"></span>
            <span>@((Product.WishlistCount ?? 0).ToString("n0"))</span>
        </div>
        @if (Product.SalesDate is not null)
        {
            <div class="j-downloads-container">
                <span class="j-icon j-icon-bag-fill j-icon-color-green"></span>
                <span>@((Product.Downloads ?? 0).ToString("n0"))</span>
            </div>
        }
        @* <div class="j-icon-2 j-icon-2-flag-@GetFlagClassSuffix(Product)"></div> *@
        <div class="j-spacer"></div>
        <BitStack Horizontal="true" Gap="0.5rem" VerticalAlign="BitAlignment.End" HorizontalAlign="BitAlignment.End">
            @if (Product.IsValid != true)
            {
                <ProductIndicator Graphic="Graphic.Warning" IconVarient="IconVarient.Fill" Color="ColorVarient.Orange" BackgroundColor="ColorVarient.Black"></ProductIndicator>
            }
            @if (Product.OriginalProductId is not null)
            {
                <ProductIndicator Graphic="Graphic.Translate" Color="ColorVarient.Primary" BackgroundColor="ColorVarient.Black"></ProductIndicator>
            }
            @if (Product.Favorite)
            {
                <ProductIndicator Graphic="Graphic.Star" Color="ColorVarient.Pink" BackgroundColor="ColorVarient.Black"></ProductIndicator>
            }
            @if (Product.HasTrial || Product.HasChobit)
            {
                <ProductIndicator Graphic="Graphic.Headphones" Color="ColorVarient.Blue" BackgroundColor="ColorVarient.Black"></ProductIndicator>
            }
        </BitStack>
    </div>
</div>

@code {
    [Parameter]
    public required VoiceWorkSearchResult Product { get; set; }

    private string GetCardClasses(VoiceWorkSearchResult voiceWork)
    {
        List<string> classNames = ["j-card", "j-voice-work-card"];

        if (voiceWork.Status == (byte)VoiceWorkStatus.NewRelease)
        {
            classNames.Add("type-sale");
        }
        else if (voiceWork.Status == (byte)VoiceWorkStatus.NewAndUpcoming)
        {
            classNames.Add("type-new");
        }

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

    private string GetReleaseDateText(VoiceWorkSearchResult voiceWork)
    {
        if (voiceWork.SalesDate.HasValue)
        {
            return voiceWork.SalesDate.Value.ToString("MMMM d, yyyy", CultureInfo.CurrentCulture);
        }

        if (voiceWork.PlannedReleaseDate.HasValue)
        {
            return voiceWork.PlannedReleaseDate.Value.ToString("MMMM d, yyyy", CultureInfo.CurrentCulture);
        }

        if (voiceWork.ExpectedDate.HasValue)
        {
            string part = voiceWork.ExpectedDate.Value.Day switch
            {
                21 => "Late",
                11 => "Middle",
                _ => "Early"
            };

            return $"{part} {voiceWork.ExpectedDate.Value.ToString("MMMM yyyy")}";
        }

        return "Unknown";
    }

    private string GetFlagClassSuffix(VoiceWorkSearchResult voiceWork)
    {
        switch (voiceWork.SubtitleLanguage)
        {
            case 1:
                return "us";
            case 2:
                return "cn";
            case 3:
                return "kr";
            default:
                return "jp";
        }
    }
}
