<template>
    <v-container v-show="gamehiding === false" class="player_video_div">
        <div v-if="currentQuestion" style="width: 100%; height: 100%;">
            
            <!-- LECTEUR VIDÉO -->
            <div v-show="currentQuestion.Type === 'video'" style="width: 100%; height: 100%;">
                <video ref="videoPlayer" class="video-js player_video" controls preload="auto">
                </video>
            </div>

            <!-- AFFICHAGE IMAGE -->
            <div v-if="currentQuestion.Type === 'picture'" style="width: 100%; height: 100%;">
                <v-img 
                    :src="getMediaUrl(currentQuestion.MediaUrl)" 
                    :key="currentQuestion.QuestionId"
                    class="player_video"
                ></v-img>
            </div>

            <!-- LECTEUR AUDIO -->
             <div v-if="currentQuestion.Type === 'audio'" class="audio-container player_video">
                 <div class="audio-visualizer">
                    <v-icon size="150" color="white" class="mb-4">mdi-music-circle</v-icon>
                    <span class="text-h2 white--text">ÉCOUTEZ</span>
                 </div>
                 <audio ref="audioPlayer" :src="getMediaUrl(currentQuestion.MediaUrl)" class="custom-audio"></audio>
             </div>

        </div>
    </v-container>
</template>

<script setup>
import { ref, computed, watch, onMounted, onBeforeUnmount, nextTick } from 'vue';
import quizStore from '@/store/quizStore';
import videojs from 'video.js';
import 'video.js/dist/video-js.css';
import { subscribeToTopic, publishMessage } from '@/services/mqttService';

// Accès au store
const currentQuestion = quizStore.getters.currentQuestion;

// Références du lecteur vidéo
const videoPlayer = ref(null);
let vjsPlayer = null;

// Références du lecteur audio
const audioPlayer = ref(null);

let gamehiding = ref(true);

// Méthodes
function getMediaUrl(relativePath) {
    if (!relativePath) return '';
    const cleanPath = relativePath.startsWith('/') ? relativePath.substring(1) : relativePath;
    const url = new URL(`../quizz/vulture-session-2026-01/${cleanPath}`, import.meta.url).href;
    console.log('GameMedia: Resolved URL:', { relativePath, url });
    return url;
}

function initVideoPlayer() {
    if (vjsPlayer) return; // Déjà initialisé
    if (!videoPlayer.value) return; // DOM pas prêt

    console.log('GameMedia: Initializing VideoJS');
    vjsPlayer = videojs(videoPlayer.value, {
        autoplay: false,
        controls: false,
        preload: 'auto',
        fluid: true,
        loop: false,
        volume: 0,
    }, () => {
        console.log('GameMedia: VideoJS Ready');
        // Si la question courante est une vidéo, la charger
        if (currentQuestion.value && currentQuestion.value.Type === 'video') {
             updateVideoSource();
        }
        
        // Masquer automatiquement à la fin de la vidéo
        vjsPlayer.on('ended', () => {
            console.log('GameMedia: Video ended, hiding');
            gamehiding.value = true;
            publishMessage('/display/control', 'hide');
        });
    });
}

function updateVideoSource() {
    if (!vjsPlayer || !currentQuestion.value) return;
    
    const url = getMediaUrl(currentQuestion.value.MediaUrl);
    console.log('GameMedia: Loading Video Source', url);
    vjsPlayer.src({ type: 'video/mp4', src: url });
    
    // L'autoplay est géré par la commande MQTT 'play' maintenant
    // if (currentQuestion.value.Settings?.AutoPlay) {
    //      vjsPlayer.play().catch(e => console.log('Autoplay blocked', e));
    // }
}

// Observateurs
watch(currentQuestion, async (newVal, oldVal) => {
    console.log('GameMedia: Question Changed', newVal);
    
    // Arrêter d'abord tous les médias
    if (vjsPlayer) {
        vjsPlayer.pause();
    }
    if (audioPlayer.value) {
        audioPlayer.value.pause();
    }
    
    // Rester masqué au changement de question jusqu'à la lecture
    gamehiding.value = true;
    publishMessage('/display/control', 'hide');
    if (!newVal) return;

    await nextTick(); // Attendre la mise à jour du DOM (v-if)

    if (newVal.Type === 'video') {
         if (!vjsPlayer) {
             initVideoPlayer();
         } else {
             updateVideoSource();
         }
    } else if (newVal.Type === 'audio') {
        // Chargement audio (pas d'autoplay)
         setTimeout(() => {
             if(audioPlayer.value){
                  console.log('GameMedia: Loading Audio');
                  audioPlayer.value.load();
                  
                  // Masquer automatiquement à la fin de l'audio
                  audioPlayer.value.onended = () => {
                      console.log('GameMedia: Audio ended, hiding');
                      gamehiding.value = true;
                      publishMessage('/display/control', 'hide');
                  };
             }
         }, 100);
    }
    // Pour le type 'picture', rien à faire, vidéo/audio déjà en pause
}, { immediate: true });

// Cycle de vie
onMounted(async () => {
    await nextTick();
    if (currentQuestion.value?.Type === 'video') {
        initVideoPlayer();
    }

    subscribeToTopic('#', (topic, message) => {
        handleMessage(topic, message);
    });
});

const handleMessage = (topic, message) => {
    console.log('GameMedia: Received', topic, message);
    if (topic === "/display/control") {
        switch (message) {
            case "play":
                gamehiding.value = false;
                console.log("▶️ GameMedia: Play");
                // Only play the media relevant to this question type
                if (currentQuestion.value?.Type === 'video' && vjsPlayer) {
                    vjsPlayer.play().catch(e => console.error("Error playing video:", e));
                }
                if (currentQuestion.value?.Type === 'audio' && audioPlayer.value) {
                    audioPlayer.value.play().catch(e => console.error("Error playing audio:", e));
                }
                if (currentQuestion.value?.Type === 'picture') {
                    // Démarrer le timer si PlayTime est configuré
                    const playTime = currentQuestion.value.Settings?.PlayTime;
                    if (playTime && playTime > 0) {
                        quizStore.actions.startTimer(playTime);
                    }
                }
                break;
            case "pause":
                gamehiding.value = true;
                console.log("⏸️ GameMedia: Pause");
                quizStore.actions.stopTimer();
                if (vjsPlayer) {
                    vjsPlayer.pause();
                }
                if (audioPlayer.value) {
                    audioPlayer.value.pause();
                }
                break;
             case "hide":
                console.log("🛑 GameMedia: Hide");
                gamehiding.value = true;
                quizStore.actions.stopTimer();
                break;
        }
    }
    // Vérifier le statut du buzzer pour masquer automatiquement au buzz (comme HidingOverlay)
    // Réplication de VideoPlayer qui n'avait que /display/control dans l'extrait fourni.
    // Comportement optionnel selon les événements du système.
    if (topic === 'vulture/buzzer/status') {
         try {
             const data = JSON.parse(message);
             if (data.status === 'blocked') {
                 console.log("GameMedia: Buzzer Blocked -> Hiding");
                 gamehiding.value = true;
                 if (vjsPlayer) vjsPlayer.pause();
                 if (audioPlayer.value) audioPlayer.value.pause();
             }
         } catch (e) { console.error('JSON Error', e); }
    }
};

onBeforeUnmount(() => {
    if (vjsPlayer) {
        vjsPlayer.dispose();
    }
});
</script>

<style>
.player_video_div {
    margin-top: 40px;
    width: calc(100vw - 20%);
    height: calc(100vh - 20%);
    border-radius: 20px !important;
  }
  .player_video {
    width: 100%;
    height: 100%;
    max-width: 100vw;
    max-height: 100vh;
    border-radius: 25px !important;
  }
  .vjs-tech{
    border-radius: 25px;
  }
  
  /* Styles additionnels pour les éléments Audio/Custom pour s'adapter au thème */
  .audio-container {
      display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        background: linear-gradient(45deg, #1a1a1a, #2c3e50);
  }
  .audio-visualizer {
        display: flex;
        flex-direction: column;
        align-items: center;
        animation: pulse 2s infinite;
    }
    .text-h2 {
        font-family: 'Bahnschrift', sans-serif !important;
    }
    .custom-audio {
        margin-top: 30px;
        width: 80%;
    }
    @keyframes pulse {
        0% { transform: scale(1); opacity: 0.8; }
        50% { transform: scale(1.05); opacity: 1; }
        100% { transform: scale(1); opacity: 0.8; }
    }
</style>
