@using MudBlazor

@if (IsOpen && !string.IsNullOrWhiteSpace(CurrentImage))
{
    <MudOverlay Visible="true" DarkBackground="true" ZIndex="1300" @onclick="CloseFromOverlay">
        <MudPaper Class="pa-4 mx-auto"
                  @onclick:stopPropagation="true"
                  Style="
                            width:95%;
                            max-width:1200px;
                            margin-top:5vh;
                            background-color:#1e1e1e;
                            color:white;
                            position:relative;
                            border-radius:16px;">

            <div class="text-center">

                <MudImage Src="@CurrentImage"
                          Fluid="true"
                          Class="mb-3"
                          Style="max-height:75vh; object-fit:contain;" />

                <MudStack Row="true"
                          Justify="Justify.SpaceBetween"
                          AlignItems="AlignItems.Center"
                          Class="mt-2">

                    <MudButton Variant="Variant.Outlined"
                               Color="Color.Inherit"
                               OnClick="OnPrevious"
                               Disabled="@(!CanPrevious)">
                        ⬅ Précédent
                    </MudButton>

                    <MudButton Variant="Variant.Outlined"
                               Color="Color.Inherit"
                               Disabled="@(SelectedImages.Count() >= MaxSelect && !SelectedImages.Contains(CurrentImage))"
                               OnClick="Toggle">
                        @(IsSelector ? (IsSelected ? "Retirer" : "Choisir") : "Télécharger")
                    </MudButton>

                    <MudButton Variant="Variant.Outlined"
                               Color="Color.Inherit"
                               OnClick="OnNext"
                               Disabled="@(!CanNext)">
                        Suivant ➡
                    </MudButton>
                </MudStack>

                <MudText Class="mt-3" Align="Align.Center" Typo="Typo.body1">
                    @(CurrentIndex + 1) / @Total
                </MudText>
            </div>

            <MudIconButton Icon="@Icons.Material.Filled.Close"
                           Color="Color.Inherit"
                           OnClick="OnClose"
                           Style="position:absolute; top:10px; right:10px;" />
        </MudPaper>
    </MudOverlay>
}

@code {
    [Parameter] public bool IsOpen { get; set; }
    [Parameter] public string? CurrentImage { get; set; }
    [Parameter] public bool IsSelector { get; set; }
    [Parameter] public int CurrentIndex { get; set; }
    [Parameter] public int Total { get; set; }
    [Parameter] public HashSet<string> SelectedImages { get; set; }
    [Parameter] public int MaxSelect { get; set; }

    [Parameter] public bool CanNext { get; set; }
    [Parameter] public bool CanPrevious { get; set; }

    [Parameter] public bool IsSelected { get; set; }

    [Parameter] public EventCallback OnClose { get; set; }
    [Parameter] public EventCallback OnNext { get; set; }
    [Parameter] public EventCallback OnPrevious { get; set; }
    [Parameter] public EventCallback Toggle { get; set; }

    private async Task CloseFromOverlay()
    {
        await OnClose.InvokeAsync();
    }
}

