<?php

use Kirby\Cms\Html;

/** @var \Kirby\Cms\Block $block */

if (
    $block->location() == 'kirby' &&
    $video = $block->video()->toFile()
) {
    // recheck, since the file might have own information
    $caption = $block->caption()->or($video->caption());
    $alt = $block->alt()->or($video->alt());
    $desc = $block->description()->or($video->description());

    $url   = $video->url();
    $attrs = array_filter([
        'autoplay'    => $block->autoplay()->toBool(),
        'controls'    => $block->controls()->toBool(),
        'loop'        => $block->loop()->toBool(),
        'muted'       => $block->muted()->toBool() || $block->autoplay()->toBool(),
        'playsinline' => $block->autoplay()->toBool(),
        'poster'      => $video->poster()->toFile()?->url(),
        'preload'     => $block->preload()->value(),
        'aria-describedby' => "video-desc-" . $block->id(),
        'title'         =>  $alt,
        'id'          => $block->id(),
    ]);
} else {

    $url = $block->url();
    $desc = $block->description();
    $caption = $block->caption();
    $alt = $block->alt();

    $attrs = array_filter([
        /* 'autoplay'    => $block->autoplay()->toBool(),
        'controls'    => $block->controls()->toBool(),
        'loop'        => $block->loop()->toBool(),
        'muted'       => $block->muted()->toBool() || $block->autoplay()->toBool(),
        'playsinline' => $block->autoplay()->toBool(),
        'poster'      => $block->poster()->toFile()?->url(),
        'preload'     => $block->preload()->value(), */

        'frameborder' => '0', // not working
        'aria-describedby' => "video-desc-" . $block->id(),
        'allowfullscreen' => 'true',
        'allow' => 'fullscreen',
        'title'         =>  $alt,
        'id'          => $block->id(),
    ]);
}

?>
<?php if (
    $block->location() == 'kirby' &&
    $video = $block->video()->toFile()
): ?>
    <figure>
        <video <?= attr($attrs ?? []) ?>>
            <source src="<?= $url ?>" type="video/mp4" />

            <!-- for blind people -->
            <?php if ($vtt_description = $video->content()->get('vtt_description_' . $kirby->language())): ?>
                <track src="<?= $vtt_description->toFile() ?>" kind="descriptions" srclang="<?= $kirby->language() ?>" label="<?= $video->content()->get('vtt_description_label_' . $kirby->language()) ?>">
            <?php endif ?>

            <!-- for deaf people -->
            <?php if ($vtt_captions_de = $video->vtt_captions_de()->toFile()): ?>
                <track src="<?= $vtt_captions_de->url() ?>" kind="captions" srclang="de" label="<?= $video->vtt_captions_label_de() ?>">
            <?php endif ?>
            <?php if ($vtt_captions_en = $video->vtt_captions_en()->toFile()): ?>
                <track src="<?= $vtt_captions_en->url() ?>" kind="captions" srclang="en" label="<?= $video->vtt_captions_label_en() ?>">
            <?php endif ?>

            <?= t('video.noSupport') ?>
        </video>
        <?php if ($caption): ?>
            <figcaption><?= $caption ?></figcaption>
        <?php endif ?>
    </figure>
<?php else: ?>

    <?php
    $cookie = $_COOKIE['cc_cookie'] ?? null;
    if ($cookie) {
        $cookieData = json_decode($_COOKIE['cc_cookie'], true);
        $externalVideos = in_array('experience', $cookieData['categories']) ?? null;
    } else {
        $externalVideos = false;
    }

    if ($externalVideos): ?>
        <?php if ($video = Html::video($url, [], $attrs ?? [])): ?>
            <figure class="iframe-container">
                <?= $video ?>
                <?php if ($caption): ?>
                    <figcaption><?= $caption ?></figcaption>
                <?php endif ?>
            </figure>
        <?php endif ?>
    <?php else: ?>
        <figure class="crop ratioed">
            <div class="video-placeholder" data-src="<?= $url ?>">
                <p><?= snippet('t_labels', ['var' => 'cookies.activateCookies']) ?></p>
            </div>
            <?php if ($caption): ?>
                <figcaption><?= $caption ?></figcaption>
            <?php endif ?>
        </figure>
    <?php endif ?>

<?php endif ?>

<?php if ($desc->isNotEmpty()): ?>
    <p id="video-desc-<?= $block->id() ?>" class="visually-hidden">
        <?= $desc ?>
    </p>
<?php endif ?>

<?php if ($vtt_description ?? null): ?>
    <p id="descAudioOutput__<?= $block->id() ?>" class="visually-hidden" aria-live="polite"></p>
<?php endif ?>