@inject IJSRuntime JS

<div class="@(Back ? "" : "container") header">
    <div class="header-content @(Back ? "with-back" : "no-back")">
        @if (Back)
        {
            <div class="left-section">
                <MudButton StartIcon="@(!Cancel ? Icons.Material.Outlined.ArrowBackIosNew : "")"
                           OnClick="GoBack"
                           Color="Color.Info"
                           Style="text-transform: none"
                           Variant="Variant.Text">
                    @BackTo
                </MudButton>
            </div>
        }

        <h3 class="page-title">@Title</h3>

        <div class="right-section">
            @if (LabelSave.IsNullOrEmpty())
            {
                @if (ShowFilter)
                {
                    <MudIconButton OnClick="OnFilterToggle" Icon="@Icons.Material.Outlined.FilterAlt"/>
                }

                @* @if (ShowCalendarToggle)
                {
                    <MudIconButton OnClick="OnCalendarToggle" Icon="@Icons.Material.Filled.CalendarMonth" Color="Color.Dark"/>
                } *@

                @if (ShowProfile)
                {
                    <MudIconButton Class="user" OnClick="OpenPersonalInfo" Icon="@Icons.Material.Filled.Person"/>
                }
            }
            else
            {
                <MudButton OnClick="OnSave"
                           Color="Color.Info"
                           Style="text-transform: none"
                           Variant="Variant.Text">
                    @LabelSave
                </MudButton>
            }
        </div>
    </div>
</div>

@code{
    [Parameter] public string? Title { get; set; }
    [Parameter] public bool ShowFilter { get; set; }
    [Parameter] public bool ShowProfile { get; set; } = true;
    [Parameter] public bool Back { get; set; }
    [Parameter] public bool BackOnTop { get; set; }
    [Parameter] public string BackTo { get; set; } = "";

    [Parameter] public EventCallback OnFilterToggle { get; set; }

    [Parameter] public bool Cancel { get; set; }
    [Parameter] public EventCallback OnCancel { get; set; }
    [Parameter] public string? LabelSave { get; set; }
    [Parameter] public EventCallback OnSave { get; set; }

    [Parameter] public bool ShowCalendarToggle { get; set; }
    [Parameter] public EventCallback OnCalendarToggle { get; set; }

    protected override void OnParametersSet()
    {
        Back = !Back ? !Back && Cancel : Back;
        BackTo = Cancel ? "Annulla" : BackTo;
    }

    private async Task GoBack()
    {
        if (Cancel)
        {
            await OnCancel.InvokeAsync();
            return;
        }

        await JS.InvokeVoidAsync("goBack");
    }

    private void OpenPersonalInfo() =>
        NavigationManager.NavigateTo("/PersonalInfo");

}