@page "/PrivateList"
@rendermode InteractiveServer

@using CouchLog.Components.Pages.PrivateList.Components
@using Microsoft.AspNetCore.Authorization
@using CouchLog.Data
@using Microsoft.AspNetCore.Identity
@using Microsoft.EntityFrameworkCore

@inject ApplicationDbContext CouchLogDb
@inject UserManager<ApplicationUser> UserManager
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject NavigationManager NavigationManager

@attribute [Authorize]

<PageTitle>Private List</PageTitle>

<MudContainer MaxWidth="MaxWidth.False">
    @*Top of the Page*@
    <MudContainer>
        <MudStack Row="true" AlignItems="AlignItems.Center">
            @*Page Title*@
            <MudText Typo="Typo.h3" Style="font-weight: bold;">Private List</MudText>
        </MudStack>
    </MudContainer>
    
    <br/>
    
    <MudContainer>
        <MudPaper Class="pa-2" Elevation="1">
            <MudStack Row="true" AlignItems="AlignItems.Center" Spacing="2">
                <MudTextField @bind-Value="_searchString"
                              Placeholder="Search..."
                              Adornment="Adornment.Start"
                              AdornmentIcon="@Icons.Material.Filled.Search"
                              Immediate="true"
                              Variant="Variant.Outlined"
                              Margin="Margin.Dense"
                              Style="max-width: 300px;"/>
            </MudStack>
        </MudPaper>
    </MudContainer>
    
    <br/>
    
    <MudContainer MaxWidth="MaxWidth.False">
        <MudGrid Spacing="6" Justify="Justify.Center">
            @foreach (var privateEntity in ActiveWatchingEntities)
            {
                <EntityCard PrivateEntity="privateEntity" />
            }
            @foreach (var privateEntity in LibraryEntities)
            {
                <EntityCard PrivateEntity="privateEntity" />
            }
        </MudGrid>
    </MudContainer>
</MudContainer>

@code
{
    //Search
    private string _searchString;
    
    //User
    private ApplicationUser? _appUser;
    
    //Entities
    private List<PrivateEntity> _privateEntities = [];
    private List<UserWatchStatus> _userWatchStatuses = [];

    private List<PrivateEntity> ActiveWatchingEntities => _privateEntities.Where(x => x.UserWatchStatus?.Name == "Watching").ToList();
    private List<PrivateEntity> LibraryEntities => _privateEntities.Where(x => x.UserWatchStatus?.Name != "Watching").ToList();

    protected override async Task OnInitializedAsync()
    {
        var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
        _appUser = await UserManager.GetUserAsync(authState.User);

        if (_appUser != null)
        {
            _privateEntities = await CouchLogDb.PrivateEntities
                .Where(x => x.UserId == _appUser.Id)
                .Include(x => x.GlobalEntity)
                .Include(x => x.UserWatchStatus)
                .Include(x => x.GlobalEntity.MediaType)
                .OrderByDescending(entity => entity.LastChange ?? entity.CreationTime)
                .ToListAsync();

            _userWatchStatuses = await CouchLogDb.UserWatchStatuses.OrderByDescending(entity => entity.Id).ToListAsync();
        }
    }
}