import { useEffect, useState } from 'react';
import { View, Text, StyleSheet, Pressable } from 'react-native';
import { LinearGradient } from 'expo-linear-gradient';
import Ionicons from '@expo/vector-icons/Ionicons';
import { spacing } from '../../../theme';
import { trackingRadii } from '../theme';
import { FAST_HERO_GRADIENT, FastOverviewForHero, buildFastHeroView } from '../utils/fasting';

type Props = {
  overview: FastOverviewForHero;
  onPress: () => void;
};

// Équivalent du widget `.tpc-fast-hero` (templates/tracking/_summary.html.twig,
// data-tk-fast-widget / tkTickFastingWidget() dans assets/js/tracking.js) qui existait
// sur l'ancienne page "Mon suivi" mais avait été perdu dans le portage React.
export function FastingHeroCard({ overview, onPress }: Props) {
  const [now, setNow] = useState(() => new Date());
  useEffect(() => {
    const id = setInterval(() => setNow(new Date()), 1000);
    return () => clearInterval(id);
  }, []);

  const view = buildFastHeroView(overview, now);

  return (
    <Pressable onPress={onPress}>
      <LinearGradient colors={FAST_HERO_GRADIENT[view.tone]} start={{ x: 0, y: 0 }} end={{ x: 1, y: 1 }} style={styles.card}>
        <View style={styles.labelRow}>
          <Ionicons name="time-outline" size={14} color="#fff" />
          <Text style={styles.label}>{view.label}</Text>
        </View>

        <Text style={styles.timer}>{view.timer}</Text>

        {view.showProgress && (
          <View style={styles.progressTrack}>
            <View style={[styles.progressFill, { width: `${view.progressPct}%` }]} />
          </View>
        )}

        <Text style={styles.goal}>{view.sub}</Text>
      </LinearGradient>
    </Pressable>
  );
}

const styles = StyleSheet.create({
  card: { borderRadius: trackingRadii.cardSm, padding: spacing.lg, gap: spacing.sm },
  labelRow: { flexDirection: 'row', alignItems: 'center', gap: 8 },
  label: { fontSize: 12, fontWeight: '800', color: '#fff', letterSpacing: 1, textTransform: 'uppercase' },
  timer: { fontSize: 32, fontWeight: '800', color: '#fff', letterSpacing: -0.5 },
  progressTrack: { height: 9, borderRadius: trackingRadii.pill, backgroundColor: 'rgba(255,255,255,0.25)', overflow: 'hidden' },
  progressFill: { height: '100%', borderRadius: trackingRadii.pill, backgroundColor: '#fff' },
  goal: { fontSize: 13, fontWeight: '600', color: 'rgba(255,255,255,0.9)' },
});
