@using salesbook.Shared.Core.Dto
@using salesbook.Shared.Core.Entity
@using salesbook.Shared.Core.Helpers.Enum
@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 clearButton">
            <MudTextField T="string?" Placeholder="Cerca..." Variant="Variant.Text" @bind-Value="Filter.Text" DebounceInterval="500"/>

            <MudIconButton Class="closeIcon" Icon="@Icons.Material.Filled.Close" OnClick="() => Filter.Text = null"/>
        </div>

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

                <MudSelectExtended SearchBox="true"
                                   ItemCollection="Users.Select(x => x.UserName).ToList()"
                                   SelectAllPosition="SelectAllPosition.NextToSearchBox"
                                   SelectAll="true"
                                   NoWrap="true"
                                   MultiSelection="true"
                                   MultiSelectionTextFunc="@(new Func<List<string>, string>(GetMultiSelectionUser))"
                                   FullWidth="true" T="string"
                                   Variant="Variant.Text"
                                   Virtualize="true"
                                   @bind-SelectedValues="Filter.User"
                                   Class="customIcon-select"
                                   AdornmentIcon="@Icons.Material.Filled.Code"/>

            </div>

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

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

                <MudSelectExtended FullWidth="true"
                                   T="string?"
                                   Variant="Variant.Text"
                                   @bind-Value="Filter.Type"
                                   Class="customIcon-select"
                                   AdornmentIcon="@Icons.Material.Filled.Code">
                    @foreach (var type in ActivityType)
                    {
                        <MudSelectItemExtended Class="custom-item-select" Value="@type.ActivityTypeId">@type.ActivityTypeId</MudSelectItemExtended>
                    }
                </MudSelectExtended>
            </div>

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

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

                <MudSelectExtended FullWidth="true"
                                   T="string?"
                                   Variant="Variant.Text"
                                   @bind-Value="Filter.Result"
                                   Class="customIcon-select"
                                   AdornmentIcon="@Icons.Material.Filled.Code">
                    @foreach (var result in ActivityResult)
                    {
                        <MudSelectItemExtended Class="custom-item-select" Value="@result.ActivityResultId">@result.ActivityResultId</MudSelectItemExtended>
                    }
                </MudSelectExtended>
            </div>

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

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

                <MudSelectExtended FullWidth="true"
                                   T="ActivityCategoryEnum?"
                                   Variant="Variant.Text"
                                   @bind-Value="Filter.Category"
                                   Class="customIcon-select"
                                   AdornmentIcon="@Icons.Material.Filled.Code">
                    @foreach (var category in CategoryList)
                    {
                        <MudSelectItemExtended T="ActivityCategoryEnum?" Class="custom-item-select" Value="@category">@category.ConvertToHumanReadable()</MudSelectItemExtended>
                    }
                </MudSelectExtended>
            </div>
        </div>

        <div class="button-section">
            <MudButton OnClick="() => Filter = new FilterActivityDTO()" 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 FilterActivityDTO Filter { get; set; }
    [Parameter] public EventCallback<FilterActivityDTO> FilterChanged { get; set; }

    private List<StbActivityResult> ActivityResult { get; set; } = [];
    private List<StbActivityType> ActivityType { get; set; } = [];
    private List<StbUser> Users { get; set; } = [];
    private List<ActivityCategoryEnum> CategoryList { get; set; } = [];

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

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

    private async Task LoadData()
    {
        Users = await manageData.GetTable<StbUser>();
        ActivityResult = await manageData.GetTable<StbActivityResult>();
        ActivityType = await manageData.GetTable<StbActivityType>(x => x.FlagTipologia.Equals("A"));
        CategoryList = ActivityCategoryHelper.AllActivityCategory;

        StateHasChanged();
    }

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

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

}