@using System.Threading.Channels
@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>Esito</b>
            </MudText>
            <MudIconButton Icon="@Icons.Material.Filled.Close" OnClick="CloseBottomSheet"/>
        </div>
        
        <div class="input-card">
            <div class="form-container">
                <span>Data effettiva</span>

                <MudTextField T="DateTime?" Format="yyyy-MM-dd" InputType="InputType.Date" @bind-Value="EffectiveDate" />
            </div>

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

            <div class="form-container">
                <span>Inizio</span>

                <MudTextField T="TimeSpan" InputType="InputType.Time" @bind-Value="EffectiveTime" @bind-Value:after="OnAfterChangeTime"/>
            </div>
            
            <div class="divider"></div>

            <div class="form-container">
                <span>Fine</span>

                <MudTextField T="TimeSpan" InputType="InputType.Time" @bind-Value="EffectiveEndTime" @bind-Value:after="OnAfterChangeTime" />
            </div>
        </div>

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

                <MudSelectExtended FullWidth="true" T="string?" Variant="Variant.Text" @bind-Value="ActivityModel.ActivityResultId" 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>
        
        <div class="input-card">
            <MudTextField T="string?" Placeholder="Descrizione esito" Variant="Variant.Text" Lines="4" @bind-Value="ActivityModel.ResultDescription" />
        </div>

        <div class="button-section">
            <MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="() => CloseBottomSheet(true)">Salva</MudButton>
        </div>
    </div>
</div>

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

    [Parameter] public ActivityDTO ActivityModel { get; set; }
    [Parameter] public EventCallback<ActivityDTO> ActivityModelChanged { get; set; }

    private List<StbActivityResult> ActivityResult { get; set; } = [];

    private DateTime? EffectiveDate { get; set; } = DateTime.Today;

    private TimeSpan EffectiveTime { get; set; }
    private TimeSpan EffectiveEndTime { get; set; } 

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

    private async Task LoadData()
    {
        ActivityResult = await ManageData.GetTable<StbActivityResult>();

        EffectiveTime = ActivityModel.EffectiveTime?.TimeOfDay ?? TimeSpan.Zero;
        EffectiveEndTime = ActivityModel.EffectiveEndtime?.TimeOfDay ?? TimeSpan.Zero;
        StateHasChanged();
    }

    private void CloseBottomSheet(bool save)
    {
        if (save)
        {
            if (EffectiveDate != null)
            {
                ActivityModel.EffectiveTime = new DateTime(EffectiveDate!.Value.Year, EffectiveDate!.Value.Month, EffectiveDate!.Value.Day,
                    EffectiveTime.Hours, EffectiveTime.Minutes, EffectiveTime.Seconds);

                ActivityModel.EffectiveEndtime = new DateTime(EffectiveDate!.Value.Year, EffectiveDate!.Value.Month, EffectiveDate!.Value.Day,
                    EffectiveEndTime.Hours, EffectiveEndTime.Minutes, EffectiveEndTime.Seconds);
            }

            if (ActivityModel.ActivityResultId.IsNullOrEmpty())
            {
                Snackbar.Clear();
                Snackbar.Configuration.PositionClass = Defaults.Classes.Position.TopCenter;

                Snackbar.Add("Esito obbligatorio!", Severity.Error);
                return;
            }
        }

        IsSheetVisible = false;
        IsSheetVisibleChanged.InvokeAsync(IsSheetVisible);

        if (save)
        {
            ActivityModelChanged.InvokeAsync(ActivityModel);
        }
    }

    private void CloseBottomSheet() => CloseBottomSheet(false);

    private void OnAfterChangeTime()
    {
        if (EffectiveEndTime < EffectiveTime)
            EffectiveEndTime = EffectiveTime + TimeSpan.FromHours(1);
    }

}