import React, { FC } from 'react';
import { Animated, TouchableOpacity } from 'react-native';
import { Carousel } from '@momo-kits/carousel';
import { Image, Pagination, Spacing } from '@momo-kits/foundation';
import { HeaderSliderBannerProps } from './types';
import styles from './styles';

/**
 * Full-width image carousel for a screen header. Driven by the scroll offset
 * `animatedValue`: zooms on pull-down, fades and lifts on scroll-up.
 */
const HeaderSliderBanner: FC<HeaderSliderBannerProps> = ({
  animatedValue = new Animated.Value(0),
  paginationBottom = 56,
  data = [],
  activeIndex = 0,
  paginationProps,
  carouselProps,
  onSnapToItem,
  onPressItem,
  paginationType = 'black_white',
}) => {
  const scale = animatedValue.interpolate({
    inputRange: [-300, 0, 300],
    outputRange: [4, 1, 1],
    extrapolate: 'clamp',
  });
  const opacity = animatedValue.interpolate({
    inputRange: [0, 150, 300],
    outputRange: [1, 0.5, 0],
    extrapolate: 'clamp',
  });
  const translateY = animatedValue.interpolate({
    inputRange: [-300, 0],
    outputRange: [-80, -2],
    extrapolate: 'clamp',
  });

  const paginationOpacity = animatedValue.interpolate({
    inputRange: [-40, 0],
    outputRange: [0, 1],
  });

  const renderItem = (info: { item: any; index: number }) => {
    const { item, index } = info;
    return (
      <TouchableOpacity disabled={!onPressItem} onPress={onPressItem}>
        <Image
          style={styles.image}
          key={`Header banner ${item}_${index}`}
          source={{
            uri: item,
          }}
        />
      </TouchableOpacity>
    );
  };

  return (
    <Animated.View
      style={[
        styles.container,
        {
          opacity: opacity,
          transform: [{ scale }, { translateY }],
        },
      ]}
    >
      <Carousel
        {...carouselProps}
        full={true}
        data={data}
        onSnapToItem={onSnapToItem}
        renderItem={renderItem}
      />
      <Animated.View
        style={[
          styles.pagination,
          {
            bottom: paginationBottom - Spacing.S,
            opacity: paginationOpacity,
          },
        ]}
      >
        <Pagination
          {...paginationProps}
          activeIndex={activeIndex}
          dataLength={data.length}
          type={paginationType}
        />
      </Animated.View>
    </Animated.View>
  );
};

export default HeaderSliderBanner;
