import { ThemedText } from "@/components/themed-text"; import { fetchH2H } from "@/lib/api"; import { H2HData, MatchDetailData } from "@/types/api"; import React, { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { Image, StyleSheet, View } from "react-native"; interface CricketH2HCardProps { data: MatchDetailData; isDark: boolean; } export function CricketH2HCard({ data, isDark }: CricketH2HCardProps) { const { t } = useTranslation(); const { match } = data; const [h2hData, setH2hData] = useState(null); const [loading, setLoading] = useState(true); const bgColor = isDark ? "#1C1C1E" : "#FFF"; const subTextColor = isDark ? "#A0A0A0" : "#666"; useEffect(() => { loadH2H(); }, []); const loadH2H = async () => { try { setLoading(true); const sportId = match.sportId; const options: any = {}; options.firstTeamId = parseInt(match.homeTeamKey); options.secondTeamId = parseInt(match.awayTeamKey); const result = await fetchH2H(sportId, options); setH2hData(result); } catch (err) { console.error(err); } finally { setLoading(false); } }; const h2hStats = React.useMemo(() => { if (!h2hData?.H2H) return { p1Wins: 0, p2Wins: 0, total: 0 }; const list = h2hData.H2H; // Mock calculation matching CricketH2H logic return { p1Wins: Math.floor(list.length / 3), p2Wins: list.length - Math.floor(list.length / 3), total: list.length, }; }, [h2hData]); const p1Percent = h2hStats.total > 0 ? ((h2hStats.p1Wins / h2hStats.total) * 100).toFixed(0) : "0"; const p2Percent = h2hStats.total > 0 ? ((h2hStats.p2Wins / h2hStats.total) * 100).toFixed(0) : "0"; if (loading) { return null; } if (h2hStats.total === 0) return null; return ( {t("detail.h2h_card.title")} {t("detail.h2h_card.total_matches")} ({h2hStats.total}) {/* Progress Bar */} {/* Stats Text */} {h2hStats.p1Wins} {t("detail.h2h_card.wins")} {p1Percent}% {h2hStats.p2Wins} {t("detail.h2h_card.wins")} {p2Percent}% ); } const styles = StyleSheet.create({ card: { margin: 16, padding: 16, borderRadius: 12, // Shadow standard elevation: 2, shadowColor: "#000", shadowOpacity: 0.1, shadowOffset: { width: 0, height: 2 }, shadowRadius: 4, }, title: { fontSize: 14, color: "#888", marginBottom: 16, }, headerRow: { flexDirection: "row", justifyContent: "space-between", alignItems: "center", marginBottom: 12, }, teamContainer: { alignItems: "center", }, teamLogo: { width: 32, height: 32, resizeMode: "contain", }, totalText: { fontSize: 13, color: "#666", }, progressBar: { flexDirection: "row", height: 8, backgroundColor: "#EEE", borderRadius: 4, marginBottom: 12, }, progressSegment: { height: "100%", }, statsRow: { flexDirection: "row", justifyContent: "space-between", }, });