import React, { useMemo } from 'react'; import { Modal, ModalOverlay, ModalContent, ModalHeader, ModalCloseButton, ModalBody, ModalFooter, Button, HStack, VStack, Text, Badge, Link, Divider, } from '@chakra-ui/react'; import { useCountdown } from '../../hooks/useCountdown'; import { assetUrl, sanitizeClubName } from '../../utils/url'; import { TeamLogo } from '../common/TeamLogo'; export type FacrMatchLike = { id?: string | number; date?: string; // yyyy-mm-dd time?: string; // HH:MM date_time?: string; // alternative combined format (dd.MM.yyyy HH:mm) home?: string; away?: string; home_logo_url?: string; away_logo_url?: string; home_id?: string; away_id?: string; competition?: string; competitionName?: string; venue?: string; score?: string | null; facr_link?: string | null; report_url?: string | null; }; interface MatchModalProps { isOpen: boolean; match: FacrMatchLike | null; onClose: () => void; onTeamClick?: (teamName: string, teamLogoUrl?: string) => void; } const formatWhen = (m: FacrMatchLike | null) => { if (!m) return ''; try { if (m.date && m.time) { const d = new Date(`${m.date}T${(m.time || '00:00')}:00`); if (!isNaN(d.getTime())) return d.toLocaleString(); } if (m.date_time) { // Try to parse dd.MM.yyyy HH:mm quickly by reordering const dt = String(m.date_time); const [dPart, tPart] = dt.split(' '); const [dd, MM, yyyy] = (dPart || '').split('.'); if (dd && MM && yyyy) { const iso = `${yyyy}-${MM.padStart(2, '0')}-${dd.padStart(2, '0')}T${(tPart || '00:00')}:00`; const d = new Date(iso); if (!isNaN(d.getTime())) return d.toLocaleString(); } return m.date_time; } } catch {} return ''; }; export const MatchModal: React.FC = ({ isOpen, match, onClose, onTeamClick }) => { const kickoffIso = useMemo(() => { if (!match) return null; if (match.date && match.time) return `${match.date}T${(match.time || '00:00')}:00`; if (match.date_time) { const dt = String(match.date_time); const [dPart, tPart] = dt.split(' '); const [dd, MM, yyyy] = (dPart || '').split('.'); if (dd && MM && yyyy) return `${yyyy}-${MM.padStart(2, '0')}-${dd.padStart(2, '0')}T${(tPart || '00:00')}:00`; return match.date_time; // fallback } return null; }, [match]); const { countdownString, isActive, timeRemaining } = useCountdown(kickoffIso, 1000); const facrLink = match?.facr_link || match?.report_url || null; const when = formatWhen(match); // Determine if match has started (countdown finished) but no score yet const matchStarted = kickoffIso ? new Date(kickoffIso).getTime() <= Date.now() : false; const hasScore = match?.score && match.score.trim() !== ''; return ( {sanitizeClubName(match?.home) || 'Domácí'} vs {sanitizeClubName(match?.away) || 'Hosté'} {match && ( onTeamClick && onTeamClick(match.home || '', match.home_logo_url)} _hover={onTeamClick ? { opacity: 0.8, transform: 'scale(1.05)' } : {}} transition="all 0.2s" role={onTeamClick ? 'button' : undefined} tabIndex={onTeamClick ? 0 : undefined} > {sanitizeClubName(match.home) || 'Domácí'} {!matchStarted ? ( // Future match - show countdown or vs isActive && countdownString ? ( <> Začátek za {countdownString} ) : ( <> vs ) ) : hasScore ? ( // Match finished with score <> {match.score} Skončeno ) : ( // Match started but no score yet <> —:— Probíhá )} {(match.competition || match.competitionName) && ( {match.competition || match.competitionName} )} onTeamClick && onTeamClick(match.away || '', match.away_logo_url)} _hover={onTeamClick ? { opacity: 0.8, transform: 'scale(1.05)' } : {}} transition="all 0.2s" role={onTeamClick ? 'button' : undefined} tabIndex={onTeamClick ? 0 : undefined} > {sanitizeClubName(match.away) || 'Hosté'} {when && Kdy: {when}} {match.venue && Kde: {match.venue}} )} {facrLink && ( )} ); }; export default MatchModal;