@namespace OpenArchival.Blazor.AdminPages.Shared
@using Microsoft.EntityFrameworkCore;
@using Microsoft.Extensions.Logging
@using MudBlazor.Interfaces
@using OpenArchival.DataAccess
@using MudBlazor
@using MudExtensions

@page "/categorieslist"

<MudPaper Class="pa-4 ma-2 rounded" Elevation="3">
    <MudText Typo="Typo.h6">Categories</MudText>
    <MudDivider Class="mb-2"></MudDivider>
    <MudList T="string" Clickable="true">
        @foreach (ArchiveCategory category in _categories)
        {
            <MudListItem OnClick="@(() => OnCategoryItemClicked(category))">
                @category.Name
                @if (ShowDeleteButton)
                {
                    <MudListItemMeta ActionPosition="ActionPosition.End">
                        <MudIconButton 
                            Icon="@Icons.Material.Filled.Delete"
                            Color="Color.Error"
                            Size="Size.Small"
                            OnClick="@((e) => HandleDeleteClick(category))"
                             />
                    </MudListItemMeta>
                }
            </MudListItem>
        }
    </MudList>
    @ChildContent
</MudPaper>

@inject IArchiveCategoryProvider CategoryProvider;
@inject ILogger<CategoriesListComponent> Logger;

@code {
    [Parameter]
    public RenderFragment ChildContent { get; set; } = default!;

    [Parameter]
    public bool ShowDeleteButton { get; set; } = false;

    [Parameter]
    public EventCallback<ArchiveCategory> ListItemClickedCallback { get; set; }

    [Parameter]
    public EventCallback<ArchiveCategory> OnDeleteClickedCallback { get; set; }

    private List<ArchiveCategory> _categories = new();

    protected override async Task OnInitializedAsync()
    {
        await LoadCategories();
    }

    private async Task LoadCategories()
    {
        var categories = await CategoryProvider.GetAllArchiveCategories();
        if (categories is null)
        {
            Logger.LogError("There were no categories in the database when attempting to load the list of categories.");
            _categories.Clear();
            return;
        }
        _categories = categories.ToList();
    }

    public async Task RefreshData()
    {
        await LoadCategories();
        StateHasChanged();
    }

    private async Task OnCategoryItemClicked(ArchiveCategory category)
    {
        await ListItemClickedCallback.InvokeAsync(category);
    }

    private async Task HandleDeleteClick(ArchiveCategory category)
    {
        await OnDeleteClickedCallback.InvokeAsync(category);
    }
}