<?php
session_start();
require_once __DIR__ . '/includes/stream_lib.php';
require_once __DIR__ . '/admin/includes/admin_lib.php';
$current = smurf_top50_touch(__DIR__ . '/data/top50.json', __DIR__ . '/data/top50_state.json');
$cfg = smurf_get_stream_config();
$settings = smurf_load_settings(__DIR__ . '/data/settings.json');
$rawUrl = $cfg['listen_url'];
$workerUrl = trim((string)($settings['player_worker_url'] ?? ''));
$useWorker = !empty($settings['player_use_worker']) && $workerUrl !== '';
$audioUrl = $useWorker ? $workerUrl : '';
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Smurfettes Pop Out Player</title>
  <link rel="stylesheet" href="/assets/css/main.css">
</head>
<body class="page-player-popout">
  <main class="content-area">
    <div class="content-wrapper">
      <section class="content-card center-text popout-player-card">
        <h1 class="hero-title">🎧 Pop Out Player</h1>
        <p class="hero-tagline">Take Smurfettes with you in a dedicated player window.</p>
        <?php if ($useWorker): ?>
          <audio controls preload="none" style="width:min(100%,560px);margin:18px auto;display:block;">
            <source src="<?= htmlspecialchars($audioUrl) ?>" type="audio/mpeg">
            Your browser does not support the audio element.
          </audio>
          <p style="opacity:.85;">Embedded player using your Cloudflare Worker HTTPS stream.</p>
        <?php else: ?>
          <div class="content-card" style="margin:18px auto;max-width:560px;text-align:left;">
            <h2 style="margin-top:0;">Player setup needed</h2>
            <p>Your site is HTTPS but your stream is HTTP, so browsers block the embedded player as mixed content.</p>
            <p>Use the external link below now, then add your Cloudflare Worker URL in <strong>Admin → Pop Out Player</strong> to enable the embedded player.</p>
          </div>
        <?php endif; ?>
        <div class="content-card" style="margin-top:18px;">
          <h2>Now Playing</h2>
          <p style="font-size:1.1rem;"><strong><?= htmlspecialchars($current ?: 'Waiting for metadata…') ?></strong></p>
          <p style="display:flex;gap:10px;justify-content:center;flex-wrap:wrap;">
            <a class="button play-btn" href="<?= htmlspecialchars($rawUrl) ?>" target="_blank" rel="noopener">Open External Player</a>
            <?php if (!$useWorker): ?><a class="button" href="/admin/player.php">Set Worker URL</a><?php endif; ?>
          </p>
        </div>
      </section>
    </div>
  </main>
</body>
</html>
