<?php
declare(strict_types=1);

if (empty($isCalendarTvView ?? null)) {
    return;
}

$collageDir = dirname(__DIR__) . '/assets/images/calendar-tv-collage';
$files = glob($collageDir . '/collage-*.png') ?: [];
if ($files === []) {
    $files = glob($collageDir . '/collage-*.{jpg,jpeg,webp}', GLOB_BRACE) ?: [];
}
natcasesort($files);
$files = array_values($files);
if ($files === []) {
    return;
}

$mosaic = count($files) === 10;
$gridClass = $mosaic ? 'calendar-tv-collage-grid calendar-tv-collage-grid--mosaic' : 'calendar-tv-collage-grid calendar-tv-collage-grid--even';
$cols = 5;
$rows = (int) max(1, ceil(count($files) / $cols));
?>
<div class="calendar-tv-collage-bg" aria-hidden="true">
    <div class="<?= htmlspecialchars($gridClass, ENT_QUOTES, 'UTF-8') ?>"<?= $mosaic ? '' : ' style="grid-template-columns: repeat(' . (int) $cols . ', 1fr); grid-template-rows: repeat(' . (int) $rows . ', 1fr);"' ?>>
        <?php foreach ($files as $absPath): ?>
            <?php
            $base = basename((string) $absPath);
            $url = 'assets/images/calendar-tv-collage/' . $base;
            ?>
        <div class="calendar-tv-collage-tile" style="background-image: url('<?= htmlspecialchars($url, ENT_QUOTES, 'UTF-8') ?>')"></div>
        <?php endforeach; ?>
    </div>
    <div class="calendar-tv-collage-scrim"></div>
</div>
