'use client'; import { useState, useRef, useEffect } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { FaPlay, FaPause, FaStepBackward, FaStepForward, FaTimes, FaChevronDown, FaChevronUp } from 'react-icons/fa'; import { Album, Song } from '@/lib/types'; import { formatPrice } from '@/lib/utils'; interface MusicPlayerProps { album: Album | null; isPurchased: boolean; onClose: () => void; onPurchase: (album: Album) => void; } export default function MusicPlayer({ album, isPurchased, onClose, onPurchase }: MusicPlayerProps) { const [currentSongIndex, setCurrentSongIndex] = useState(0); const [isPlaying, setIsPlaying] = useState(false); const [currentTime, setCurrentTime] = useState(0); const [duration, setDuration] = useState(0); const [showTrackList, setShowTrackList] = useState(false); const audioRef = useRef(null); const currentSong = album?.songs[currentSongIndex]; useEffect(() => { if (album) { setCurrentSongIndex(0); setIsPlaying(false); setCurrentTime(0); } }, [album]); useEffect(() => { const audio = audioRef.current; console.log(audioRef.current) if (!audio) return; const updateTime = () => setCurrentTime(audio.currentTime); const updateDuration = () => setDuration(audio.duration); const handleEnded = () => { if (!isPurchased) { // Preview ended setIsPlaying(false); setCurrentTime(0); } else { // Move to next song handleNext(); } }; audio.addEventListener('timeupdate', updateTime); audio.addEventListener('loadedmetadata', updateDuration); audio.addEventListener('ended', handleEnded); return () => { audio.removeEventListener('timeupdate', updateTime); audio.removeEventListener('loadedmetadata', updateDuration); audio.removeEventListener('ended', handleEnded); }; }, [isPurchased, currentSongIndex, album]); const togglePlay = () => { const audio = audioRef.current; if (!audio) return; if (isPlaying) { audio.pause(); } else { audio.play(); } setIsPlaying(!isPlaying); }; const handleNext = () => { if (!album) return; const nextIndex = (currentSongIndex + 1) % album.songs.length; setCurrentSongIndex(nextIndex); setIsPlaying(false); setCurrentTime(0); }; const handlePrevious = () => { if (!album) return; const prevIndex = currentSongIndex === 0 ? album.songs.length - 1 : currentSongIndex - 1; setCurrentSongIndex(prevIndex); setIsPlaying(false); setCurrentTime(0); }; const handleSeek = (e: React.ChangeEvent) => { const audio = audioRef.current; if (!audio || !isPurchased) return; // Only allow seeking for purchased albums const time = parseFloat(e.target.value); audio.currentTime = time; setCurrentTime(time); }; const formatTime = (time: number) => { if (isNaN(time)) return '0:00'; const minutes = Math.floor(time / 60); const seconds = Math.floor(time % 60); return `${minutes}:${seconds.toString().padStart(2, '0')}`; }; if (!album) return null; return ( {album && ( <> {/* Backdrop */} {/* Player Modal */}
{/* Close Button - Top Right */} {/* Album Artwork */}
{/*
*/} {album.title} {/*
*/}
{/* Preview Badge */} {!isPurchased && (
Preview
)}
{/* Song Info */}

{currentSong?.title}

{album.title}

Track {currentSongIndex + 1} of {album.songs.length}

{/* Progress Bar */}
{formatTime(currentTime)} {isPurchased ? formatTime(duration) : '0:30'}
{/* Controls */} {isPlaying ? ( ) : ( )} {/* Purchase Button */} {!isPurchased && ( onPurchase(album)} className="w-full mb-2 py-2.5 bg-paper-brown hover:bg-paper-dark border-2 border-paper-dark font-semibold text-paper-light text-sm transition-all shadow-paper-lg" > Purchase Full Album - {formatPrice(album.price)} )} {/* Track List Toggle */} {showTrackList && (
{album.songs.map((song, index) => ( ))}
)}
{/* Hidden Audio Element */}
)}
); }