<?php

declare(strict_types=1);

use Fisharebest\Webtrees\FlashMessages;
use Fisharebest\Webtrees\Http\RequestHandlers\Logout;
use Fisharebest\Webtrees\Http\RequestHandlers\HomePage;
use Fisharebest\Webtrees\Http\RequestHandlers\SelectLanguage;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Module\CustomCssJsModule;
use Fisharebest\Webtrees\Module\ModuleCustomInterface;
use Fisharebest\Webtrees\Module\ModuleGlobalInterface;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Services\ModuleService;
use Fisharebest\Webtrees\View;

/**
 * @var string $title
 * @var string $content
 */

?>

<!DOCTYPE html>
<html dir="<?= I18N::locale()->direction() ?>" lang="<?= I18N::locale()->languageTag() ?>">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="csrf" content="<?= e(csrf_token()) ?>">

        <title><?= strip_tags($title) ?></title>

        <link rel="icon" href="<?= e(asset('favicon.ico')) ?>" type="image/x-icon">

        <link rel="stylesheet" href="<?= e(asset('css/vendor.min.css')) ?>">
        <link rel="stylesheet" href="<?= e(asset('css/administration.min.css')) ?>">

        <?= View::stack('styles') ?>

        <?= Registry::container()->get(ModuleService::class)->findByInterface(ModuleGlobalInterface::class)->map(static fn (ModuleGlobalInterface $module): string => $module instanceof ModuleCustomInterface || $module instanceof CustomCssJsModule ? '' : $module->headContent())->implode('') ?>
    </head>
    <body class="container-lg wt-global wt-control-panel">
        <header>
            <div class="wt-accessibility-links position-fixed">
                <a class="visually-hidden visually-hidden-focusable btn btn-info btn-sm" href="#content">
                    <?= /* I18N: Skip over the headers and menus, to the main content of the page */
                    I18N::translate('Skip to content') ?>
                </a>
            </div>

            <ul class="nav small d-flex justify-content-end">
                <li class="nav-item menu-mypage">
                    <a class="nav-link active" href="<?= e(route(HomePage::class)) ?>"><?= I18N::translate('My page') ?></a>
                </li>
                <li class="nav-item dropdown menu-language">
                    <a class="nav-link dropdown-toggle" data-bs-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">
                        <?= I18N::translate('Language') ?>
                    </a>
                    <div class="dropdown-menu">
                        <?php foreach (I18N::activeLocales() as $locale) : ?>
                            <a class="dropdown-item menu-language-<?= $locale->languageTag() ?> <?= I18N::languageTag() === $locale->languageTag() ? ' active' : '' ?>" href="#" data-wt-post-url="<?= e(route(SelectLanguage::class, ['language' => $locale->languageTag()])) ?>"><?= $locale->endonym() ?></a>
                        <?php endforeach ?>
                    </div>
                </li>
                <li class="nav-item menu-logout">
                    <a class="nav-link" href="#" data-wt-post-url="<?= e(route(Logout::class)) ?>" data-wt-reload-url="<?= e(route(HomePage::class)) ?>"><?= I18N::translate('Sign out') ?></a>
                </li>
            </ul>
        </header>

        <main id="content">
            <?php foreach (FlashMessages::getMessages() as $message) : ?>
                <div class="alert alert-<?= $message->status ?> alert-dismissible" role="alert">
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="<?= I18N::translate('close') ?>">
                    </button>
                    <?= $message->text ?>
                </div>
            <?php endforeach ?>

            <?= $content ?>
        </main>


        <script src="<?= e(asset('js/vendor.min.js')) ?>"></script>
        <script src="<?= e(asset('js/webtrees.min.js')) ?>"></script>

        <?= View::stack('javascript') ?>

        <?= Registry::container()->get(ModuleService::class)->findByInterface(ModuleGlobalInterface::class)->map(static fn (ModuleGlobalInterface $module): string => $module instanceof ModuleCustomInterface || $module instanceof CustomCssJsModule ? '' : $module->bodyContent())->implode('') ?>
    </body>
</html>
