@using System.Globalization
@using System.Net
@using Microsoft.Extensions.Options
@using Wave.Data
@using Wave.Utilities
@inherits LayoutComponentBase

@inject NavigationManager Navigation
@inject IOptions<RequestLocalizationOptions> LocalizationOptions
@inject IOptions<Customization> Customizations
@inject IStringLocalizer<MainLayout> Localizer

<div class="drawer md:drawer-open drawer-end">
	<input id="main-drawer" type="checkbox" class="drawer-toggle" />
	<div class="drawer-content">
		<div class="flex flex-col h-screen overflow-x-auto">
			<div class="md:hidden p-4 flex justify-between bg-base-200">
				<div class="h-12">
					<LogoPartial/>
				</div>
				<label for="main-drawer" class="btn btn-square btn-ghost drawer-button" title="open navigation">
					<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
						<path stroke-linecap="round" stroke-linejoin="round" d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25h16.5" />
					</svg>
				</label>
			</div>
			<main class="flex-1 flex flex-col">
				<SectionOutlet SectionName="GlobalHeader" />
				<input id="narrow-reading-toggle" checked="@Customizations.Value.DefaultNarrowReader" type="checkbox" class="narrow-reading-toggle" />
				<div class="flex-1 container mx-auto px-4 md:px-12 py-8 reading-toggle-target h-full">
					<AlertComponent CanDelete="true" />
					@Body
					@if (HttpContext is null || HttpContext?.GetEndpoint()?
						     .Metadata.GetMetadata<RenderModeAttribute>()?
					     .Mode is not null) { 
						// for some reason that's how you test for interactive render modes
						<ToastComponent />
					}
				</div>
			</main>
			<footer class="flex flex-col md:flex-row items-center justify-center p-4 gap-y-3 gap-x-4 bg-base-300 text-base-content">
				<span class="flex-1 text-center md:text-left">
					@if (Customizations.Value.Footer is { } footer && !string.IsNullOrWhiteSpace(footer)) {
						<span class="prose prose-neutral">@MarkdownUtilities.ParseToMarkup(footer)</span>
					} else {
						<text>Powered by</text>
						<a class="link link-primary" target="_blank" href="https://github.com/miawinter98/Wave">Wave</a>
					}
				</span>
				<div class="join w-56" title="@Localizer["Language_Label"]">
					<button type="button" class="btn btn-sm btn-neutral btn-square no-animation join-item" aria-hidden="true" tabindex="-1">
						<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="w-6 h-6">
							<path d="M21.721 12.752a9.711 9.711 0 0 0-.945-5.003 12.754 12.754 0 0 1-4.339 2.708 18.991 18.991 0 0 1-.214 4.772 17.165 17.165 0 0 0 5.498-2.477ZM14.634 15.55a17.324 17.324 0 0 0 .332-4.647c-.952.227-1.945.347-2.966.347-1.021 0-2.014-.12-2.966-.347a17.515 17.515 0 0 0 .332 4.647 17.385 17.385 0 0 0 5.268 0ZM9.772 17.119a18.963 18.963 0 0 0 4.456 0A17.182 17.182 0 0 1 12 21.724a17.18 17.18 0 0 1-2.228-4.605ZM7.777 15.23a18.87 18.87 0 0 1-.214-4.774 12.753 12.753 0 0 1-4.34-2.708 9.711 9.711 0 0 0-.944 5.004 17.165 17.165 0 0 0 5.498 2.477ZM21.356 14.752a9.765 9.765 0 0 1-7.478 6.817 18.64 18.64 0 0 0 1.988-4.718 18.627 18.627 0 0 0 5.49-2.098ZM2.644 14.752c1.682.971 3.53 1.688 5.49 2.099a18.64 18.64 0 0 0 1.988 4.718 9.765 9.765 0 0 1-7.478-6.816ZM13.878 2.43a9.755 9.755 0 0 1 6.116 3.986 11.267 11.267 0 0 1-3.746 2.504 18.63 18.63 0 0 0-2.37-6.49ZM12 2.276a17.152 17.152 0 0 1 2.805 7.121c-.897.23-1.837.353-2.805.353-.968 0-1.908-.122-2.805-.353A17.151 17.151 0 0 1 12 2.276ZM10.122 2.43a18.629 18.629 0 0 0-2.37 6.49 11.266 11.266 0 0 1-3.746-2.504 9.754 9.754 0 0 1 6.116-3.985Z" />
						</svg>
					</button>
					<select class="select select-bordered select-sm md:text-center join-item min-w-0 flex-1" aria-label="@Localizer["Language_Label"]"
							onchange="window.location = `/language/${this.value}?returnUrl=@WebUtility.UrlEncode("/" + Navigation.ToBaseRelativePath(Navigation.Uri))`">
						@foreach (var culture in LocalizationOptions.Value.SupportedCultures ?? new List<CultureInfo>()) {
							<option value="@culture"
							        selected="@(CultureInfo.CurrentUICulture.Equals(culture))">
								@culture.NativeName
							</option>
						}
					</select>
				</div>
				<div class="join w-56" title="@Localizer["Theme_Label"]">
					<button type="button" class="btn btn-square btn-sm btn-neutral no-animation join-item" aria-hidden="true" tabindex="-1">
						<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="w-6 h-6">
							<path fill-rule="evenodd" d="M2.25 4.125c0-1.036.84-1.875 1.875-1.875h5.25c1.036 0 1.875.84 1.875 1.875V17.25a4.5 4.5 0 1 1-9 0V4.125Zm4.5 14.25a1.125 1.125 0 1 0 0-2.25 1.125 1.125 0 0 0 0 2.25Z" clip-rule="evenodd" />
							<path d="M10.719 21.75h9.156c1.036 0 1.875-.84 1.875-1.875v-5.25c0-1.036-.84-1.875-1.875-1.875h-.14l-8.742 8.743c-.09.089-.18.175-.274.257ZM12.738 17.625l6.474-6.474a1.875 1.875 0 0 0 0-2.651L15.5 4.787a1.875 1.875 0 0 0-2.651 0l-.1.099V17.25c0 .126-.003.251-.01.375Z" />
						</svg>
					</button>
					<select id="theme-select" class="select select-bordered select-sm md:text-center join-item min-w-0 flex-1" aria-label="@Localizer["Theme_Label"]"
					        onchange="window.location = `/theme/${this.value}?returnUrl=@WebUtility.UrlEncode("/" + Navigation.ToBaseRelativePath(Navigation.Uri))`">
						<option selected="@(UserTheme is null)" value="">@Localizer["Theme_Default"]</option>
						<option selected="@(UserTheme is "wave-light")" value="wave-light">@Localizer["Theme_WaveLight"]</option>
						<option selected="@(UserTheme is "wave-dark")" value="wave-dark">@Localizer["Theme_WaveDark"]</option>
						<option selected="@(UserTheme is "modern-light")" value="modern-light">@Localizer["Theme_ModernLight"]</option>
						<option selected="@(UserTheme is "modern-dark")" value="modern-dark">@Localizer["Theme_ModernDark"]</option>
					</select>
				</div>
			</footer>
		</div>
	</div>
	<div class="drawer-side">
		<label for="main-drawer" aria-label="close sidebar" class="drawer-overlay"></label>
		
		<div class="bg-base-200 p-4 h-full flex flex-col gap-4 w-48 lg:w-64">
			<NavMenu />
			
			@if (Customizations.Value.HideVersion is not true) {
				<a aria-hidden="true" tabindex="-1" class="absolute left-2 right-2 bottom-2 text-center text-slate-400" href="https://github.com/miawinter98/wave/releases/">@Version</a>
			}
		</div>
	</div>
</div>

@code {
	[CascadingParameter(Name = "Version")]
    private string Version { get; set; } = string.Empty;
	[CascadingParameter(Name = "UserTheme")]
	private string? UserTheme { get; set; }
	[CascadingParameter]
	private HttpContext? HttpContext { get; set; }
}
