<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>TV Mode - {{ shop.name }}</title>
  <style>
    /* Reset and full-screen layout */
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      background: #000;
      overflow: hidden;
      font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    }

    /* TV container - CSS Grid */
    .tv-container {
      display: grid;
      grid-template-areas: "screen";
      width: 100vw;
      height: 100vh;
      position: relative;
    }

    /* Player iframe */
    #tv-screen {
      grid-area: screen;
      width: 100%;
      height: 100%;
      border: none;
    }

    /* TV controls overlay */
    .tv-controls {
      grid-area: screen;
      display: grid;
      place-items: center;
      z-index: 10;
      pointer-events: none;
      opacity: 0;
      transition: opacity 0.3s;
    }

    .tv-container:hover .tv-controls {
      opacity: 1;
    }

    /* Control buttons */
    .controls-panel {
      display: grid;
      grid-template-columns: auto auto auto;
      gap: 1rem;
      background: rgba(0,0,0,0.85);
      padding: 2rem;
      border-radius: 12px;
      pointer-events: auto;
    }

    .controls-panel button {
      background: rgba(255,255,255,0.2);
      border: 2px solid rgba(255,255,255,0.3);
      color: white;
      padding: 1rem 1.5rem;
      border-radius: 8px;
      cursor: pointer;
      font-size: 1.1rem;
      transition: all 0.2s;
      min-width: 120px;
    }

    .controls-panel button:hover:not(:disabled) {
      background: rgba(255,255,255,0.4);
      border-color: rgba(255,255,255,0.6);
      transform: scale(1.05);
    }

    .controls-panel button:disabled {
      opacity: 0.3;
      cursor: not-allowed;
    }

    .controls-panel button.primary {
      font-size: 1.5rem;
      padding: 1.5rem;
    }

    /* Shop branding */
    .tv-branding {
      position: absolute;
      top: 2rem;
      left: 2rem;
      color: white;
      background: rgba(0,0,0,0.5);
      padding: 1rem 1.5rem;
      border-radius: 8px;
      font-size: 1.5rem;
      font-weight: bold;
      z-index: 5;
      opacity: 0;
      transition: opacity 0.3s;
      pointer-events: none;
    }

    .tv-container:hover .tv-branding {
      opacity: 1;
    }

    /* Loading indicator */
    .tv-loading {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      color: white;
      font-size: 2rem;
      z-index: 100;
      pointer-events: none;
    }

    .tv-loading.hidden {
      display: none;
    }

    /* Mobile adjustments */
    @media (max-width: 600px) {
      .controls-panel {
        grid-template-columns: 1fr;
        gap: 0.75rem;
        padding: 1rem;
      }

      .controls-panel button {
        min-width: 200px;
      }

      .tv-branding {
        font-size: 1.2rem;
        padding: 0.75rem 1rem;
      }
    }
  </style>
</head>
<body>
  <div class="tv-container">
    <iframe id="tv-screen" src="/player/{{ start_product_id }}" allow="autoplay"></iframe>

    <div class="tv-branding">📺 {{ shop.name }}</div>

    <div class="tv-loading" id="tvLoading">Loading...</div>

    <div class="tv-controls">
      <div class="controls-panel">
        <button onclick="changeChannel()" title="Random media (C)">🎲 Random</button>
        <button onclick="exitTV()" title="Exit TV mode (ESC)" class="primary">✕ Exit</button>
        <button onclick="toggleFullscreen()" title="Fullscreen (F)">⛶ Full</button>
      </div>
    </div>
  </div>

  <script>
    'use strict';

    const iframe = document.getElementById('tv-screen');
    const loading = document.getElementById('tvLoading');

    // Hide loading indicator when iframe loads
    iframe.addEventListener('load', () => {
      loading.classList.add('hidden');
    });

    /**
     * Change to a random channel
     */
    function changeChannel() {
      loading.classList.remove('hidden');
      iframe.src = '/random';
    }

    /**
     * Exit TV mode
     */
    function exitTV() {
      window.location.href = '/';
    }

    /**
     * Toggle fullscreen
     */
    function toggleFullscreen() {
      if (!document.fullscreenElement) {
        document.documentElement.requestFullscreen().catch(err => {
          console.error('Error attempting to enable fullscreen:', err);
        });
      } else {
        document.exitFullscreen();
      }
    }

    /**
     * Keyboard shortcuts
     */
    document.addEventListener('keydown', (e) => {
      switch(e.key) {
        case 'Escape':
          exitTV();
          break;
        case 'c':
        case 'C':
          changeChannel();
          break;
        case 'f':
        case 'F':
          e.preventDefault();
          toggleFullscreen();
          break;
      }
    });

    /**
     * Listen for messages from the player iframe to auto-advance
     */
    window.addEventListener('message', (event) => {
      // Security: check origin if needed
      if (event.data && event.data.action === 'navigateToProduct') {
        const productId = event.data.productId;
        if (productId) {
          loading.classList.remove('hidden');
          iframe.src = '/player/' + productId;
        }
      } else if (event.data && event.data.action === 'playNext') {
        // Auto-advance to next
        changeChannel();
      }
    });

    // Auto-enter fullscreen on load (may require user interaction first)
    setTimeout(() => {
      if (!document.fullscreenElement) {
        toggleFullscreen();
      }
    }, 500);
  </script>
</body>
</html>
