import { EventsTimeline } from "@/components/live-detail/events-timeline";
import { LiveLeagueInfo } from "@/components/live-detail/live-league-info";
import { LiveMatchTabs } from "@/components/live-detail/live-match-tabs";
import { LiveScoreHeader } from "@/components/live-detail/live-score-header";
import { OddsCard } from "@/components/live-detail/odds-card";
import { OtherInfoCard } from "@/components/live-detail/other-info-card";
import { StatsCard } from "@/components/live-detail/stats-card";
import { TennisPowerGraph } from "@/components/live-detail/tennis-power-graph";
import { TennisScoreboard } from "@/components/live-detail/tennis-scoreboard";
import { TennisStatsCard } from "@/components/live-detail/tennis-stats-card";
import { ThemedText } from "@/components/themed-text";
import { ThemedView } from "@/components/themed-view";
import { Colors } from "@/constants/theme";
import { useTheme } from "@/context/ThemeContext";
import { fetchLiveScore } from "@/lib/api";
import { LiveScoreMatch } from "@/types/api";
import { useLocalSearchParams } from "expo-router";
import React, { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import {
  ActivityIndicator,
  ScrollView,
  StyleSheet,
  TouchableOpacity,
  View,
} from "react-native";
import { useSafeAreaInsets } from "react-native-safe-area-context";

export default function LiveDetailScreen() {
  const { id, league_id, sport_id } = useLocalSearchParams<{
    id: string;
    league_id: string;
    sport_id: string;
  }>();
  const { theme } = useTheme();
  const { t } = useTranslation();
  const insets = useSafeAreaInsets();
  const isDark = theme === "dark";

  const [loading, setLoading] = useState(true);
  const [match, setMatch] = useState<LiveScoreMatch | null>(null);
  const [activeTab, setActiveTab] = useState("detail"); // Default to detail to show all data

  useEffect(() => {
    loadLiveDetail();
    // 设置每 15 秒更新一次直播比分
    const timer = setInterval(() => {
      refreshLiveDetail();
    }, 15000);
    return () => clearInterval(timer);
  }, [id, league_id]);

  const loadLiveDetail = async () => {
    try {
      setLoading(true);
      await refreshLiveDetail();
    } catch (err) {
      console.error(err);
    } finally {
      setLoading(false);
    }
  };

  const refreshLiveDetail = async () => {
    try {
      // Fetch live scores for the league
      const sportId = parseInt(sport_id || "1");
      const leagueId = parseInt(league_id || "0");

      const liveData = await fetchLiveScore(sportId, leagueId);

      if (liveData && Array.isArray(liveData)) {
        // Find the specific match
        const found = liveData.find((m) => m.event_key.toString() === id);
        if (found) {
          setMatch(found);
        }
      }
    } catch (err) {
      console.error("Refresh live detail error:", err);
    }
  };

  if (loading) {
    return (
      <ThemedView style={styles.center}>
        <ActivityIndicator size="large" color={Colors[theme].tint} />
      </ThemedView>
    );
  }

  if (!match) {
    return (
      <ThemedView style={styles.center}>
        <ThemedText>{t("detail.not_found")}</ThemedText>
        <TouchableOpacity style={styles.retryButton} onPress={loadLiveDetail}>
          <ThemedText style={styles.retryText}>{t("detail.retry")}</ThemedText>
        </TouchableOpacity>
      </ThemedView>
    );
  }

  const renderTabContent = () => {
    const numericSportId = parseInt(sport_id || "1");
    // Tennis Check
    const isTennis =
      !!match.event_first_player ||
      (match.league_name &&
        /ATP|WTA|ITF|Challenger/i.test(match.league_name || ""));

    switch (activeTab) {
      case "stats":
        return !isTennis ? (
          <StatsCard match={match} isDark={isDark} />
        ) : (
          <View style={styles.center}>
            <ThemedText style={{ opacity: 0.5 }}>
              {t("detail.empty_stats")}
            </ThemedText>
          </View>
        );
      case "odds":
        return (
          <OddsCard match={match} isDark={isDark} sportId={numericSportId} />
        );
      case "detail":
        if (isTennis) {
          return (
            <>
              <TennisScoreboard match={match} isDark={isDark} />
              <TennisStatsCard match={match} isDark={isDark} />
              <TennisPowerGraph match={match} isDark={isDark} />
            </>
          );
        }
        return (
          <>
            <OddsCard match={match} isDark={isDark} sportId={numericSportId} />
            <StatsCard match={match} isDark={isDark} />
            <EventsTimeline match={match} isDark={isDark} />
            <OtherInfoCard match={match} isDark={isDark} />
          </>
        );
      default:
        return (
          <View style={styles.center}>
            <ThemedText style={{ opacity: 0.5 }}>
              {t("detail.empty_stats")}
            </ThemedText>
          </View>
        );
    }
  };

  return (
    <ThemedView style={styles.container}>
      <ScrollView
        bounces={false}
        showsVerticalScrollIndicator={false}
        contentContainerStyle={{ paddingBottom: insets.bottom + 20 }}
      >
        <LiveScoreHeader match={match} topInset={insets.top} />
        <LiveLeagueInfo match={match} />
        <LiveMatchTabs
          activeTab={activeTab}
          onTabChange={setActiveTab}
          isDark={isDark}
        />

        {renderTabContent()}
      </ScrollView>
    </ThemedView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  center: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    padding: 20,
    minHeight: 200,
  },
  retryButton: {
    marginTop: 20,
    paddingHorizontal: 20,
    paddingVertical: 10,
    backgroundColor: "#007AFF",
    borderRadius: 8,
  },
  retryText: {
    color: "#FFF",
    fontWeight: "600",
  },
});
