<?php

declare(strict_types=1);

use Fisharebest\Webtrees\FlashMessages;
use Fisharebest\Webtrees\Http\RequestHandlers\AppleTouchIconPng;
use Fisharebest\Webtrees\Http\RequestHandlers\BrowserconfigXml;
use Fisharebest\Webtrees\Http\RequestHandlers\SearchQuickAction;
use Fisharebest\Webtrees\Http\RequestHandlers\WebmanifestJson;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Module\ModuleFooterInterface;
use Fisharebest\Webtrees\Module\ModuleGlobalInterface;
use Fisharebest\Webtrees\Module\ModuleThemeInterface;
use Fisharebest\Webtrees\Services\ModuleService;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\Validator;
use Fisharebest\Webtrees\View;
use Fisharebest\Webtrees\Webtrees;
use Psr\Http\Message\ServerRequestInterface;

/**
 * @var string                 $content
 * @var ServerRequestInterface $request
 * @var string                 $title
 * @var Tree                   $tree
 */

?>

<!DOCTYPE html>
<html dir="<?= I18N::locale()->direction() ?>" lang="<?= I18N::locale()->languageTag() ?>">
    <head>
        <meta charset="UTF-8">
        <meta name="csrf" content="<?= e(csrf_token()) ?>">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="<?= e($meta_robots ?? 'noindex') ?>">
        <meta name="generator" content="<?= e(Webtrees::NAME) ?> <?= e(Webtrees::VERSION) ?>">
        <meta name="description" content="<?= $meta_description ?? '' ?>">

        <title>
            <?= strip_tags($title) ?>
            <?php if ($tree !== null && $tree->getPreference('META_TITLE') !== '') : ?>
                – <?= e($tree->getPreference('META_TITLE')) ?>
            <?php endif ?>
        </title>

        <!--iOS-->
        <link rel="apple-touch-icon" sizes="180x180" href="<?= e(route(AppleTouchIconPng::class)) ?>">
        <!--Generic favicons-->
        <link rel="icon" sizes="32x32" href="<?= e(asset('favicon-32.png')) ?>">
        <link rel="icon" sizes="192x192" href="<?= e(asset('favicon-192.png')) ?>">
        <!--IE11/Edge-->
        <meta name="msapplication-config" content="<?= e(route(BrowserconfigXml::class)) ?>">

        <link rel="manifest" href="<?= e(route(WebmanifestJson::class)) ?>" crossorigin="use-credentials">

        <link rel="stylesheet" href="<?= e(asset('css/vendor.min.css')) ?>">
        <?php foreach (app(ModuleThemeInterface::class)->stylesheets() as $stylesheet) : ?>
            <link rel="stylesheet" href="<?= e($stylesheet) ?>">
        <?php endforeach ?>

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

        <?= app(ModuleService::class)->findByInterface(ModuleGlobalInterface::class)->map(static function (ModuleGlobalInterface $module): string {
            return $module->headContent();
        })->implode('') ?>
    </head>

    <body class="wt-global wt-theme-<?= e(app(ModuleThemeInterface::class)->name()) ?> wt-route-<?= e(basename(strtr(Validator::attributes($request)->route()->name ?? '/', ['\\' => '/']))) ?>">
        <header class="wt-header-wrapper d-print-none">
            <div class="container-lg wt-header-container">
                <div class="row wt-header-content">
                    <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>
                    <div class="col wt-site-logo"></div>

                    <?php if ($tree !== null) : ?>
                        <h1 class="col wt-site-title"><?= e($tree->title()) ?></h1>

                        <div class="col wt-header-search">
                            <form method="post" action="<?= e(route(SearchQuickAction::class, ['tree' => $tree->name()])) ?>" class="wt-header-search-form" role="search">
                                <div class="input-group">
                                    <label class="visually-hidden" for="quick-search"><?= I18N::translate('Search') ?></label>

                                    <input type="search" class="form-control wt-header-search-field" id="quick-search" name="query" size="15" placeholder="<?= I18N::translate('Search') ?>">

                                    <button type="submit" class="btn btn-primary wt-header-search-button" aria-label="<?= I18N::translate('Search') ?>">
                                        <?= view('icons/search') ?>
                                    </button>
                                </div>

                                <?= csrf_field() ?>
                            </form>
                        </div>
                    <?php endif ?>

                    <div class="col wt-secondary-navigation">
                        <ul class="nav wt-user-menu">
                            <?php foreach (app(ModuleThemeInterface::class)->userMenu($tree) as $menu) : ?>
                                <?= view('components/menu-item', ['menu' => $menu]) ?>
                            <?php endforeach ?>
                        </ul>
                    </div>

                    <?php if ($tree !== null) : ?>
                        <nav class="col wt-primary-navigation">
                            <ul class="nav wt-genealogy-menu">
                                <?php foreach (app(ModuleThemeInterface::class)->genealogyMenu($tree) as $menu) : ?>
                                    <?= view('components/menu-item', ['menu' => $menu]) ?>
                                <?php endforeach ?>
                            </ul>
                        </nav>
                    <?php endif ?>
                </div>
            </div>
        </header>

        <main id="content" class="wt-main-wrapper">
            <div class="container-lg wt-main-container">
                <div class="flash-messages">
                    <?php foreach (FlashMessages::getMessages() as $message) : ?>
                        <div class="alert alert-<?= e($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 ?>
                </div>

                <?= $content ?>
            </div>
        </main>

        <footer class="container-lg wt-footers d-print-none">
            <?= app(ModuleService::class)->findByInterface(ModuleFooterInterface::class)->map(static function (ModuleFooterInterface $module) use ($request): string {
                return $module->getFooter($request);
            })->implode('') ?>
        </footer>

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

        <script>
            // Trigger an event when we click on an (any) image
            $('body').on('click', 'a.gallery', function () {
                // Enable colorbox for images
                $("a[type^=image].gallery").colorbox({
                    // Don't scroll window with document
                    fixed: true,
                    width: "85%",
                    height: "85%",
                    current: "",
                    previous: '<i class="fa-solid fa-arrow-left wt-icon-flip-rtl" title="<?= I18N::translate('previous') ?>"></i>',
                    next: '<i class="fa-solid fa-arrow-right wt-icon-flip-rtl" title="<?= I18N::translate('next') ?>"></i>',
                    slideshowStart: '<i class="fa-solid fa-play" title="<?= I18N::translate('Play') ?>"></i>',
                    slideshowStop: '<i class="fa-solid fa-stop" title="<?= I18N::translate('Stop') ?>"></i>',
                    close: '<i class="fa-solid fa-times" title="<?= I18N::translate('close') ?>"></i>',
                    title: function () {
                        return this.dataset.title;
                    },
                    photo: true,
                    rel: "gallery", // Turn all images on the page into a slideshow
                    slideshow: true,
                    slideshowAuto: false,
                    slideshowSpeed: 4000,
                    // Add wheelzoom to the displayed image
                    onComplete: function () {
                        // Disable click on image triggering next image
                        // https://github.com/jackmoore/colorbox/issues/668
                        $(".cboxPhoto").unbind("click");
                        // Enable wheel/pinch zooming
                        $('.cboxPhoto').wrap("<pinch-zoom></pinch-zoom>");
                    }
                });
            });
        </script>

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

        <?= app(ModuleService::class)->findByInterface(ModuleGlobalInterface::class)->map(static function (ModuleGlobalInterface $module): string {
            return $module->bodyContent();
        })->implode('') ?>
    </body>
</html>
