@page "/"
@inject VoiceWorksClient Client
@using JSMR.Application.VoiceWorks.Queries.Search
@using JSMR.UI.Blazor.Components
@using JSMR.UI.Blazor.Services

<PageTitle>Home</PageTitle>

<BitPivot Size="BitSize.Medium">
    <BitPivotItem HeaderText="@($"Available ({availableVoiceWorks?.Length ?? 0})")">
        <JProductCollection Products="availableVoiceWorks"></JProductCollection>
    </BitPivotItem>
    <BitPivotItem HeaderText="@($"Upcoming ({upcomingVoiceWorks?.Length ?? 0})")">
        <JProductCollection Products="upcomingVoiceWorks"></JProductCollection>
    </BitPivotItem>
    <BitPivotItem HeaderText="@($"Announcements ({announcedVoiceWorks?.Length ?? 0})")">
        <JProductCollection Products="announcedVoiceWorks"></JProductCollection>
    </BitPivotItem>
</BitPivot>

@code {
    VoiceWorkSearchResult[]? availableVoiceWorks;
    VoiceWorkSearchResult[]? upcomingVoiceWorks;
    VoiceWorkSearchResult[]? announcedVoiceWorks;

    protected override Task OnInitializedAsync()
    {
        _ = LoadAvailableVoiceWorksAsync();
        _ = LoadUpcomingVoiceWorksAsync();
        _ = LoadAnnouncedVoiceWorksAsync();

        return Task.CompletedTask;
    }

    private async Task LoadAvailableVoiceWorksAsync()
    {
        SearchVoiceWorksRequest request = new(
            Options: new()
            {
                Criteria = new()
                {
                    CircleStatus = CircleStatus.Favorited,
                    ReleaseDateStart = DateOnly.FromDateTime(DateTime.Today.AddDays(-6))
                },
                SortOptions =
                [
                    new(VoiceWorkSortField.ReleaseDate, Application.Common.Search.SortDirection.Descending),
                    new(VoiceWorkSortField.Downloads, Application.Common.Search.SortDirection.Descending)
                ]
            }
        );

        var result = await Client.SearchAsync(request);

        availableVoiceWorks = result?.Results.Items;

        await InvokeAsync(StateHasChanged);
    }

    private async Task LoadUpcomingVoiceWorksAsync()
    {
        SearchVoiceWorksRequest request = new(
            Options: new()
            {
                Criteria = new()
                {
                    CircleStatus = CircleStatus.Favorited,
                    ScheduledReleaseDateEnd = DateOnly.FromDateTime(DateTime.Today.AddDays(8))
                },
                SortOptions =
                [
                    new(VoiceWorkSortField.ScheduledReleaseDate, Application.Common.Search.SortDirection.Ascending),
                    new(VoiceWorkSortField.WishlistCount, Application.Common.Search.SortDirection.Descending)
                ]
            }
        );

        var result = await Client.SearchAsync(request);

        upcomingVoiceWorks = result?.Results.Items;

        await InvokeAsync(StateHasChanged);
    }

    private async Task LoadAnnouncedVoiceWorksAsync()
    {
        SearchVoiceWorksRequest request = new(
            Options: new()
            {
                Criteria = new()
                {
                    Status = Domain.Enums.VoiceWorkStatus.NewAndUpcoming
                },
                SortOptions =
                [
                    new(VoiceWorkSortField.FavoriteCircle, Application.Common.Search.SortDirection.Ascending),
                    new(VoiceWorkSortField.WishlistCount, Application.Common.Search.SortDirection.Descending)
                ]
            }
        );

        var result = await Client.SearchAsync(request);

        announcedVoiceWorks = result?.Results.Items;

        await InvokeAsync(StateHasChanged);
    }
}