@using Microsoft.AspNetCore.Components.Routing
@using ModelsLib
@using MudBlazor
@inject NavigationManager Navigation
@rendermode InteractiveServer
@inject IWebHostEnvironment Env
@inject PhotoService ShootingDb
@inject IDialogService DialogService
<MudHidden Breakpoint="Breakpoint.MdAndDown">

    <MudAppBar Color="Color.Dark" Elevation="4">

        <!-- Logo -->
        <MudText Typo="Typo.h6" Class="ml-2">
            <MudNavLink Href="/" Class="nav-link">
                Photo Sharing Helrira
            </MudNavLink>
        </MudText>

        <MudSpacer />

        <!-- Menu desktop -->
        <MudNavMenu Class="d-flex">
            <MudNavLink Href="/shootingdesire" Class="nav-link">
                Projets
            </MudNavLink>
            <MudNavLink Href="/Contact" Class="nav-link">
                Contact
            </MudNavLink>
        </MudNavMenu>

        <!-- Recherche desktop -->
        <MudStack Row="true" Spacing="2" Class="ml-4">
            <MudTextField @bind-Value="Id"
                          Label="Recherche"
                          Variant="Variant.Outlined"
                          Style="color: white;"
                          InputStyle="color: white;" />

            <MudButton OnClick="SendSelection"
                       Variant="Variant.Outlined"
                       Color="Color.Inherit">
                Rechercher
            </MudButton>
        </MudStack>

    </MudAppBar>

</MudHidden>
<MudHidden Breakpoint="Breakpoint.LgAndUp">
<!-- Drawer mobile -->
<!-- AppBar mobile -->
<MudAppBar Color="Color.Dark" Elevation="4">
    <!-- Bouton burger -->
    <MudIconButton Icon="@Icons.Material.Filled.Menu"
                   Color="Color.Default"
                   OnClick="@(() => _drawerOpen = true)" />

    <MudText Typo="Typo.h6" Class="ml-2">
            <MudNavLink Href="/" Class="nav-link">
                Photo Sharing Helrira
            </MudNavLink>
    </MudText>

    <MudSpacer />

    <!-- Bouton recherche -->
    <MudIconButton Icon="@Icons.Material.Filled.Search"
                   Color="Color.Default"
                   OnClick="OpenSearchDialogAsync" />
</MudAppBar>

<!-- Drawer mobile -->
<MudDrawer @bind-Open="_drawerOpen"
           Anchor="Anchor.Left"
           Variant="DrawerVariant.Temporary"
           Color="Color.Dark"
           Class="custom-drawer drawer-fixed-width">

    <MudStack Spacing="2" Class="pa-3" Style="display:flex; flex-direction:column; height:100%;">
        <MudNavMenu Class="flex-grow-1">
                <MudNavLink Href="/shootingdesire">Projets</MudNavLink>
            <MudNavLink Href="/Contact">Contact</MudNavLink>
        </MudNavMenu>
    </MudStack>

</MudDrawer>
</MudHidden>
@code {
    private string Id { get; set; } = "";

    private bool _drawerOpen = false;

    private async Task OpenSearchDialogAsync()
    {
        try
        {
            var parameters = new DialogParameters();
            parameters.Add("OnSearch", EventCallback.Factory.Create<string>(this, SendSelectionMobil));
            var options = new DialogOptions
            {
                MaxWidth = MaxWidth.Small,
                FullWidth = true
            };
            var dialogRef = await DialogService.ShowAsync<SearchDialog>("", parameters, options);
            var result = await dialogRef.Result;
            if (!result.Canceled)
            {

                Console.WriteLine("Résultat : " + result.Data);

            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("ERREUR DIALOG : " + ex.Message);
            Console.WriteLine(ex.StackTrace);
        }
    }

    void ToggleDrawer() => _drawerOpen = !_drawerOpen;
    /// <summary>
    /// a la validation de la saisie
    /// </summary>
    private async void SendSelectionMobil(string value)
    {
        Id = value;
        //si une saisie est faite dans le champs
        SendSelection();
    }

    
    /// <summary>
    /// a la validation de la saisie
    /// </summary>
    private async void SendSelection()
    {
        //si une saisie est faite dans le champs
        if (!string.IsNullOrWhiteSpace(Id))
        {
            string idSaisie = Id.ToLower().Trim();//passage en full minuscule + suppression espace
            var path = Path.Combine(Env.WebRootPath, "Photos", idSaisie);

            bool dossierExiste = Directory.Exists(path);//verification de présence du dossier

            if (dossierExiste)
            {
                ShootingData datas = ShootingDb.GetInfosShooting(idSaisie);
                if (datas == null)
                {
                    await ShootingDb.AddShootingInfoPerDefault(idSaisie);
                }
                else
                {
                    if (datas.IsPhotosSelected)
                    {
                        Navigation.NavigateTo($"/Share?Id={idSaisie}");//redirection vers le shooting post retouche
                    }
                    else
                    {
                        Navigation.NavigateTo($"/PhotoSelect?Id={idSaisie}");//redirection vers le shooting à selectionné
                    }
                }


            }
            else
            {
                Navigation.NavigateTo($"/PhotoSelectError");//redirection vers page d'erreur
            }


        }
    }
}
