<?php

declare(strict_types=1);

use Fisharebest\Webtrees\Family;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Media;
use Fisharebest\Webtrees\MediaFile;
use Fisharebest\Webtrees\Source;
use Fisharebest\Webtrees\Tree;
use Illuminate\Support\Collection;

/**
 * @var int                    $block_id
 * @var int                    $delay
 * @var Collection<Family>     $linked_families
 * @var Collection<Individual> $linked_individuals
 * @var Collection<Source>     $linked_sources
 * @var Media                  $media
 * @var MediaFile              $media_file
 * @var bool                   $show_controls
 * @var bool                   $start_automatically
 * @var Tree                   $tree
 */

?>

<div class="wt-slide-show-container">
    <?php if ($show_controls) : ?>
        <div class="wt-slide-show-controls text-center">
            <a href="#" title="<?= I18N::translate('Play') ?>" <?= $start_automatically ? 'hidden' : '' ?>>
                <?= view('icons/media-play') ?>
                <span class="visually-hidden"><?= I18N::translate('Play') ?></span>
            </a>
            <a href="#" title="<?= I18N::translate('Stop') ?>" <?= $start_automatically ? '' : 'hidden' ?>>
                <?= view('icons/media-stop') ?>
                <span class="visually-hidden"><?= I18N::translate('Stop') ?></span>
            </a>
            <a href="#" title="<?= I18N::translate('Next image') ?>">
                <?= view('icons/media-next') ?>
                <span class="visually-hidden"><?= I18N::translate('Next image') ?></span>
            </a>
        </div>
    <?php endif ?>

    <figure class="wt-slide-show-figure text-center">
        <?= $media_file->displayImage(200, 200, 'contain', ['class' => 'slide-show-image img-fluid']) ?>
        <figcaption class="wt-slide-show-figcaption">
            <a href="<?= e($media->url()) ?>">
                <b><?= $media->fullName() ?></b>
            </a>
        </figcaption>
    </figure>

    <p class="wt-slide-show-notes text-center">
        <?php foreach ($media->facts(['NOTE']) as $fact) : ?>
            <?= view('fact-gedcom-fields', ['gedcom' => $fact->gedcom(), 'parent' => $media->tag(), 'tree' => $tree]) ?>
        <?php endforeach ?>
    </p>

    <ul class="fa-ul wt-slide-show-links">
        <?php foreach ($linked_individuals as $individual) : ?>
            <li>
                <span class="fa-li" title="<?= I18N::translate('Individual') ?>"><?= view('icons/individual') ?></span>
                <a href="<?= e($individual->url()) ?>" class="wt-slide-show-link">
                    <?= $individual->fullName() ?>
                </a>
            </li>
        <?php endforeach ?>

        <?php foreach ($linked_families as $family) : ?>
            <li>
                <span class="fa-li" title="<?= I18N::translate('Family') ?>"><?= view('icons/family') ?></span>
                <a href="<?= e($family->url()) ?>" class="wt-slide-show-link">
                    <?= $family->fullName() ?>
                </a>
            </li>
        <?php endforeach ?>

        <?php foreach ($linked_sources as $source) : ?>
            <li>
                <span class="fa-li" title="<?= I18N::translate('Source') ?>"><?= view('icons/source') ?></span>
                <a href="<?= e($source->url()) ?>" class="wt-slide-show-link">
                    <?= $source->fullName() ?>
                </a>
            </li>
        <?php endforeach ?>
    </ul>
</div>

<script>
  (function () {
    let block = document.getElementById('block-<?= $block_id ?>');

    let play = <?= json_encode($start_automatically, JSON_THROW_ON_ERROR) ?>;

    function slideShowReload () {
      clearTimeout(timeout);

      if (document.hidden) {
        // No point loading images when nobody is looking.
        timeout = setTimeout(slideShowReload, 1000);
      } else {
        $(block.parentNode).load($(block).parent().data('wtAjaxUrl') + '&start=' + (play ? '1' : '0'));
      }
    }

    let timeout = null;

    if (play) {
      timeout = setTimeout(slideShowReload, <?= json_encode($delay * 1000, JSON_THROW_ON_ERROR) ?>);
    }

    block.querySelector('.wt-icon-media-play').addEventListener('click', (event) => {
      event.preventDefault();
      block.querySelector('.wt-icon-media-play').parentNode.hidden = true;
      block.querySelector('.wt-icon-media-stop').parentNode.hidden = false;
      play = true;
      slideShowReload();
    });

    block.querySelector('.wt-icon-media-stop').addEventListener('click', (event) => {
      event.preventDefault();
      block.querySelector('.wt-icon-media-stop').parentNode.hidden = true;
      block.querySelector('.wt-icon-media-play').parentNode.hidden = false;
      play = false;
      clearTimeout(timeout);
    });

    block.querySelector('.wt-icon-media-next').addEventListener('click', (event) => {
      event.preventDefault();
      slideShowReload();
    });
  })();
</script>
