@using salesbook.Shared.Components.Layout.Spinner
@using salesbook.Shared.Core.Dto
@using salesbook.Shared.Core.Interface
@using salesbook.Shared.Components.Layout.Overlay
@inject IIntegryApiService IntegryApiService

<div class="bottom-sheet-backdrop @(IsSheetVisible ? "show" : "")" @onclick="CloseBottomSheet"></div>

<div class="bottom-sheet-container @(IsSheetVisible ? "show" : "")">
    <div style="height: 95vh" class="bottom-sheet pb-safe-area">
        <div class="title">
            <MudText Typo="Typo.h6">
                <b>Cerca indirizzo</b>
            </MudText>
            <MudIconButton Icon="@Icons.Material.Filled.Close" OnClick="() => CloseBottomSheet()"/>
        </div>
        
        <div class="input-card clearButton">
            <MudTextField T="string?" Placeholder="Cerca..." Variant="Variant.Text" @bind-Value="Address" DebounceInterval="500" OnDebounceIntervalElapsed="SearchAllAddress" />

            <MudIconButton Class="closeIcon" Icon="@Icons.Material.Filled.Close" OnClick="() => Address = null" />
        </div>
        
        <div>
            @if (Loading)
            {
                <SpinnerLayout />
            }
            else
            {
                if (Addresses != null)
                {
                    foreach (var address in Addresses)
                    {
                        <b><span @onclick="() => OnSelectAddress(address.PlaceId)">@address.Description</span></b>

                        <div class="divider"></div>
                    }
                }
                else
                {
                    <NoDataAvailable Text="Nessun indirizzo trovato" />
                }
            }
        </div>
    </div>
</div>

<SaveOverlay VisibleOverlay="VisibleOverlay" SuccessAnimation="SuccessAnimation"/>

@code {
    [Parameter] public string Region { get; set; }

    [Parameter] public IndirizzoDTO Indirizzo { get; set; }
    [Parameter] public EventCallback<IndirizzoDTO> IndirizzoChanged { get; set; }

    [Parameter] public bool IsSheetVisible { get; set; }
    [Parameter] public EventCallback<bool> IsSheetVisibleChanged { get; set; }

    private bool Loading { get; set; }

    private string? Address { get; set; }
    private List<AutoCompleteAddressDTO>? Addresses { get; set; }

    private string? Uuid { get; set; }

    //Overlay for save
    private bool VisibleOverlay { get; set; }
    private bool SuccessAnimation { get; set; }

    protected override async Task OnParametersSetAsync()
    {
        if (IsSheetVisible)
        {
            Uuid = Guid.NewGuid().ToString();
            Address = null;
            Addresses = null;
        }
    }

    private void CloseBottomSheet()
    {
        IsSheetVisible = false;
        IsSheetVisibleChanged.InvokeAsync(IsSheetVisible);
    }

    private async Task SearchAllAddress()
    {
        if (Address == null || Uuid == null) return;

        Loading = true;
        StateHasChanged();

        Addresses = await IntegryApiService.AutoCompleteAddress(Address, Region, Uuid);

        Loading = false;
        StateHasChanged();
    }

    private async Task OnSelectAddress(string placeId)
    {
        CloseBottomSheet();

        VisibleOverlay = true;
        StateHasChanged();

        try
        {
            Indirizzo = await IntegryApiService.PlaceDetails(placeId, Uuid);
            await IndirizzoChanged.InvokeAsync(Indirizzo);
        }
        catch (Exception e)
        {
            Snackbar.Configuration.PositionClass = Defaults.Classes.Position.TopCenter;
            Snackbar.Clear();

            Snackbar.Add("Impossibile selezionare questo indirizzo", Severity.Error);
        }

        VisibleOverlay = false;
        StateHasChanged();
    }

}