@using salesbook.Shared.Core.Dto
@using salesbook.Shared.Core.Entity
@using salesbook.Shared.Core.Interface
@inject IManageDataService manageData

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

<div class="bottom-sheet-container @(IsSheetVisible ? "show" : "")">
    <div class="bottom-sheet pb-safe-area">
        <div class="title">
            <MudText Typo="Typo.h6">
                <b>Filtri</b>
            </MudText>
            <MudIconButton Icon="@Icons.Material.Filled.Close" OnClick="CloseBottomSheet"/>
        </div>

        <div class="input-card">
            <div class="form-container">
                <span class="disable-full-width">Con agente</span>

                <MudCheckBox @bind-Value="Filter.ConAgente" Color="Color.Primary" @bind-Value:after="OnAfterChangeConAgente"/>
            </div>

            <div class="divider"></div>

            <div class="form-container">
                <span class="disable-full-width">Senza agente</span>

                <MudCheckBox @bind-Value="Filter.SenzaAgente" Color="Color.Primary" @bind-Value:after="OnAfterChangeSenzaAgente"/>
            </div>

            <div class="divider"></div>

            <div class="form-container">
                <span class="disable-full-width">Agente</span>
                
                <MudSelectExtended FullWidth="true" T="string?" Variant="Variant.Text" NoWrap="true"
                                   @bind-SelectedValues="Filter.Agenti" @bind-SelectedValues:after="OnAfterChangeAgenti"
                                   MultiSelection="true" MultiSelectionTextFunc="@(new Func<List<string>, string>(GetMultiSelectionAgente))"
                                   SelectAllPosition="SelectAllPosition.NextToSearchBox" SelectAll="true"
                                   Class="customIcon-select" AdornmentIcon="@Icons.Material.Filled.Code">
                    @foreach (var user in Users)
                    {
                        <MudSelectItemExtended Class="custom-item-select" Value="@user.UserCode">@($"{user.UserCode} - {user.FullName}")</MudSelectItemExtended>
                    }
                </MudSelectExtended>
            </div>
        </div>

        <div class="input-card">
            <div class="form-container">
                <MudTextField T="string?"
                              Placeholder="Indirizzo"
                              Variant="Variant.Text"
                              @bind-Value="Filter.Indirizzo"
                              DebounceInterval="500"/>
            </div>

            <div class="divider"></div>

            <div class="form-container">
                <MudTextField T="string?"
                              Placeholder="Città"
                              Variant="Variant.Text"
                              @bind-Value="Filter.Citta"
                              DebounceInterval="500"/>
            </div>

            <div class="divider"></div>

            <div class="form-container">
                <MudTextField T="string?"
                              Placeholder="Provincia"
                              Variant="Variant.Text"
                              @bind-Value="Filter.Prov"
                              DebounceInterval="500"/>
            </div>

            <div class="divider"></div>

            <div class="form-container">
                <MudTextField T="string?"
                              Placeholder="Nazione"
                              Variant="Variant.Text"
                              @bind-Value="Filter.Nazione"
                              DebounceInterval="500"/>
            </div>
        </div>

        <div class="button-section">
            <MudButton OnClick="ClearFilters" Variant="Variant.Outlined" Color="Color.Error">Pulisci</MudButton>
            <MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="OnFilterButton">Filtra</MudButton>
        </div>
    </div>
</div>

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

    [Parameter] public FilterUserDTO Filter { get; set; }
    [Parameter] public EventCallback<FilterUserDTO> FilterChanged { get; set; }

    private List<StbUser> Users { get; set; } = [];

    protected override async Task OnParametersSetAsync()
    {
        if (IsSheetVisible)
            await LoadData();
    }

    private async Task LoadData()
    {
        Users = await manageData.GetTable<StbUser>(x => x.KeyGroup == 5);
    }

    private string GetMultiSelectionAgente(List<string> selectedValues)
    {
        return $"{selectedValues.Count} Agent{(selectedValues.Count != 1 ? "i selezionati" : "e selezionato")}";
    }

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

    private void OnFilterButton()
    {
        FilterChanged.InvokeAsync(Filter);
        CloseBottomSheet();
    }

    private void OnAfterChangeAgenti()
    {
        if (Filter.Agenti == null || !Filter.Agenti.Any()) return;

        Filter.ConAgente = false;
        Filter.SenzaAgente = false;
    }

    private void ClearFilters()
    {
        Filter.ConAgente = false;
        Filter.SenzaAgente = false;
        Filter.Agenti = [];
        Filter.Indirizzo = null;
        Filter.Citta = null;
        Filter.Nazione = null;
        Filter.Prov = null;
    }

    private void OnAfterChangeConAgente() => Filter.SenzaAgente = false;

    private void OnAfterChangeSenzaAgente() => Filter.ConAgente = false;

}