@using System.Security.Claims
@using Microsoft.Extensions.Options
@using Wave.Data
@implements IDisposable

@inject NavigationManager NavigationManager
@inject IOptions<Features> Features
@inject IStringLocalizer<NavMenu> Localizer

<div class="h-12 hidden md:block" role="banner" aria-label="logo">
	<LogoPartial />
</div>                         
<nav class="flex-1 flex flex-col justify-between" aria-label="Main">
	<ul class="menu" aria-label="site navigation">
		<li><NavLink href="" Match="NavLinkMatch.All">@Localizer["Home_Label"]</NavLink></li>
		<AuthorizeView Policy="ArticleEditPermissions">
			<Authorized>
				<li><NavLink href="article/new" data-enhance-nav="false">@Localizer["ArticleNew_Label"]</NavLink></li>
				<li><NavLink href="drafts">@Localizer["Drafts_Label"]</NavLink></li>
			</Authorized>
		</AuthorizeView>
		<AuthorizeView Policy="ArticleReviewPermissions">
			<Authorized>
				<li><NavLink href="review">@Localizer["Review_Label"]</NavLink></li>
			</Authorized>
		</AuthorizeView>
		<AuthorizeView Policy="ArticleDeletePermissions">
			<Authorized>
				<li><NavLink href="future">@Localizer["Future_Label"]</NavLink></li>
				<li><NavLink href="deleted">@Localizer["Deleted_Label"]</NavLink></li>
			</Authorized>
		</AuthorizeView>
		<AuthorizeView Policy="CategoryManagePermissions">
			<Authorized>
				<li><NavLink href="manage/categories">@Localizer["ManageCategories_Label"]</NavLink></li>
			</Authorized>
		</AuthorizeView>
		<AuthorizeView Policy="RoleAssignPermissions">
			<Authorized>
				<li><NavLink href="manage/users">@Localizer["ManageUsers_Label"]</NavLink></li>
			</Authorized>
		</AuthorizeView>
		<AuthorizeView Roles="Admin">
			<Authorized>
				<li><NavLink href="manage/api">@Localizer["ManageApi_Label"]</NavLink></li>
				<li><NavLink href="newsletter">@Localizer["Newsletter_Label"]</NavLink></li>
			</Authorized>
		</AuthorizeView>
	</ul>
	<ul class="menu gap-2 mb-2" aria-label="Account">
		<AuthorizeView>
			<Authorized>
				<li class="flex gap-2">
					<NavLink href="/Account/Manage">
						<span class="line-clamp-2">@context.User.FindFirst("FullName")!.Value</span>
						<div class="w-8">
							<ProfilePictureComponent Size="100" ProfileId="@context.User.FindFirst("Id")!.Value" />
						</div>
					</NavLink>
				</li>
				<li class="">
					<form action="/Account/Logout" method="post">
						<AntiforgeryToken />
						<input type="hidden" name="ReturnUrl" value="@_currentUrl" />
						<button type="submit" class="flex gap-2">
							@Localizer["Logout_Label"]
							<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="w-5 h-5">
								<path fill-rule="evenodd" d="M3 4.25A2.25 2.25 0 0 1 5.25 2h5.5A2.25 2.25 0 0 1 13 4.25v2a.75.75 0 0 1-1.5 0v-2a.75.75 0 0 0-.75-.75h-5.5a.75.75 0 0 0-.75.75v11.5c0 .414.336.75.75.75h5.5a.75.75 0 0 0 .75-.75v-2a.75.75 0 0 1 1.5 0v2A2.25 2.25 0 0 1 10.75 18h-5.5A2.25 2.25 0 0 1 3 15.75V4.25Z" clip-rule="evenodd" />
								<path fill-rule="evenodd" d="M6 10a.75.75 0 0 1 .75-.75h9.546l-1.048-.943a.75.75 0 1 1 1.004-1.114l2.5 2.25a.75.75 0 0 1 0 1.114l-2.5 2.25a.75.75 0 1 1-1.004-1.114l1.048-.943H6.75A.75.75 0 0 1 6 10Z" clip-rule="evenodd" />
							</svg>
						</button>
					</form>
				</li>
			</Authorized>
			<NotAuthorized>
				<li>
					<NavLink href="/Account/Login">
						@Localizer["Login_Label"]
						<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="w-5 h-5">
							<path fill-rule="evenodd" d="M17 4.25A2.25 2.25 0 0 0 14.75 2h-5.5A2.25 2.25 0 0 0 7 4.25v2a.75.75 0 0 0 1.5 0v-2a.75.75 0 0 1 .75-.75h5.5a.75.75 0 0 1 .75.75v11.5a.75.75 0 0 1-.75.75h-5.5a.75.75 0 0 1-.75-.75v-2a.75.75 0 0 0-1.5 0v2A2.25 2.25 0 0 0 9.25 18h5.5A2.25 2.25 0 0 0 17 15.75V4.25Z" clip-rule="evenodd" />
							<path fill-rule="evenodd" d="M1 10a.75.75 0 0 1 .75-.75h9.546l-1.048-.943a.75.75 0 1 1 1.004-1.114l2.5 2.25a.75.75 0 0 1 0 1.114l-2.5 2.25a.75.75 0 1 1-1.004-1.114l1.048-.943H1.75A.75.75 0 0 1 1 10Z" clip-rule="evenodd" />
						</svg>
					</NavLink>
				</li>
				@if (Features.Value.NativeSignup) {
					<li>
						<NavLink href="/Account/Register">
							@Localizer["SignUp_Label"]
						</NavLink>
					</li>
				}
			</NotAuthorized>
		</AuthorizeView>
	</ul>
</nav>

@code {
	private string? _currentUrl;

	protected override void OnInitialized() {
		_currentUrl = NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
		NavigationManager.LocationChanged += OnLocationChanged;
	}

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

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

}

