import React, { useEffect } from 'react';
import { View, StyleSheet, Image } from 'react-native';
import Animated, {
  useAnimatedStyle,
  useSharedValue,
  withTiming,
  withDelay,
  withSequence,
} from 'react-native-reanimated';
import { LinearGradient } from 'expo-linear-gradient';
import { colors, spacing } from '../../theme/tokens';
import { Body, Caption } from '../../components/ui';
import { t } from '../../i18n';

interface SplashScreenProps {
  onFinish: () => void;
}

export function SplashScreen({ onFinish }: SplashScreenProps) {
  const logoOpacity = useSharedValue(0);
  const logoScale = useSharedValue(0.8);
  const textOpacity = useSharedValue(0);

  useEffect(() => {
    logoOpacity.value = withTiming(1, { duration: 800 });
    logoScale.value = withTiming(1, { duration: 800 });
    textOpacity.value = withDelay(400, withTiming(1, { duration: 600 }));

    const timer = setTimeout(onFinish, 2500);
    return () => clearTimeout(timer);
  }, []);

  const logoAnimStyle = useAnimatedStyle(() => ({
    opacity: logoOpacity.value,
    transform: [{ scale: logoScale.value }],
  }));

  const textAnimStyle = useAnimatedStyle(() => ({
    opacity: textOpacity.value,
  }));

  return (
    <LinearGradient
      colors={[colors.brand.cream, '#FFFFFF']}
      style={styles.container}
    >
      <Animated.View style={[styles.logoWrap, logoAnimStyle]}>
        <Image
          source={require('../../../assets/branding/logo-wifaq.png')}
          style={styles.logo}
          resizeMode="contain"
          accessibilityLabel="Logo Wifaq"
        />
      </Animated.View>
      <Animated.View style={textAnimStyle}>
        <Body style={styles.tagline} align="center" color={colors.neutral[500]}>
          Gestion financière équitable
        </Body>
      </Animated.View>
      <View style={styles.credits}>
        <Caption align="center" color={colors.neutral[400]}>
          {t('app.createdBy')} {t('app.creator')}
        </Caption>
        <Caption align="center" color={colors.neutral[400]}>
          {t('app.forOrg')} {t('app.organization')}
        </Caption>
      </View>
    </LinearGradient>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  logoWrap: {
    width: 160,
    height: 160,
    alignItems: 'center',
    justifyContent: 'center',
    marginBottom: spacing['2xl'],
  },
  logo: {
    width: 140,
    height: 140,
  },
  tagline: {
    letterSpacing: 0.5,
  },
  credits: {
    position: 'absolute',
    bottom: spacing['3xl'],
    alignItems: 'center',
  },
});
