import Styles from "@/constants/Styles";
import { apiGetDivisionOneFeature } from "@/lib/api";
import { useAuthSession } from "@/providers/AuthProvider";
import { useTheme } from "@/providers/ThemeProvider";
import { Feather, MaterialCommunityIcons } 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 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.mb10, { backgroundColor: colors.card, borderWidth: 1, borderColor: colors.icon + '20' }]} 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.dimmed} style={Styles.mr05} />
                           <Text style={[Styles.textInformation, { color: colors.dimmed }]} 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>
                  //    )}
                  // />
                  :
                  <View style={{ alignItems: 'center', paddingVertical: 16, gap: 6, opacity: 0.5 }}>
                     <MaterialCommunityIcons name="clipboard-check-outline" size={28} color={colors.dimmed} />
                     <Text style={[Styles.textDefault, { color: colors.dimmed }]}>Tidak ada tugas hari ini</Text>
                  </View>
         }
      </View>
   )
}