<?php
/**
 * Section Hero - Affichage conditionnel (Direct / Playlist / Vidéo / Rien)
 *
 * Ce fichier gère l'affichage de la section hero en fonction de la configuration:
 * - HERO_TYPE = 'live': Affiche le direct PeerTube
 * - HERO_TYPE = 'video': Affiche une vidéo PeerTube unique
 * - HERO_TYPE = 'playlist': Affiche une playlist (PeerTube/Funkwhale/Castopod)
 * - HERO_TYPE = 'none': N'affiche rien
 */

// Type de hero défini dans config
$heroType = defined('HERO_TYPE') ? HERO_TYPE : 'live';

// Si le type est 'none', ne rien afficher
if ($heroType === 'none') {
    return;
}
?>

<section class="hero" aria-labelledby="hero-section-title">
    <h2 id="hero-section-title" class="sr-only">
        <?php
        if ($heroType === 'live') {
            echo 'Diffusion en direct';
        } elseif ($heroType === 'video') {
            echo 'Vidéo';
        } else {
            echo 'Playlist';
        }
        ?>
    </h2>

    <?php if ($heroType === 'live'): ?>
        <!-- Mode DIRECT -->
        <?php
        // Vérifier s'il y a un direct en cours
        $liveStream = getLiveStream();

        if ($liveStream) {
            // Afficher le direct en cours
            ?>
            <div class="live-badge">
                <i class="fas fa-circle"></i> DIRECT
            </div>
            <div class="hero-video-container">
                <iframe
                    src="<?php echo PEERTUBE_URL; ?>/videos/embed/<?php echo $liveStream['id']; ?>?autoplay=1&muted=1"
                    frameborder="0"
                    allowfullscreen="allowfullscreen"
                    allow="autoplay; fullscreen"
                    title="Diffusion en direct: <?php echo htmlspecialchars($liveStream['title']); ?>"
                    aria-describedby="live-description">
                </iframe>
                <div id="live-description" class="sr-only">
                    Lecteur vidéo pour la diffusion en direct de <?php echo htmlspecialchars($liveStream['channel']); ?>
                </div>
            </div>
            <div class="hero-video-info">
                <h2><?php echo htmlspecialchars($liveStream['title']); ?></h2>
                <div class="hero-channel-info">
                    <?php if (strpos($liveStream['channelAvatar'], 'default-avatar.png') !== false || empty($liveStream['channelAvatar'])): ?>
                        <div class="channel-avatar-placeholder" role="img" aria-label="Avatar par défaut">
                            <i class="fas fa-user-circle" aria-hidden="true"></i>
                        </div>
                    <?php else: ?>
                        <img src="<?php echo $liveStream['channelAvatar']; ?>" alt="Avatar de la chaîne <?php echo htmlspecialchars($liveStream['channel']); ?>" class="channel-avatar">
                    <?php endif; ?>
                    <span class="channel-name"><?php echo htmlspecialchars($liveStream['channel']); ?></span>
                </div>
            </div>
        <?php
        } else {
            // Aucun direct en cours - vérifier s'il y a une annonce configurée
            $showNextLiveAnnouncement = defined('NEXT_LIVE_ENABLED') && NEXT_LIVE_ENABLED === true;

            if ($showNextLiveAnnouncement) {
                // Afficher l'annonce du prochain live
                // Définir l'image de fond si disponible
                $bgImageStyle = '';
                if (!empty(NEXT_LIVE_IMAGE) && file_exists(NEXT_LIVE_IMAGE)) {
                    $bgImageStyle = 'background-image: url(\'' . htmlspecialchars(NEXT_LIVE_IMAGE) . '\');';
                }
                ?>
                <div class="hero-next-live" style="<?php echo $bgImageStyle; ?>" nonce="<?php echo getCspNonce(); ?>">
                    <?php if (!empty(NEXT_LIVE_IMAGE) && file_exists(NEXT_LIVE_IMAGE)): ?>
                        <div class="hero-next-live-image-container">
                            <img src="<?php echo htmlspecialchars(NEXT_LIVE_IMAGE); ?>"
                                 alt="<?php echo htmlspecialchars(NEXT_LIVE_TITLE); ?>"
                                 class="hero-next-live-image">
                        </div>
                    <?php endif; ?>
                    <div class="hero-next-live-content">
                        <i class="fas fa-calendar-alt"></i>
                        <?php
                        if (!empty(NEXT_LIVE_DATE)) {
                            $liveDate = new DateTime(NEXT_LIVE_DATE, new DateTimeZone(DEFAULT_TIMEZONE));
                            $dayFormatter = new IntlDateFormatter(
                                'fr_FR',
                                IntlDateFormatter::FULL,
                                IntlDateFormatter::NONE,
                                DEFAULT_TIMEZONE,
                                IntlDateFormatter::GREGORIAN,
                                'EEEE d MMMM'
                            );
                            $formattedDay = $dayFormatter->format($liveDate);
                            $formattedDay = ucfirst($formattedDay);
                            $dynamicTitle = NEXT_LIVE_TITLE . ' - ' . $formattedDay;
                        } else {
                            $dynamicTitle = NEXT_LIVE_TITLE;
                        }
                        ?>
                        <h2><?php echo htmlspecialchars($dynamicTitle); ?></h2>
                        <?php
                        if (!empty(NEXT_LIVE_DATE)) {
                            $liveHour = $liveDate->format('H\hi');
                            $dynamicDescription = 'Rejoignez-nous à ' . $liveHour . '. ' . NEXT_LIVE_DESCRIPTION;
                        } else {
                            $dynamicDescription = NEXT_LIVE_DESCRIPTION;
                        }
                        ?>
                        <p><?php echo nl2br(htmlspecialchars($dynamicDescription)); ?></p>
                        <?php if (!empty(NEXT_LIVE_DATE)): ?>
                            <div class="hero-next-live-datetime">
                                <p class="hero-next-live-date">
                                    <i class="fas fa-clock"></i>
                                    <?php
                                    $formatter = new IntlDateFormatter(
                                        'fr_FR',
                                        IntlDateFormatter::FULL,
                                        IntlDateFormatter::SHORT,
                                        DEFAULT_TIMEZONE
                                    );
                                    echo $formatter->format($liveDate);

                                    $offset = $liveDate->format('P');
                                    echo ' <span class="utc-offset">(UTC' . $offset . ')</span>';
                                    ?>
                                </p>

                                <!-- Autres fuseaux horaires -->
                                <div class="hero-next-live-timezones">
                                    <?php
                                    // Ordre croissant : du plus en retard au plus en avance
                                    $timezones = [
                                        'Ma\'ohi Nui' => 'Pacific/Tahiti',
                                        'Martinique / Guadeloupe' => 'America/Martinique',
                                        'Guyane' => 'America/Cayenne',
                                        'France' => 'Europe/Paris',
                                        'Kanaky' => 'Pacific/Noumea'
                                    ];

                                    foreach($timezones as $name => $timezone):
                                        $liveDateLocal = clone $liveDate;
                                        $liveDateLocal->setTimezone(new DateTimeZone($timezone));
                                        $dayDiff = $liveDateLocal->format('j') - $liveDate->format('j');

                                        $dayIndicator = '';
                                        if ($dayDiff > 0) {
                                            $dayIndicator = ' <span class="day-shift">+1j</span>';
                                        } elseif ($dayDiff < 0) {
                                            $dayIndicator = ' <span class="day-shift">-1j</span>';
                                        }
                                    ?>
                                        <span class="hero-timezone-item">
                                            <strong><?php echo $name; ?> :</strong> <?php echo $liveDateLocal->format('H\hi'); ?><?php echo $dayIndicator; ?>
                                        </span>
                                    <?php endforeach; ?>
                                </div>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            <?php
            } else {
                ?>
                <div class="hero-no-live">
                    <i class="fas fa-tv"></i>
                    <h2>Aucun direct en cours</h2>
                    <p>Revenez plus tard pour découvrir nos prochaines diffusions en direct.</p>
                </div>
            <?php
            }
        }
        ?>

    <?php elseif ($heroType === 'playlist'): ?>
        <!-- Mode PLAYLIST -->
        <?php
        $platform = defined('PLAYLIST_PLATFORM') ? PLAYLIST_PLATFORM : 'peertube';
        $instanceUrl = defined('PLAYLIST_INSTANCE_URL') ? PLAYLIST_INSTANCE_URL : '';
        $playlistId = defined('PLAYLIST_ID') ? PLAYLIST_ID : '';
        $playlistTitle = defined('PLAYLIST_TITLE') ? PLAYLIST_TITLE : 'Playlist';
        $playlistDescription = defined('PLAYLIST_DESCRIPTION') ? PLAYLIST_DESCRIPTION : '';

        if (!empty($instanceUrl) && !empty($playlistId)):
        ?>
            <div class="hero-playlist">
                <div class="hero-playlist-header">
                    <i class="fas fa-list-music"></i>
                    <div class="hero-playlist-info">
                        <h2><?php echo htmlspecialchars($playlistTitle); ?></h2>
                        <?php if (!empty($playlistDescription)): ?>
                            <p><?php echo htmlspecialchars($playlistDescription); ?></p>
                        <?php endif; ?>
                    </div>
                </div>

                <div class="hero-playlist-container">
                    <?php if ($platform === 'peertube'): ?>
                        <!-- Embed PeerTube Playlist -->
                        <iframe
                            src="<?php echo rtrim($instanceUrl, '/'); ?>/video-playlists/embed/<?php echo $playlistId; ?>"
                            frameborder="0"
                            allowfullscreen="allowfullscreen"
                            sandbox="allow-same-origin allow-scripts allow-popups allow-forms"
                            title="<?php echo htmlspecialchars($playlistTitle); ?>">
                        </iframe>

                    <?php elseif ($platform === 'funkwhale'): ?>
                        <!-- Embed Funkwhale Playlist -->
                        <iframe
                            src="<?php echo rtrim($instanceUrl, '/'); ?>/front/embed.html?&type=playlist&id=<?php echo $playlistId; ?>"
                            frameborder="0"
                            allowfullscreen="allowfullscreen"
                            title="<?php echo htmlspecialchars($playlistTitle); ?>">
                        </iframe>

                    <?php elseif ($platform === 'castopod'): ?>
                        <!-- Embed Castopod Podcast -->
                        <iframe
                            src="<?php echo rtrim($instanceUrl, '/'); ?>/<?php echo $playlistId; ?>/embed/dark"
                            frameborder="0"
                            allowfullscreen="allowfullscreen"
                            title="<?php echo htmlspecialchars($playlistTitle); ?>">
                        </iframe>

                    <?php endif; ?>
                </div>

                <div class="hero-playlist-footer">
                    <a href="<?php
                        if ($platform === 'peertube') {
                            echo rtrim($instanceUrl, '/') . '/w/p/' . $playlistId;
                        } elseif ($platform === 'funkwhale') {
                            echo rtrim($instanceUrl, '/') . '/library/playlists/' . $playlistId;
                        } elseif ($platform === 'castopod') {
                            echo rtrim($instanceUrl, '/') . '/' . $playlistId;
                        }
                    ?>" target="_blank" rel="noopener noreferrer" class="hero-playlist-link">
                        <i class="fas fa-external-link-alt"></i>
                        Voir sur <?php echo ucfirst($platform); ?>
                    </a>
                </div>
            </div>
        <?php else: ?>
            <div class="hero-no-live">
                <i class="fas fa-list-music"></i>
                <h2>Playlist non configurée</h2>
                <p>Configurez PLAYLIST_INSTANCE_URL et PLAYLIST_ID dans votre fichier de configuration.</p>
            </div>
        <?php endif; ?>

    <?php elseif ($heroType === 'video'): ?>
        <!-- Mode VIDÉO -->
        <?php
        $videoId = defined('HERO_VIDEO_ID') ? HERO_VIDEO_ID : '';
        $videoTitle = defined('HERO_VIDEO_TITLE') ? HERO_VIDEO_TITLE : 'Vidéo de présentation';

        if (!empty($videoId)):
        ?>
            <div class="hero-video-container">
                <iframe
                    src="<?php echo PEERTUBE_URL; ?>/videos/embed/<?php echo $videoId; ?>"
                    frameborder="0"
                    allowfullscreen="allowfullscreen"
                    sandbox="allow-same-origin allow-scripts allow-popups"
                    allow="autoplay; fullscreen"
                    title="<?php echo htmlspecialchars($videoTitle); ?>">
                </iframe>
            </div>
        <?php else: ?>
            <div class="hero-no-live">
                <i class="fas fa-video"></i>
                <h2>Vidéo non configurée</h2>
                <p>Configurez HERO_VIDEO_ID dans votre fichier de configuration.</p>
            </div>
        <?php endif; ?>

    <?php endif; ?>
</section>
