@page "/Share"
@rendermode InteractiveServer
@using BlazorBootstrap;
@using Microsoft.AspNetCore.WebUtilities
@using ModelsLib
@using PhotoShareHelri.Components.Blocs
@using System.Text
@inject NavigationManager NavManager
@inject EmailService mailService
@inject PhotoService ShootingDb
@inject IJSRuntime JS
@using MudBlazor

<MudContainer MaxWidth="MaxWidth.Large" Class="d-flex justify-center mt-6 mb-6">
    <MudPaper Elevation="3" Class="pa-6 selection-wrapper">

        <!-- Header -->
        <MudStack Spacing="1" AlignItems="AlignItems.Center" Class="mb-4 text-center">
            <MudText Typo="Typo.h4">Résultat du shooting</MudText>
            <MudText Typo="Typo.h6" Class="title-shoot">@Title</MudText>
        </MudStack>

  
        <!-- Galerie -->
        <MudGrid Justify="Justify.Center" Spacing="3">
            @for (int index = 0; index < Images.Count; index++)
            {
                var img = Images[index];
               

                <MudItem xs="12" sm="6" md="4" lg="3" xl="2">
                    <MudPaper Class="image-card pa-2 position-relative" Elevation="2">

                        <MudImage Src="@img"
                                  Alt="Photo sélection"
                                  Class="gallery-thumb"
                                  Fluid="true"
                                  @onclick="() => OpenModalZoom(img)" />
                    </MudPaper>
                </MudItem>
            }
        </MudGrid>

    </MudPaper>
</MudContainer>


@* Modal de zoom sur une image *@
<ModalDiapo IsOpen="@IsModalZoomOpen"
            CurrentImage="@CurrentImage"
            CurrentIndex="@CurrentIndex"
            IsSelector=false
            Total="@Images.Count"
            CanNext="@CanNext"
            CanPrevious="@CanPrevious"
            MaxSelect="@MaxSelect"
            SelectedImages="@SelectedImages"
            IsSelected="@(CurrentImage != null && @SelectedImages.Contains(@CurrentImage))"
            OnClose="CloseModalZoom"
            OnNext="NextModalZoom"
            OnPrevious="PreviousModalZoom"
            Toggle="() => ToggleImage(CurrentImage!)" />


@code {
    #region Variables
    HashSet<string> SelectedImages = new();
    private int MaxSelect = 1;

    // Liste des images disponibles
    private List<string> Images = new();

    public string Title { get; set; }
    bool IsModalZoomOpen = false;
    string? CurrentImage;

    int CurrentIndex =>
        CurrentImage == null ? -1 : Images.IndexOf(CurrentImage);

    bool CanNext => CurrentIndex >= 0 && CurrentIndex < Images.Count - 1;
    bool CanPrevious => CurrentIndex > 0;
    private string ShootingId;

    #endregion

    #region modal zoom
    /// <summary>
    /// Ouverture de la modal à partir du clic sur image voulu
    /// </summary>
    void OpenModalZoom(string img)
    {
        CurrentImage = img;
        IsModalZoomOpen = true;
    }

    ///<summary>
    /// fermeture de la modal
    /// </summary>
    void CloseModalZoom()
    {
        IsModalZoomOpen = false;
    }

    /// <summary>
    /// photo suivante dans la modal
    /// dans la liste des photos affichés
    /// </summary>
    void NextModalZoom()
    {
        if (CanNext)
            CurrentImage = Images[CurrentIndex + 1];
    }

    /// <summary>
    /// photo précédente
    /// </summary>
    void PreviousModalZoom()
    {
        if (CanPrevious)
            CurrentImage = Images[CurrentIndex - 1];
    }
    #endregion

    /// <summary>
    /// arrivé sur la page
    /// </summary>
    protected override async Task OnInitializedAsync()
    {
        //récupération de l'identifiant de shooting, si identifiant null ou si recherche inexistante redirection vers la page de recherche de shooting
        var uri = NavManager.ToAbsoluteUri(NavManager.Uri);
        if (QueryHelpers.ParseQuery(uri.Query).TryGetValue("Id", out var id))
        {
            if (!string.IsNullOrEmpty(id))
            {
                ShootingId = id;

                Images = ShootingDb.LireContenu(ShootingId, true);

            }
        }

        // Code exécuté au chargement du composant
    }

    /// <summary>
    /// Déclenche le téléchargement de l'image originale sur la modal de visualisation
    /// </summary>
    /// <param name="img">image sélectionné par son hash</param>
    private async Task ToggleImage(string img)
    {
        var filePath = $"{img}"; // chemin dans wwwroot
        await JS.InvokeVoidAsync("downloadFile", filePath, $"{img}");
    }
}