@using System.Globalization
@using Server.Services
@using System.Net
@inject LocalizationService T
@{
    var currentUrl = WebUtility.HtmlEncode(Context.Request.Path);
}
<!DOCTYPE html>
<html lang="@CultureInfo.CurrentUICulture.TwoLetterISOLanguageName">
<head>
    <meta charset="utf-8" />
    <meta name="description" content="Embeddingsearch server" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - embeddingsearch</title>
    <link rel="preload" href="~/fonts/bootstrap-icons.woff2" as="font" type="font/woff2" crossorigin="anonymous"/>
    @if (!Context.Request.Query.ContainsKey("renderRaw") && !Context.Request.Query.ContainsKey("noCriticalCSS"))
    {
        <link rel="preload" href="~/lib/bootstrap/dist/css/bootstrap.min.css" as="style"/>
        <link rel="stylesheet" fetchpriority="high"
        href="~/lib/bootstrap/dist/css/bootstrap.min.css"
        media="print"
        onload="this.media='all'">
    } else if (Context.Request.Query.ContainsKey("noCriticalCSS"))
    {
        <link rel="preload" href="~/lib/bootstrap/dist/css/bootstrap.min.css" as="style"/>
        <link rel="stylesheet" fetchpriority="high"
        href="~/lib/bootstrap/dist/css/bootstrap.min.css">
    }
    <style>
        @Html.Raw(File.ReadAllText(System.IO.Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "css", "site.css")))
    </style>
    @if (!Context.Request.Query.ContainsKey("noCriticalCSS"))
    {
        <style>
            @if (Context.Request.Path.Value is not null)
            {
                string path = System.IO.Path.Combine("CriticalCSS", Context.Request.Path.Value.Trim('/').Replace("/", ".") + ".css");
                if (File.Exists(path))
                {
                    @Html.Raw(File.ReadAllText(path));
                }
            }
        </style>
    }
    <script>
        window.appTranslations = {
            closeAlert: '@T["Close alert"]'
        };
    </script>
</head>
<body data-bs-theme="dark">
    <header>
        <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light border-bottom box-shadow mb-3">
            <div class="container-fluid">
                <a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">embeddingsearch</a>
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                        aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
                    <ul class="navbar-nav flex-grow-1">
                        @if (User.Identity?.IsAuthenticated == true)
                        {
                            <li class="nav-item">
                                <a class="nav-link text" asp-area="" asp-controller="Home" asp-action="Index">@T["Home"]</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link text" asp-area="" asp-controller="Home" asp-action="Searchdomains">@T["Searchdomains"]</a>
                            </li>
                            @if (User.IsInRole("Admin") || User.IsInRole("Swagger"))
                            {
                                <li class="nav-item">
                                    <a class="nav-link text" href="/swagger/index.html?ReturnUrl=@(currentUrl)">@T["Swagger"]</a>
                                </li>
                            }
                            @if (User.IsInRole("Admin"))
                            {
                                <li class="nav-item">
                                    <a class="nav-link text" href="/elmah?ReturnUrl=@(currentUrl)">@T["Elmah"]</a>
                                </li>
                            }
                            <li class="nav-item">
                                <a class="nav-link text" asp-area="" asp-controller="Account" asp-action="Logout">@T["Logout"]</a>
                            </li>
                        }
                        else
                        {
                            <li class="nav-item">
                                <a class="nav-link text" asp-area="" asp-controller="Account" asp-action="Login">@T["Login"]</a>
                            </li>
                        }
                    </ul>
                </div>
            </div>
        </nav>
    </header>
    <div class="container">
        <main role="main" class="pb-3">
            @RenderBody()
        </main>
    </div>

    <footer class="border-top footer text-muted">
        <div class="container">
            &copy; 2025 - embeddingsearch
        </div>
    </footer>
    <script src="~/lib/jquery/dist/jquery.min.js" defer></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js" defer></script>
    <script src="~/js/site.js" asp-append-version="true" defer></script>
    @await RenderSectionAsync("Scripts", required: false)
</body>
</html>
<script>
  const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');

  function applyTheme(e) {
    document.body.setAttribute(
      'data-bs-theme',
      e.matches ? 'dark' : 'light'
    );
  }

  applyTheme(mediaQuery);
  mediaQuery.addEventListener('change', applyTheme);
</script>
