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

type Props = {
   id: string
   idProject: string
   title: string
   dateStart: string
   dateEnd: string
   projectTitle: string
}

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

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

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

   return (
      <View>
         <Text style={[Styles.textDefaultSemiBold, Styles.mb05]}>Tugas Hari Ini</Text>
         {
            data.length > 0 ?
               <Carousel
                  ref={ref}
                  style={{ width: "100%" }}
                  width={width * 0.8}
                  height={100}
                  data={data}
                  loop={true}
                  autoPlay={false}
                  autoPlayReverse={false}
                  pagingEnabled={true}
                  snapEnabled={true}
                  vertical={false}
                  renderItem={({ index }) => (
                     <View style={[Styles.wrapPaper, { width: '95%' }]}>
                        <Text style={[Styles.textDefaultSemiBold]} numberOfLines={1} ellipsizeMode="tail">{data[index].title} - {data[index].projectTitle}</Text>
                        <View style={[Styles.rowItemsCenter, Styles.mt10]}>
                           <Feather name="clock" size={18} color="grey" style={Styles.mr05} />
                           <Text style={[Styles.textInformation]} numberOfLines={1} ellipsizeMode="tail">{data[index].dateStart} - {data[index].dateEnd}</Text>
                        </View>
                     </View>
                  )}
               />
               :
               <Text style={[Styles.textDefault, Styles.cGray, { textAlign: 'center' }]}>Tidak ada tugas</Text>
         }
      </View>
   )
}