import type React from 'react'
import { StyleSheet, View } from 'react-native'
import Animated, { css } from 'react-native-reanimated'
import { useTheme } from 'tamagui'

const shimmerKeyframes = css.keyframes({
  '0%': { opacity: 0.3, transform: [{ translateX: -100 }] },
  '50%': { opacity: 0.7 },
  '100%': { opacity: 0.3, transform: [{ translateX: 100 }] },
})

interface SkeletonItemProps {
  backgroundColor: string
  shimmerBackgroundColor: string
}

const SkeletonItem: React.FC<SkeletonItemProps> = ({
  backgroundColor,
  shimmerBackgroundColor,
}) => {
  return (
    <View style={styles.skeletonItem}>
      <View style={styles.avatarContainer}>
        <View style={[styles.avatarSkeleton, { backgroundColor }]}>
          <Animated.View
            style={[
              styles.shimmer,
              {
                backgroundColor: shimmerBackgroundColor,
                animationName: shimmerKeyframes,
                animationDuration: '1.5s',
                animationIterationCount: 'infinite',
              },
            ]}
          />
        </View>
      </View>

      <View style={[styles.usernameSkeleton, { backgroundColor }]}>
        <Animated.View
          style={[
            styles.shimmer,
            styles.usernameShimmer,
            {
              backgroundColor: shimmerBackgroundColor,
              animationName: shimmerKeyframes,
              animationDuration: '1.5s',
              animationIterationCount: 'infinite',
            },
          ]}
        />
      </View>
    </View>
  )
}

const skeletonData = Array.from({ length: 6 }, (_, index) => index)

const StorySkeletonLoader: React.FC = () => {
  const theme = useTheme()

  return (
    <View style={styles.container}>
      <View style={styles.listContainer}>
        {skeletonData.map((id) => (
          <SkeletonItem
            key={id}
            backgroundColor={theme.colorTertiary.val}
            shimmerBackgroundColor={theme.color.val}
          />
        ))}
      </View>
    </View>
  )
}

const styles = StyleSheet.create({
  container: {
    paddingVertical: 16,
  },
  listContainer: {
    paddingHorizontal: 16,
    flexDirection: 'row',
  },
  skeletonItem: {
    alignItems: 'center',
    marginRight: 16,
    width: 80,
  },
  avatarContainer: {
    marginBottom: 8,
  },
  avatarSkeleton: {
    width: 80,
    height: 80,
    borderRadius: 40,
    overflow: 'hidden',
    position: 'relative',
  },
  usernameSkeleton: {
    width: 60,
    height: 12,
    borderRadius: 6,
    overflow: 'hidden',
    position: 'relative',
  },
  shimmer: {
    position: 'absolute',
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
  },
  usernameShimmer: {
    borderRadius: 6,
  },
})

export default StorySkeletonLoader
