<?php
if (!isset($public_file)) { exit('No direct access.'); }

$liveTitle    = !empty($advancenews['LiveTV_Title']) ? $advancenews['LiveTV_Title'] : 'Live TV';
$liveStatus   = isset($advancenews['LiveTV_Status']) ? $advancenews['LiveTV_Status'] : 'deactive';
$liveSource   = isset($advancenews['LiveTV_Source']) ? $advancenews['LiveTV_Source'] : 'auto';
$liveURL      = isset($advancenews['LiveTV_URL'])    ? trim($advancenews['LiveTV_URL']) : '';
$noticeOn     = isset($advancenews['LiveTV_Notice_Status']) ? $advancenews['LiveTV_Notice_Status'] : 'off';
$noticeTitle  = isset($advancenews['LiveTV_Notice_Title']) ? $advancenews['LiveTV_Notice_Title'] : '';
$noticeText   = isset($advancenews['LiveTV_Notice_Text'])  ? $advancenews['LiveTV_Notice_Text']  : '';
$noticeSpeed  = isset($advancenews['LiveTV_Notice_Speed']) ? $advancenews['LiveTV_Notice_Speed'] : '40s';

// ========== আরভি লোগো (RV Logo) ==========
$rv_logo_url = 'https://sky-times.net/uploads/Untitled design_20260622_231444_0000.png';

// Auto detect source from URL
if ($liveSource === 'auto' && $liveURL !== '') {
    if (preg_match('~youtube\.com|youtu\.be~i', $liveURL))      { $liveSource = 'youtube'; }
    elseif (preg_match('~facebook\.com|fb\.watch~i', $liveURL)) { $liveSource = 'facebook'; }
    elseif (preg_match('~\.m3u8(\?|$)~i', $liveURL))            { $liveSource = 'hls'; }
    else                                                         { $liveSource = 'iframe'; }
}

// Build embed URL - autoplay with sound (mute=0)
$embedURL = '';
if ($liveSource === 'youtube') {
    $vid = '';
    if (preg_match('~(?:v=|youtu\.be/|embed/|live/)([A-Za-z0-9_\-]{6,})~', $liveURL, $m)) {
        $vid = $m[1];
    }
    if ($vid !== '') {
        $embedURL = 'https://www.youtube.com/embed/' . $vid
                  . '?autoplay=1&mute=0&controls=1&rel=0&modestbranding=1&showinfo=0&iv_load_policy=3&playsinline=1';
    }
} elseif ($liveSource === 'facebook') {
    $embedURL = 'https://www.facebook.com/plugins/video.php?href=' . urlencode($liveURL)
              . '&show_text=false&autoplay=true&mute=0&controls=true';
} elseif ($liveSource === 'iframe') {
    $embedURL = $liveURL;
}

$tt_page_info = array(
  'Page_Info' => array(
    'Title'       => $liveTitle,
    'Description' => 'Watch ' . $liveTitle . ' streaming live online.',
    'Keyword'     => 'live tv, '. $liveTitle .', live stream',
  ),
  'Social_og' => array(
    'Image' => '',
    'Type'  => 'video.other',
    'URL'   => 'livetv/',
  ),
);
require_once('./header.php');
?>

<style>
  .tt-livetv-wrap{
    max-width:1100px;margin:20px auto;padding:0 10px;
    font-family:'SolaimanLipi','Kalpurush',sans-serif;
  }

  /* ভিডিও কন্টেইনার - রিলেটিভ পজিশন (লোগো বসানোর জন্য) */
  .tt-livetv-box{
    position:relative;
    width:100%;
    background:#000;
    overflow:hidden;
    line-height:0;
    font-size:0;
  }
  .tt-livetv-box iframe,
  .tt-livetv-box video{
    display:block;
    width:100%;
    height:auto;
    aspect-ratio:16/9;
    border:0;
    background:#000;
    margin:0;
    padding:0;
    vertical-align:top;
  }
  .tt-livetv-off{
    width:100%;
    aspect-ratio:16/9;
    display:flex;
    align-items:center;
    justify-content:center;
    color:#bbb;
    font-size:18px;
    letter-spacing:1px;
    background:#000;
  }

  /* ========== আরভি লোগো (উপরের ডান পাশের কোনায় - একটু বেশি ডানে এবং উপরে) ========== */
  .rv-logo {
    position: absolute;
    top: 8px;        /* আগে 15px ছিল, এখন আরেকটু উপরে (8px) */
    right: 8px;      /* আগে 15px ছিল, এখন আরেকটু ডানে (8px) */
    width: 80px;     /* প্রস্থ - আপনার ইচ্ছেমতো পরিবর্তন করুন */
    height: auto;
    z-index: 10;
    opacity: 1;      /* সম্পূর্ণ অস্বচ্ছ (Transparent নয়) */
    border-radius: 8px; /* একটু গোলাকার */
    box-shadow: 0 0 8px rgba(255,255,255,0.3), 0 2px 4px rgba(0,0,0,0.2); /* পুলশে ইফেক্ট (Glow) */
    pointer-events: none; /* লোগোতে ক্লিক করা গেলে ভিডিও কন্ট্রোল বাধা পাবে না */
    transition: all 0.3s ease;
  }
  
  /* হোভার করলে পুলশে ইফেক্ট বাড়বে */
  .rv-logo:hover {
    box-shadow: 0 0 15px rgba(255,255,255,0.6), 0 4px 8px rgba(0,0,0,0.3);
    transform: scale(1.02);
  }

  /* মোবাইলের জন্য সাইজ ছোট */
  @media (max-width: 600px) {
    .rv-logo {
      width: 55px;
      top: 5px;
      right: 5px;
      border-radius: 6px;
    }
  }

  /* Red label + Green content rows - কোনো পরিবর্তন নেই */
  .tt-row{
    display:flex;
    align-items:stretch;
    margin-top:6px;
    font-size:20px;
    font-weight:700;
    overflow:hidden;
  }
  .tt-row .tt-label{
    background:#e30613;
    color:#fff;
    padding:10px 16px;
    white-space:nowrap;
    display:flex;
    align-items:center;
  }
  .tt-row .tt-value{
    background:#1aa84a;
    color:#fff;
    padding:10px 14px;
    flex:1;
    overflow:hidden;
    white-space:nowrap;
    position:relative;
  }
  .tt-row .tt-value span{
    display:inline-block;
    padding-left:100%;
    animation:ttMarquee var(--sp,40s) linear infinite;
  }
  @keyframes ttMarquee{
    0%{transform:translateX(0)}
    100%{transform:translateX(-100%)}
  }

  .tt-datetime{
    margin-top:6px;
    background:#e30613;
    color:#fff;
    text-align:center;
    padding:14px;
    font-size:22px;
    font-weight:700;
  }

  @media (max-width:600px){
    .tt-livetv-wrap{margin:10px auto;padding:0 6px;}
    .tt-row,.tt-datetime{font-size:16px;}
    .tt-row .tt-label,.tt-row .tt-value{padding:8px 10px;}
    .tt-datetime{padding:10px;font-size:17px;}
  }
</style>

<div class="tt-livetv-wrap">

  <!-- ভিডিও প্লেয়ার + আরভি লোগো -->
  <div class="tt-livetv-box">
    <?php if ($liveStatus !== 'active' || $liveURL === ''): ?>
      <div class="tt-livetv-off">● Off Air</div>

    <?php elseif ($liveSource === 'hls'): ?>
      <video id="tt-hls" playsinline autoplay controls></video>
      <script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
      <script>
        (function(){
          var video = document.getElementById('tt-hls');
          var url = <?php echo json_encode($liveURL); ?>;
          
          video.controls = true;
          video.muted = false;
          video.volume = 1.0;
          
          function startPlay() {
            video.play().catch(function(e) {
              console.log("Auto-play error:", e);
              document.body.addEventListener('click', function playOnClick() {
                video.play();
                document.body.removeEventListener('click', playOnClick);
              }, { once: true });
            });
          }
          
          if (window.Hls && Hls.isSupported()) {
            var hls = new Hls();
            hls.loadSource(url);
            hls.attachMedia(video);
            hls.on(Hls.Events.MANIFEST_PARSED, startPlay);
          } else if (video.canPlayType('application/vnd.apple.mpegurl')) {
            video.src = url;
            video.addEventListener('loadedmetadata', startPlay);
          } else {
            video.src = url;
            startPlay();
          }
        })();
      </script>

    <?php elseif ($embedURL !== ''): ?>
      <iframe src="<?php echo htmlspecialchars($embedURL, ENT_QUOTES); ?>"
              allow="autoplay; encrypted-media; picture-in-picture"
              allowfullscreen></iframe>

    <?php else: ?>
      <div class="tt-livetv-off">● Stream Unavailable</div>
    <?php endif; ?>

    <!-- ===== আরভি লোগো (উপরের ডান পাশের কোনায় - একটু বেশি ডানে ও উপরে) ===== -->
    <?php if (!empty($rv_logo_url)): ?>
      <img src="<?php echo htmlspecialchars($rv_logo_url, ENT_QUOTES); ?>" 
           alt="RV Logo" 
           class="rv-logo"
           onerror="this.style.display='none'"> <!-- লোগো না থাকলে লুকিয়ে যাবে -->
    <?php endif; ?>
  </div>

  <!-- নোটিশ (লাল + সবুজ) - কোনো পরিবর্তন নেই -->
  <?php if ($noticeOn === 'on' && $noticeText !== ''): ?>
    <div class="tt-row">
      <div class="tt-label"><?php echo htmlspecialchars($noticeTitle !== '' ? $noticeTitle : 'নোটিশ:', ENT_QUOTES); ?></div>
      <div class="tt-value" style="--sp:<?php echo htmlspecialchars($noticeSpeed, ENT_QUOTES); ?>;">
        <span><?php echo htmlspecialchars($noticeText, ENT_QUOTES); ?></span>
      </div>
    </div>
  <?php endif; ?>

  <!-- তারিখ ও সময় - কোনো পরিবর্তন নেই -->
  <div class="tt-datetime" id="tt-datetime"></div>
</div>

<script>
(function(){
  var bnDigits=['০','১','২','৩','৪','৫','৬','৭','৮','৯'];
  var bnMonths=['জানুয়ারি','ফেব্রুয়ারি','মার্চ','এপ্রিল','মে','জুন','জুলাই','আগস্ট','সেপ্টেম্বর','অক্টোবর','নভেম্বর','ডিসেম্বর'];
  function toBn(s){return String(s).replace(/[0-9]/g,function(d){return bnDigits[d];});}
  function pad(n){return n<10?'0'+n:''+n;}
  function tick(){
    var d=new Date();
    var date=toBn(d.getDate())+' '+bnMonths[d.getMonth()]+' '+toBn(d.getFullYear());
    var h=d.getHours(); var ampm=h>=12?'পিএম':'এএম'; h=h%12; if(h===0)h=12;
    var time=toBn(pad(h))+':'+toBn(pad(d.getMinutes()))+':'+toBn(pad(d.getSeconds()));
    var el=document.getElementById('tt-datetime');
    if(el) el.textContent=date+' । '+time+' '+ampm;
  }
  tick(); setInterval(tick,1000);
})();
</script>

<?php require_once('./footer.php'); ?>