@using ImageBoardServerApp.Auth
@using ImageBoardServerApp.Data.Repository
@inject AuthenticationStateProvider authStateProvider 
@inject NavigationManager navManager

<div class="top-row ps-3 navbar navbar-dark">
    <div class="container-fluid">
        <a class="navbar-brand" href="">BB v0.2.3</a>
        <AuthorizeView>
            <Authorized>
                <a class="navbar-brand" @onclick="logout" href="javascript:void(0)" >[Logout]</a>
            </Authorized>
            <NotAuthorized>
                <a class="navbar-brand" href="/sys/login">[Login]</a>
            </NotAuthorized>
        </AuthorizeView>
        <button title="Navigation menu" class="navbar-toggler" @onclick="ToggleNavMenu">
            <span class="navbar-toggler-icon"></span>
        </button>
    </div>
</div>

<div class="@NavMenuCssClass" @onclick="ToggleNavMenu">
    <nav class="flex-column">
        <div class="nav-item px-3">
            <NavLink class="nav-link" href="" Match="NavLinkMatch.All">
                <span class="oi oi-home" aria-hidden="true"></span> Home
            </NavLink>
        </div>
        @foreach (BoardData b in boards)
        {
            <div class="nav-item px-3">
                <NavLink class="nav-link" href="@b.Tag">
                    <span class="oi oi-list-rich" aria-hidden="true"></span> /@b.Tag/ - @b.Topic
                </NavLink>
            </div>
        }
    </nav>
</div>

@code {
    private bool collapseNavMenu = true;

    private string? NavMenuCssClass => collapseNavMenu ? "collapse" : null;

    private void ToggleNavMenu()
    {
        collapseNavMenu = !collapseNavMenu;
    }
    
    private async Task logout()
    {
        var customAuthStateProvider = (CustomAuthenticationStateProvider) authStateProvider;
        await customAuthStateProvider.UpdateAuthenticationStateAsync(null);
        navManager.NavigateTo("/", true);
    }

    public List<BoardData> boards { get; set; }

    protected override async Task OnInitializedAsync()
    {
        boards = await BoardsRepository.getBoardsAsync();
    }

}