@using Moonlight.App.Helpers
@using Moonlight.App.Repositories
@using Moonlight.App.Services
@using Moonlight.App.Services.Sessions
@using Moonlight.App.Database.Entities
@using Task = System.Threading.Tasks.Task

@inject IdentityService IdentityService
@inject NavigationManager NavigationManager
@inject CookieService CookieService

@if (User != null)
{
    <div class="menu menu-column justify-content-center"
         data-kt-menu="true">
        <div class="menu-item">
            <div class="dropdown">
                <button class="btn btn-success dropdown-toggle" type="button" data-bs-toggle="dropdown">
                    <TL>Create</TL>
                </button>
                <ul class="dropdown-menu">
                    <li>
                        <a class="dropdown-item py-2" href="/servers/create">
                            <TL>Server</TL>
                        </a>
                    </li>
                    <li>
                        <a class="dropdown-item py-2" href="/domains/create">
                            <TL>Domain</TL>
                        </a>
                    </li>
                    <li>
                        <a class="dropdown-item py-2" href="/webspaces/create">
                            <TL>Webspace</TL>
                        </a>
                    </li>
                </ul>
            </div>
        </div>
    </div>
}

<div class="app-navbar flex-shrink-0">
    <div class="app-navbar-item ms-1 ms-lg-3">
        <ThemeSwitcher
            ToggleBtnClass="btn btn-icon btn-custom btn-icon-muted btn-active-light btn-active-color-primary w-35px h-35px w-md-40px h-md-40px"
            ToggleBtnIconClass="svg-icon svg-icon-2">
        </ThemeSwitcher>
    </div>

    @if (User != null)
    {
        <div class="app-navbar-item ms-1 ms-lg-3">
            <a href="/support" class="btn btn-icon btn-custom btn-icon-muted btn-active-light btn-active-color-primary w-35px h-35px w-md-40px h-md-40px position-relative">
                <i class="bx bx-support"></i>
            </a>
        </div>

        <div class="app-navbar-item ms-1 ms-lg-3" id="kt_header_user_menu_toggle">
            <div class="cursor-pointer symbol symbol-35px symbol-md-40px" data-kt-menu-trigger="click" data-kt-menu-attach="parent" data-kt-menu-placement="bottom-end">
                <img alt="Avatar" src="/api/moonlight/avatar/@(User.Id)"/>
            </div>

            <div class="menu menu-sub menu-sub-dropdown menu-column menu-rounded menu-gray-800 menu-state-bg menu-state-primary fw-semibold py-4 fs-6 w-275px" data-kt-menu="true">
                <div class="menu-item px-3">
                    <div class="menu-content d-flex align-items-center px-3">
                        <div class="symbol symbol-50px me-5">
                            <img alt="Avatar" src="/api/moonlight/avatar/@(User.Id)"/>
                        </div>
                        <div class="d-flex flex-column">
                            <div class="fw-bold d-flex align-items-center fs-5">
                                @(User.FirstName) @(User.LastName)

                                @if (User.Admin)
                                {
                                    <span class="badge badge-light-success fw-bold fs-8 px-2 py-1 ms-2">Admin</span>
                                }
                            </div>
                            <a class="fw-semibold text-muted text-hover-primary fs-7">@(User.Email)</a>
                        </div>
                    </div>
                </div>
                <div class="separator my-2"></div>
                <div class="menu-item px-5 my-1">
                    <a href="/profile" class="menu-link px-5">
                        <TL>Profile</TL>
                    </a>
                </div>
                <div class="menu-item px-5">
                    <a @onclick="Logout" class="menu-link px-5">
                        <TL>Logout</TL>
                    </a>
                </div>
            </div>
        </div>
    }
</div>

@code
{
    private User? User;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            User = await IdentityService.Get();

            await InvokeAsync(StateHasChanged);
        }
    }

    private async void Logout()
    {
        await CookieService.SetValue("token", "", 1);
        NavigationManager.NavigateTo(NavigationManager.Uri, true);
    }
}