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

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

export default function TaskDivisionDetail({ refreshing }: { refreshing: boolean }) {
   const { colors } = useTheme();
   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;
   const [loading, setLoading] = useState(true)

   async function handleLoad(loading: boolean) {
      try {
         setLoading(loading)
         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)
      } finally {
         setLoading(false)
      }
   }

   useEffect(() => {
      if (refreshing)
         handleLoad(false)
   }, [refreshing])

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

   return (
      <View>
         <Text style={[Styles.textDefaultSemiBold, Styles.mb05]}>Tugas Hari Ini</Text>
         {
            loading ?
               <Skeleton width={100} widthType="percent" height={60} borderRadius={10} />
               :
               data.length > 0 ?
                  data.map((item, index) => (
                     <Pressable key={index} style={[Styles.wrapPaper, Styles.mb05, { backgroundColor: colors.card, borderColor: colors.background }]} onPress={() => { router.push(`/division/${id}/task/${item.idProject}`) }}>
                        <Text style={[Styles.textDefaultSemiBold]} numberOfLines={1} ellipsizeMode="tail">{item.title} - {item.projectTitle}</Text>
                        <View style={[Styles.rowItemsCenter, Styles.mt10]}>
                           <Feather name="clock" size={18} color={colors.text} style={Styles.mr05} />
                           <Text style={[Styles.textInformation]} numberOfLines={1} ellipsizeMode="tail">{item.dateStart} - {item.dateEnd}</Text>
                        </View>
                     </Pressable>
                  ))
                  // <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>
   )
}