import { useCallback, useEffect, useMemo, useRef } from 'react';
import {
  Animated,
  Image,
  StyleSheet,
  useWindowDimensions,
  View,
} from 'react-native';
import Logo from 'screens/../../assets/bootsplash/logo.png';
import AppText from './AppText';

const SplashScreen = ({ onFinish }: { onFinish: () => void }) => {
  const { width: screenWidth, height: screenHeight } = useWindowDimensions();
  const circleSize = useMemo(
    () => Math.min(Math.max(screenWidth * 0.6, 150), 300),
    [screenWidth],
  );

  const fadeAnim = useRef(new Animated.Value(0)).current;
  const scaleAnim = useRef(new Animated.Value(0.5)).current;
  const slideAnim = useRef(new Animated.Value(0)).current;

  const animationConfig = useMemo(
    () => ({
      fadeAnimation: Animated.timing(fadeAnim, {
        toValue: 1,
        duration: 1000,
        useNativeDriver: true,
      }),
      scaleAnimation: Animated.spring(scaleAnim, {
        toValue: 1,
        tension: 50,
        friction: 7,
        useNativeDriver: true,
      }),
      slideAnimation: Animated.timing(slideAnim, {
        toValue: -screenWidth,
        duration: 500,
        useNativeDriver: true,
      }),
    }),
    [fadeAnim, scaleAnim, slideAnim, screenWidth],
  );

  const startInitialAnimations = useCallback(() => {
    Animated.parallel([
      animationConfig.fadeAnimation,
      animationConfig.scaleAnimation,
    ]).start();
  }, [animationConfig]);

  const startSlideAnimation = useCallback(() => {
    animationConfig.slideAnimation.start(() => onFinish());
  }, [animationConfig, onFinish]);

  useEffect(() => {
    startInitialAnimations();

    const timeoutId = setTimeout(() => {
      startSlideAnimation();
    }, 2000);

    return () => clearTimeout(timeoutId);
  }, [startInitialAnimations, startSlideAnimation]);

  const circleStyles = useMemo(
    () => ({
      outer: {
        width: screenWidth * 1.5,
        height: screenWidth * 1.5,
        borderRadius: (screenWidth * 1.5) / 2,
      },
      middle: {
        width: screenWidth * 1.1,
        height: screenWidth * 1.1,
        borderRadius: (screenWidth * 1.1) / 2,
      },
      inner: {
        width: circleSize,
        height: circleSize,
        borderRadius: circleSize / 2,
      },
    }),
    [screenWidth, circleSize],
  );

  const logoStyles = useMemo(
    () => ({
      width: circleSize * 0.8,
      height: circleSize * 0.8,
      top: -circleSize * 0.25,
    }),
    [circleSize],
  );

  const brandTextStyles = useMemo(
    () => ({
      bottom: -circleSize * 0.1,
    }),
    [circleSize],
  );

  const brandFontSize = useMemo(
    () => Math.min(Math.max(screenWidth * 0.15, 24), 60),
    [screenWidth],
  );

  const containerStyle = useMemo(
    () => [styles.container, { transform: [{ translateX: slideAnim }] }],
    [slideAnim],
  );

  const logoContainerStyle = useMemo(
    () => [
      styles.logoContainer,
      {
        opacity: fadeAnim,
        transform: [{ scale: scaleAnim }],
      },
    ],
    [fadeAnim, scaleAnim],
  );

  return (
    <Animated.View style={containerStyle}>
      <View style={styles.gradientBackground} />

      <Animated.View style={logoContainerStyle}>
        <View style={styles.circleWrapper}>
          <View style={[styles.circleOuter, circleStyles.outer]} />
          <View style={[styles.circleMiddle, circleStyles.middle]} />
          <View style={[styles.circleInner, circleStyles.inner]}>
            <Image
              source={Logo}
              style={[styles.logo, logoStyles]}
              resizeMode="contain"
            />
            <View style={[styles.brandText, brandTextStyles]}>
              <AppText style={[styles.brand, { fontSize: brandFontSize }]}>
                CPOST
              </AppText>
            </View>
          </View>
        </View>
      </Animated.View>
    </Animated.View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    position: 'absolute',
    width: '100%',
    height: '100%',
    zIndex: 1000,
  },
  gradientBackground: {
    ...StyleSheet.absoluteFillObject,
    backgroundColor: '#27A7E8',
  },
  logoContainer: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  circleWrapper: {
    justifyContent: 'center',
    alignItems: 'center',
  },
  circleOuter: {
    backgroundColor: 'rgba(255, 255, 255, 0.1)',
    position: 'absolute',
  },
  circleMiddle: {
    backgroundColor: 'rgba(255, 255, 255, 0.1)',
    position: 'absolute',
  },
  circleInner: {
    backgroundColor: 'rgba(255, 255, 255, 0.1)',
    justifyContent: 'center',
    alignItems: 'center',
    position: 'relative',
    overflow: 'visible',
  },
  logo: {
    position: 'absolute',
    alignSelf: 'center',
  },
  brandText: {
    position: 'absolute',
    width: '100%',
    alignItems: 'center',
  },
  brand: {
    fontWeight: '500',
    color: '#fff',
  },
});

export default SplashScreen;
