import { useEffect, useState } from 'react' import { useNavigate } from 'react-router-dom' import { useLang } from '@/context/LangContext' import { Bookmark, Podcast, Clock, Calendar, Play, ChevronRight } from 'lucide-react' import { bootstrapMedia, videos as VIDEOS, podcasts as PODCASTS } from '@/data/media' import MediaWavePlayer from '@/components/MediaWavePlayer' import { authFetch, useAuth } from '@/context/AuthContext' const NAVY = '#032340' const GOLD = '#CD9E53' const T = { fa: { videoLabel: 'ویدیوکست', videoSub: 'آخرین گفتگوهای تصویری اندیشکده', podLabel: 'پادکست‌ها', podSub: 'شنیدنی‌های اندیشکده فولاد آینده', watchAll: 'مشاهده همه', listenAll: 'مشاهده همه', }, en: { videoLabel: 'Videocast', videoSub: 'Latest video conversations from the Institute', podLabel: 'Podcasts', podSub: 'Audio episodes from Future Steel Institute', watchAll: 'View All', listenAll: 'View All', }, } export default function VideocastPodcastSection({ only }: { only?: 'video' | 'pod' }) { const { lang } = useLang() const { member } = useAuth() const navigate = useNavigate() const t = T[lang] const [activeVideo, setActiveVideo] = useState(0) const [activePod, setActivePod] = useState(() => { const playableIndex = PODCASTS.findIndex(item => Boolean(item.mediaUrl?.trim())) return playableIndex >= 0 ? playableIndex : 0 }) const [podcasts, setPodcasts] = useState(() => [...PODCASTS]) const [bookmarkedIds, setBookmarkedIds] = useState>(new Set()) const [bookmarkBusy, setBookmarkBusy] = useState(false) const pod = podcasts[activePod] ?? podcasts[0] useEffect(() => { let active = true bootstrapMedia().then(() => { if (!active) return setPodcasts([...PODCASTS]) const playableIndex = PODCASTS.findIndex(item => Boolean(item.mediaUrl?.trim())) setActivePod(playableIndex >= 0 ? playableIndex : 0) }) return () => { active = false } }, []) useEffect(() => { if (!member) { setBookmarkedIds(new Set()); return } let active = true authFetch('/api/members/me/bookmarks') .then(async response => response.ok ? response.json() : []) .then(items => { if (!active || !Array.isArray(items)) return setBookmarkedIds(new Set(items.filter(item => item.contentType === 'podcast').map(item => String(item.contentId)))) }) .catch(() => {}) return () => { active = false } }, [member]) const togglePodcastBookmark = async () => { if (!member) { navigate('/login'); return } if (!pod || bookmarkBusy) return const contentId = String(pod.id) const saved = bookmarkedIds.has(contentId) setBookmarkBusy(true) try { const response = saved ? await authFetch(`/api/members/me/bookmarks/podcast/${encodeURIComponent(contentId)}`, { method: 'DELETE' }) : await authFetch('/api/members/me/bookmarks', { method: 'POST', body: JSON.stringify({ contentId, contentType: 'podcast', title: pod[lang].title, summary: pod[lang].body, coverImage: pod.cover, path: '/podcast', }), }) if (!response.ok) throw new Error('bookmark_failed') setBookmarkedIds(current => { const next = new Set(current) if (saved) next.delete(contentId) else next.add(contentId) return next }) } catch { /* keep the previous state when the request fails */ } finally { setBookmarkBusy(false) } } const showVideo = only !== 'pod' const showPod = only !== 'video' // when split into one column, give it full width instead of 60/40 const gridCols = only ? '1fr' : '60% 40%' return (
{/* ── VIDEOCAST (full-width featured player + playlist + description) ── */} {showVideo && (
{/* heading */}

{t.videoLabel}

{/* featured player + playlist — all in one row */}
{/* center play */} {/* controls bar */}
🔊 ۰۹:۲۷ / {VIDEOS[activeVideo].duration}
{/* title — directly below the video, inside the right column */}
🎙

{VIDEOS[activeVideo][lang].title}

{VIDEOS[activeVideo][lang].body}

{/* playlist column — beside the featured player */}
{/* video playlist — 3 thumbnail cards */}
{VIDEOS.slice(0, 3).map((v, i) => ( ))}
)} {/* ── PODCAST (full width) ── */} {showPod && (
{/* heading */}

{t.podLabel}

{/* ── featured player card ── */}
{/* cover — full height, flush to right edge */}
{/* rating badge overlaid at bottom of cover */}
★★★★★ {pod.rating}
{/* player body — directly beside cover, no gap */}
{/* bookmark */}
{/* title */}

{pod[lang].title}

{/* date */}
{pod.date}
{/* play + waveform */} {pod.mediaUrl ? (
) : (
۰۲:۳۱
{[4,6,8,10,7,5,8,12,15,18,14,10,8,12,16,20,22,17,12,9,12,15,18,22,24,20,15,11,8,12,15,17,20,22,18,14,10,8,12,15,19,21,16,12,9,11,14,18,20,16,11,8,6,9,12,15,17,13,9,7,10,13,16,18,14,10,8,11,14,16,13,9,7,10,13,16,18,14,10,7].map((h, i) => ( ))}
{pod.duration}
)}
{/* ── episode list (left) + description (right) ── */}
{/* episode list — first DOM → left in LTR, left in RTL via order */}
{podcasts.filter((_, idx) => idx !== activePod).slice(0, 2).map((p) => { const originalIndex = podcasts.findIndex(item => item.id === p.id); return ( ) })}
{/* description — second DOM → right in LTR, right in RTL via order */}
{lang === 'fa' ? ( <>

{pod[lang].title}

) : ( <>

{pod[lang].title}

)}

{pod[lang].body}

{lang === 'fa' ? 'مهمان برنامه: ' : 'Guest: '}{pod[lang].guest}
)}
) }