import React, { useEffect, useState, useRef } from 'react'; import { Scan, Scissors, Activity, Radio, CheckCircle } from 'lucide-react'; import { getApiUrl } from '../config'; const ProcessingAnimation = ({ media, isComplete, syncedTime, isSyncedPlaying, syncTrigger }) => { const [videoSrc, setVideoSrc] = useState(null); const [isYouTube, setIsYouTube] = useState(false); const videoRef = useRef(null); const iframeRef = useRef(null); useEffect(() => { if (!media) return; if (media.type === 'file') { const url = URL.createObjectURL(media.payload); setIsYouTube(false); setVideoSrc(url); return () => URL.revokeObjectURL(url); } else if (media.type === 'server') { // Uploaded source served from the backend (survives a page reload). setIsYouTube(false); setVideoSrc(getApiUrl(media.payload)); } else if (media.type === 'url') { setIsYouTube(true); const videoId = getYouTubeId(media.payload); setVideoSrc(videoId); } }, [media]); // Handle Sync Playback for Local Video useEffect(() => { if (!isYouTube && videoRef.current) { if (isSyncedPlaying) { // Sync Mode: Seek to time and Play videoRef.current.currentTime = syncedTime; videoRef.current.play().catch(e => console.log("Auto-play prevented", e)); videoRef.current.loop = false; videoRef.current.muted = true; // Keep muted to avoid double audio with clip } else { // Stop Sync: Pause. Once analysis is complete, resume the ambient loop. videoRef.current.pause(); if (isComplete) { videoRef.current.loop = true; videoRef.current.play().catch(e => console.log("Ambient play prevented", e)); } } } }, [syncedTime, isSyncedPlaying, isYouTube, isComplete, syncTrigger]); // Handle Sync Playback for YouTube (Basic Iframe Control via PostMessage) useEffect(() => { if (isYouTube && iframeRef.current && videoSrc) { const iframeWindow = iframeRef.current.contentWindow; if (isSyncedPlaying) { // Seek and Play iframeWindow.postMessage(JSON.stringify({ event: 'command', func: 'seekTo', args: [syncedTime, true] }), '*'); iframeWindow.postMessage(JSON.stringify({ event: 'command', func: 'playVideo', args: [] }), '*'); } else { // Pause iframeWindow.postMessage(JSON.stringify({ event: 'command', func: 'pauseVideo', args: [] }), '*'); } } }, [syncedTime, isSyncedPlaying, isYouTube, videoSrc, syncTrigger]); const getYouTubeId = (url) => { const regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|&v=)([^#&?]*).*/; const match = url.match(regExp); return (match && match[2].length === 11) ? match[2] : null; }; const containerClasses = `relative w-full aspect-video rounded-card overflow-hidden bg-black border border-rule2 mb-8 group animate-fade transition-all duration-500 ${isComplete && !isSyncedPlaying ? 'grayscale brightness-50' : ''} ${isSyncedPlaying ? 'ring-2 ring-brass ring-offset-2 ring-offset-black' : ''}`; const getVideoOpacityClass = () => { if (isSyncedPlaying) return 'opacity-100'; // Playing: Full visibility if (isComplete) return 'opacity-30'; // Idle Result: Darker return 'opacity-40 grayscale group-hover:grayscale-0'; // Processing: Dark + Grayscale effect }; return (