import Styles from "@/constants/Styles";
import { Feather } from "@expo/vector-icons";
import React, { useEffect, useState } from "react";
import { Dimensions, Text, View } from "react-native";
import Carousel, { ICarouselInstance } from "react-native-reanimated-carousel";
import { useAuthSession } from "@/providers/AuthProvider";
import { useLocalSearchParams } from "expo-router";
import { apiGetDivisionOneFeature } from "@/lib/api";

type Props = {
   id: string
   name: string
   extension: string
   path: string
   share: boolean
}

export default function FileDivisionDetail() {
   const ref = React.useRef<ICarouselInstance>(null);
   const width = Dimensions.get("window").width;
   const [data, setData] = useState<Props[]>([])
   const { token, decryptToken } = useAuthSession()
   const { id } = useLocalSearchParams<{ id: string }>()

   async function handleLoad() {
      try {
         const hasil = await decryptToken(String(token?.current))
         const response = await apiGetDivisionOneFeature({ user: hasil, id, cat: 'new-file' })
         setData(response.data)
      } catch (error) {
         console.error(error)
      }
   }

   useEffect(() => {
      handleLoad()
   }, [])

   return (
      <View style={[Styles.mb15]}>
         <Text style={[Styles.textDefaultSemiBold, Styles.mv10]}>Dokumen Terkini</Text>
         {
            data.length > 0 ?
               <Carousel
                  ref={ref}
                  width={width}
                  height={115}
                  data={data}
                  loop={true}
                  autoPlay={false}
                  autoPlayReverse={false}
                  pagingEnabled={true}
                  snapEnabled={true}
                  vertical={false}
                  style={{
                     width: width,
                  }}
                  mode="parallax"
                  modeConfig={{
                     parallaxScrollingScale: 1,
                     parallaxScrollingOffset: 280,
                  }}
                  renderItem={({ index }) => (
                     <View style={{ margin: 'auto', width: '28%' }}>
                        <View style={{ alignItems: 'center' }}>
                           <View style={[Styles.wrapPaper, { alignItems: 'center' }]}>
                              <Feather name="file-text" size={50} color="black" style={Styles.mr05} />
                           </View>
                        </View>
                        <Text style={[Styles.textMediumNormal, { textAlign: 'center' }]} numberOfLines={1}>{data[index].name}.{data[index].extension}</Text>
                     </View>

                  )}
               />
               :
               <Text style={[Styles.textDefault, Styles.cGray, { textAlign: 'center' }]}>Tidak ada file</Text>
         }
      </View>
   )
}