@using CommunityToolkit.Mvvm.Messaging
@using salesbook.Shared.Core.Dto
@using salesbook.Shared.Core.Entity
@using salesbook.Shared.Core.Messages.Activity.Copy
@using salesbook.Shared.Core.Messages.Activity.New
@inject IDialogService Dialog
@inject IMessenger Messenger
@inject CopyActivityService CopyActivityService

<div class="container animated-navbar @(IsVisible ? "show-nav" : "hide-nav") @(IsVisible? PlusVisible ? "with-plus" : "without-plus" : "with-plus")">
    <nav class="navbar @(IsVisible? PlusVisible ? "with-plus" : "without-plus" : "with-plus")">
        <div class="container-navbar">
            <ul class="navbar-nav flex-row nav-justified align-items-center w-100 text-center">
                <li class="nav-item">
                    <NavLink class="nav-link" href="Users" Match="NavLinkMatch.All">
                        <div class="d-flex flex-column">
                            <i class="ri-group-line"></i>
                            <span>Contatti</span>
                        </div>
                    </NavLink>
                </li>
                <li class="nav-item">
                    <NavLink class="nav-link" href="Calendar" Match="NavLinkMatch.All">
                        <div class="d-flex flex-column">
                            <i class="ri-calendar-todo-line"></i>
                            <span>Agenda</span>
                        </div>
                    </NavLink>
                </li>
                <li class="nav-item">
                    <NavLink class="nav-link" href="Notifications" Match="NavLinkMatch.All">
                        <div class="d-flex flex-column">
                            <i class="ri-notification-4-line"></i>
                            <span>Notifiche</span>
                        </div>
                    </NavLink>
                </li>
            </ul>
        </div>

        @if (PlusVisible)
        {
            <MudMenu PopoverClass="custom_popover" AnchorOrigin="Origin.TopLeft" TransformOrigin="Origin.BottomRight">
                <ActivatorContent>
                    <MudFab Class="custom-plus-button" Color="Color.Surface" Size="Size.Medium" IconSize="Size.Medium" IconColor="Color.Primary" StartIcon="@Icons.Material.Filled.Add" />
                </ActivatorContent>
                <ChildContent>
                    <MudMenuItem Disabled="true">Nuovo contatto</MudMenuItem>
                    <MudMenuItem OnClick="CreateActivity">Nuova attivitą</MudMenuItem>
                </ChildContent>
            </MudMenu>
        }
    </nav>
</div>

@code
{
    private bool IsVisible { get; set; } = true;
    private bool PlusVisible { get; set; } = true;

    protected override Task OnInitializedAsync()
    {
        CopyActivityService.OnCopyActivity += async dto => await CreateActivity(dto);

        NavigationManager.LocationChanged += (_, args) =>
        {
            var location = args.Location.Remove(0, NavigationManager.BaseUri.Length);

            var newIsVisible = new List<string> { "Calendar", "Users", "Notifications" }
                .Contains(location);

            var newPlusVisible = new List<string> { "Calendar", "Users" }
                .Contains(location);

            if (IsVisible == newIsVisible && PlusVisible == newPlusVisible) return;

            IsVisible = newIsVisible;
            PlusVisible = newPlusVisible;
            StateHasChanged();
        };
        return Task.CompletedTask;
    }

    private Task CreateActivity() => CreateActivity(null);

    private async Task CreateActivity(ActivityDTO? activity)
    {
        var result = await ModalHelpers.OpenActivityForm(Dialog, activity, null);

        if (result is { Canceled: false, Data: not null } && result.Data.GetType() == typeof(StbActivity))
        {
            Messenger.Send(new NewActivityMessage(((StbActivity)result.Data).ActivityId));
        }
    }
}

