@implements IDisposable

@inject NavigationManager NavigationManager
@inject AuthenticationStateProvider AuthStateProvider
@inject EntKube.Web.Services.UserAccessService UserAccessService
@inject IConfiguration Configuration

<div class="top-row ps-3 navbar navbar-dark">
    <div class="container-fluid">
        <a class="navbar-brand" href="">EntKube</a>
    </div>
</div>

<input type="checkbox" title="Navigation menu" class="navbar-toggler" />

<div class="nav-scrollable" onclick="document.querySelector('.navbar-toggler').click()">
    <nav class="nav flex-column">
        <div class="nav-item px-3">
            <NavLink class="nav-link" href="" Match="NavLinkMatch.All">
                <span class="bi bi-house-door-fill-nav-menu" aria-hidden="true"></span> Home
            </NavLink>
        </div>

        @if (showTenantsLink)
        {
            <div class="nav-item px-3">
                <NavLink class="nav-link" href="tenants">
                    <span class="bi bi-list-nested-nav-menu" aria-hidden="true"></span> Tenants
                </NavLink>
            </div>
        }

        <div class="nav-item px-3">
            <NavLink class="nav-link" href="portal">
                <span class="bi bi-grid-nav-menu" aria-hidden="true"></span> Portal
            </NavLink>
        </div>

        <div class="nav-item px-3">
            <NavLink class="nav-link" href="portal/status">
                <span class="bi bi-activity-nav-menu" aria-hidden="true"></span> Status
            </NavLink>
        </div>

        <AuthorizeView Roles="Admin">
            <Authorized>
                <div class="nav-item px-3">
                    <NavLink class="nav-link" href="admin/users">
                        <span class="bi bi-people-nav-menu" aria-hidden="true"></span> Users
                    </NavLink>
                </div>
                <div class="nav-item px-3">
                    <NavLink class="nav-link" href="admin/roles">
                        <span class="bi bi-shield-lock-nav-menu" aria-hidden="true"></span> Roles
                    </NavLink>
                </div>
                <div class="nav-item px-3">
                    <NavLink class="nav-link" href="admin/backup">
                        <span class="bi bi-arrow-repeat-nav-menu" aria-hidden="true"></span> Backup
                    </NavLink>
                </div>
            </Authorized>
        </AuthorizeView>

        <AuthorizeView>
            <Authorized>
                <div class="nav-item px-3">
                    <NavLink class="nav-link" href="Account/Manage">
                        <span class="bi bi-person-fill-nav-menu" aria-hidden="true"></span> @context.User.Identity?.Name
                    </NavLink>
                </div>
                <div class="nav-item px-3">
                    <form action="Account/Logout" method="post">
                        <AntiforgeryToken />
                        <input type="hidden" name="ReturnUrl" value="@currentUrl" />
                        <button type="submit" class="nav-link">
                            <span class="bi bi-arrow-bar-left-nav-menu" aria-hidden="true"></span> Logout
                        </button>
                    </form>
                </div>
            </Authorized>
            <NotAuthorized>
                @if (allowRegistration)
                {
                    <div class="nav-item px-3">
                        <NavLink class="nav-link" href="Account/Register">
                            <span class="bi bi-person-nav-menu" aria-hidden="true"></span> Register
                        </NavLink>
                    </div>
                }
                <div class="nav-item px-3">
                    <NavLink class="nav-link" href="Account/Login">
                        <span class="bi bi-person-badge-nav-menu" aria-hidden="true"></span> Login
                    </NavLink>
                </div>
            </NotAuthorized>
        </AuthorizeView>
    </nav>
</div>

@code {
    private string? currentUrl;
    private bool showTenantsLink;
    private bool allowRegistration = true;

    protected override async Task OnInitializedAsync()
    {
        currentUrl = NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
        NavigationManager.LocationChanged += OnLocationChanged;

        allowRegistration = Configuration.GetValue<bool>("Auth:AllowRegistration", true);

        AuthenticationState authState = await AuthStateProvider.GetAuthenticationStateAsync();
        System.Security.Claims.ClaimsPrincipal user = authState.User;

        if (user.Identity?.IsAuthenticated == true)
        {
            string? userId = user.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value;
            if (userId is not null)
                showTenantsLink = await UserAccessService.HasAnyAccessAsync(userId);
        }
    }

    private void OnLocationChanged(object? sender, LocationChangedEventArgs e)
    {
        currentUrl = NavigationManager.ToBaseRelativePath(e.Location);
        StateHasChanged();
    }

    public void Dispose()
    {
        NavigationManager.LocationChanged -= OnLocationChanged;
    }
}
