import { useEffect, useRef } from 'react';
import { Animated, Easing, Text, View } from 'react-native';
import { Ionicons } from '@expo/vector-icons';
import { useColors } from '../../lib/theme';

/**
 * "Tap-Here"-Indicator UNTER einem Screenshot.
 *
 * Vorher: absolut-positionierter Pulse-Ring INNERHALB des Screenshots
 * → musste per-locale fine-tuned werden weil Apple-Dialog-Dimensionen
 *   pro Sprache variieren (DE-Text länger als EN, Modal höher, Button
 *   rutscht runter…).
 *
 * Jetzt: render-unter dem Screenshot. Layout-agnostic. Animation lenkt
 * Aufmerksamkeit auf die richtige Region ohne pixel-genaue Position.
 *
 *   ┌──────────────────────────┐
 *   │  <iOS Dialog Screenshot>  │
 *   │   [Nicht erlauben]        │
 *   │   [Erlauben]              │
 *   └──────────────────────────┘
 *           ▲
 *      ┌─────────────┐
 *      │ ▲ Tippe     │   ← Animated bouncing pill mit Pfeil + Label
 *      │  "Erlauben"  │
 *      └─────────────┘
 */
export function ScreenshotPointer({
  buttonLabel,
}: {
  /** Der Text auf dem korrekten Button im iOS-Dialog. Wird ins Label übernommen. */
  buttonLabel: string;
}) {
  const colors = useColors();
  const bounce = useRef(new Animated.Value(0)).current;
  const pulse = useRef(new Animated.Value(0)).current;

  useEffect(() => {
    const bounceLoop = Animated.loop(
      Animated.sequence([
        Animated.timing(bounce, {
          toValue: 1,
          duration: 600,
          useNativeDriver: true,
          easing: Easing.out(Easing.cubic),
        }),
        Animated.timing(bounce, {
          toValue: 0,
          duration: 600,
          useNativeDriver: true,
          easing: Easing.in(Easing.cubic),
        }),
      ]),
    );
    const pulseLoop = Animated.loop(
      Animated.sequence([
        Animated.timing(pulse, { toValue: 1, duration: 900, useNativeDriver: true }),
        Animated.timing(pulse, { toValue: 0, duration: 900, useNativeDriver: true }),
      ]),
    );
    bounceLoop.start();
    pulseLoop.start();
    return () => {
      bounceLoop.stop();
      pulseLoop.stop();
    };
  }, [bounce, pulse]);

  const translateY = bounce.interpolate({ inputRange: [0, 1], outputRange: [0, -6] });
  const arrowOpacity = pulse.interpolate({ inputRange: [0, 1], outputRange: [0.6, 1] });
  const arrowScale = pulse.interpolate({ inputRange: [0, 1], outputRange: [1, 1.12] });

  return (
    <View style={{ alignItems: 'center', marginTop: 8 }}>
      {/* Animated Up-Arrow zeigt auf den Screenshot (auf dessen unteren Button) */}
      <Animated.View
        style={{
          opacity: arrowOpacity,
          transform: [{ translateY }, { scale: arrowScale }],
        }}
      >
        <Ionicons name="caret-up" size={28} color={colors.brandOrange} />
      </Animated.View>

      {/* Label-Pille */}
      <Animated.View
        style={{
          marginTop: 4,
          backgroundColor: colors.brandOrange,
          borderRadius: 999,
          paddingVertical: 8,
          paddingHorizontal: 16,
          flexDirection: 'row',
          alignItems: 'center',
          gap: 8,
          transform: [{ translateY }],
        }}
      >
        <Text style={{ fontSize: 16 }}>👆</Text>
        <Text
          style={{
            fontFamily: 'Nunito_700Bold',
            fontSize: 14,
            color: '#ffffff',
            letterSpacing: 0.2,
          }}
        >
          {buttonLabel}
        </Text>
      </Animated.View>
    </View>
  );
}
