<?php include 'includes/header.php'; ?>

<?php
date_default_timezone_set('America/New_York'); // EST

$scheduleFile = 'schedule.txt';
$currentDay = date('l'); // e.g. Monday
$currentHour = date('H') . ':00'; // e.g. 14:00

$schedule = [];
if (file_exists($scheduleFile)) {
  $lines = explode("\n", file_get_contents($scheduleFile));
  foreach ($lines as $line) {
    $parts = explode('|', trim($line));
    if (count($parts) === 3) {
      [$day, $hour, $dj] = $parts;
      $schedule[$day][$hour] = $dj;
    }
  }
}

$currentDJ = $schedule[$currentDay][$currentHour] ?? 'auto dj';

$nextHourIndex = (int)substr($currentHour, 0, 2) + 1;
$nextHour = str_pad($nextHourIndex % 24, 2, '0', STR_PAD_LEFT) . ':00';
$nextDJ = $schedule[$currentDay][$nextHour] ?? 'auto dj';

$comingAfter = [];
for ($i = 2; $i <= 4; $i++) {
  $hour = str_pad(($nextHourIndex + $i - 1) % 24, 2, '0', STR_PAD_LEFT) . ':00';
  $comingAfter[] = $schedule[$currentDay][$hour] ?? 'auto dj';
}
?>

<div class="container content-box mt-5">

  <!-- Hero Section -->
  <div class="text-center mb-5">
    <h1>Welcome to Home Time Radio</h1>

    <div class="my-3">
      <img src="assets/images/logo.jpg" alt="Home Time Radio Card" class="img-fluid" style="max-width: 700px">
    </div>

    <p class="subtext">Your home for classic rewind and fresh hits</p>

    <a href="http://cr3.broadcasturself.com:65000/stream" class="btn btn-primary mt-3">▶ Play Now</a>

    <div class="mt-4 d-flex justify-content-center flex-wrap gap-3">
      <a href="https://mytuner-radio.com/radio/home-time-radio-507690" target="_blank" class="btn btn-accent">🎧 Listen on MyTuner</a>
      <a href="https://onlineradiobox.com/us/hometime" target="_blank" class="btn btn-accent">📻 Listen on RadioBox</a>
    </div>
  </div>

  <!-- DJ Schedule -->
  <div class="row text-center mb-4">
    <div class="col-md-4">
      <div class="dj-box">
        <h3>Now On Air</h3>
        <p><strong><?php echo $currentDJ; ?></strong> (<?php echo $currentHour; ?> EST)</p>
      </div>
    </div>
    <div class="col-md-4">
      <div class="dj-box">
        <h3>Next Up</h3>
        <p><strong><?php echo $nextDJ; ?></strong> (<?php echo $nextHour; ?> EST)</p>
      </div>
    </div>
    <div class="col-md-4">
<div class="dj-box">
  <h3>Coming After</h3>
  <p><strong><?php echo $comingAfter[0]; ?></strong> (<?php echo $afterHour; ?> EST)</p>
</div>

    </div>
  </div>

<!-- What's Playing Now -->
<div class="text-center mb-5">
  <div id="liveTrackInfo">
    🎶 What’s Playing Now<br>
    <?php
    date_default_timezone_set('America/New_York');
    header("refresh: 60;");

    $server_ip = 'cr3.broadcasturself.com';
    $server_port = '65001';
    $admin_user = 'HomeTimeRadio';
    $admin_password = 'KFnph5qQuBPXaT3VJ4jp';

    $mounts = ['/stream', '/autodj', '/autodj2'];
    $stats_url = "http://$admin_user:$admin_password@$server_ip:$server_port/admin/stats.xml";

    $xml = @file_get_contents($stats_url);
    if (!$xml) {
      echo "<span>Unable to fetch stream info.</span>";
    } else {
      $found = false;
      foreach ($mounts as $mount) {
        $needle = '<source mount="' . $mount . '">';
        if (str_contains($xml, $needle)) {
          $section = strstr($xml, $needle);
          $titleBlock = strstr($section, '<title>');
          if ($titleBlock) {
            $start = strpos($titleBlock, '<title>') + 7;
            $end = strpos($titleBlock, '</title>');
            $track = trim(substr($titleBlock, $start, $end - $start));
            if ($track !== '') {
              echo "<span>$track</span>";
              $found = true;
              break;
            }
          }
        }
      }
      if (!$found) {
        echo "<span>No track info available.</span>";
      }
    }
    ?>
  </div>
</div>


  <!-- Classic Rewind + Discord -->
  <div class="row mb-5">
    <div class="col-md-6">
      <h4>THE HOME OF CLASSIC REWIND</h4>
      <img src="assets/images/bill.png" alt="Vinyl Records" class="img-fluid rounded">
    </div>
    <div class="col-md-6">
      <iframe src="https://discord.com/widget?id=1190186238158852106&theme=dark"
        width="100%" height="670"
        allowtransparency="true" frameborder="0"
        sandbox="allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts"
        title="Discord Community Widget"></iframe>
    </div>
  </div>

  <!-- Albums -->
  <div class="text-center mb-4">
    <h4>Albums</h4>
    <p class="subtext">From classic to modern — we’ve got it all</p>
  </div>
  <div class="row mb-5">
    <div class="col-md-6">
      <a href="assets/images/album1.jpg" data-fancybox="gallery">
        <img src="assets/images/album1.jpg" class="img-fluid rounded" alt="Album 1">
      </a>
    </div>
    <div class="col-md-6">
      <a href="assets/images/album2.jpg" data-fancybox="gallery">
        <img src="assets/images/album2.jpg" class="img-fluid rounded" alt="Album 2">
      </a>
    </div>
  </div>

  <!-- Comment Box -->
  <div class="mb-5">
    <h4>Leave a Comment</h4>
    <form method="POST" action="api/save-comment.php">
      <textarea name="comment" class="form-control mb-2" placeholder="Share your thoughts..." required></textarea>
      <button type="submit" class="btn btn-dark">Post Comment</button>
    </form>
  </div>

  <!-- Recent Comments -->
  <div class="bg-light p-3 rounded mb-5">
    <h5>Recent Comments</h5>
    <?php
    $commentsFile = 'comments.txt';
    if (file_exists($commentsFile)) {
      $allComments = explode("---\n", file_get_contents($commentsFile));
      $recent = array_slice(array_reverse($allComments), 0, 3);
      foreach ($recent as $entry) {
        $lines = explode("\n", trim($entry));
        $reply = '';
        $content = [];

        foreach ($lines as $line) {
          if (str_starts_with($line, 'Reply:')) {
            $reply = substr($line, 7);
          } else {
            $content[] = $line;
          }
        }

        echo "<div class='mb-3 border-bottom pb-2'>";
        echo "<p>" . nl2br(htmlspecialchars(implode("\n", $content))) . "</p>";
        if ($reply) {
          echo "<div class='bg-white border rounded p-2 mt-2'><strong>Reply:</strong> " . htmlspecialchars($reply) . "</div>";
        }
        echo "</div>";
      }
    } else {
      echo "<p>No comments yet.</p>";
    }
    ?>
  </div>

  <!-- Visitor Counter -->
  <div class="text-center mb-5">
    <p class="visitor-count">👀 Visitors so far: under construction </p>
  </div>

</div>

<?php include 'includes/footer.php'; ?>
