<?php
/**
 * Hyvä Themes - https://hyva.io
 * Copyright © Hyvä Themes 2020-present. All rights reserved.
 * This product is licensed per Magento install
 * See https://hyva.io/license
 */

declare(strict_types=1);

use Hyva\Theme\Model\ViewModelRegistry;
use Hyva\Theme\ViewModel\CurrentProduct;
use Hyva\Theme\ViewModel\Modal;
use Magento\Catalog\Model\Product;
use Magento\Framework\Escaper;
use Magento\Framework\View\Element\Template;

// phpcs:disable Magento2.Templates.ThisInTemplate.FoundThis

/** @var Template $block */
/** @var Escaper $escaper */
/** @var ViewModelRegistry $viewModels */

/** @var CurrentProduct $currentProduct */
$currentProduct = $viewModels->require(CurrentProduct::class);

$product = $block->hasData('product')
    ? $block->getData('product')
    : $currentProduct->get();

if (!$product || !$product->getId()) {
    return;
}


$currentProduct = $viewModels->require(CurrentProduct::class);
$product = $block->hasData('product') ? $block->getData('product') : $currentProduct->get();

if (!$product || !$product->getId()) {
    return;
}

$rawVideoContent = (string) $product->getData('videotab');
$cleanContent = strip_tags(html_entity_decode($rawVideoContent));
$cleanContent = trim($cleanContent);

// Split on line breaks, trim each line
$videoUrls = array_filter(array_map('trim', preg_split('/\r\n|\r|\n/', $cleanContent)));
$videoCount = count($videoUrls);
?>

<?php if ($videoCount): ?>

    <?php if ($videoCount === 1): ?>
        <!-- One video: center -->
        <div class="flex justify-center w-full">


            <div class="w-full max-w-2xl">
                <iframe class="w-full aspect-video"
                        src="<?= $escaper->escapeUrl($videoUrls[0]); ?>"
                        frameborder="0"

                        allowfullscreen>
                </iframe>
            </div>
        </div>

    <?php elseif ($videoCount === 2): ?>
        <!-- Two videos: side by side -->
        <div class="grid grid-cols-1 sm:grid-cols-2 gap-4 w-full">
            <?php foreach ($videoUrls as $url): ?>
                <div class="w-full aspect-video">
                    <iframe class="w-full aspect-video"
                            src="<?= $escaper->escapeUrl($url); ?>"
                            frameborder="0"
                            allowfullscreen>
                    </iframe>
                </div>
            <?php endforeach; ?>
        </div>

    <?php else: ?>
        <!-- Three or more: 2 per row -->
        <div class="grid grid-cols-1 sm:grid-cols-2 gap-4 w-full">
            <?php foreach ($videoUrls as $url): ?>
                <div class="w-full">
                    <iframe class="w-full aspect-video"
                            src="<?= $escaper->escapeUrl($url); ?>"
                            frameborder="0"
                            allowfullscreen>
                    </iframe>
                </div>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>

<?php endif; ?>
