import Styles from "@/constants/Styles";
import { apiGetDataHome } from "@/lib/api";
import { useAuthSession } from "@/providers/AuthProvider";
import { useTheme } from "@/providers/ThemeProvider";
import { useQuery } from "@tanstack/react-query";
import { router } from "expo-router";
import React from "react";
import { Dimensions, View } from "react-native";
import Carousel, { ICarouselInstance } from "react-native-reanimated-carousel";
import LabelStatus from "../labelStatus";
import PaperGridContent from "../paperGridContent";
import ProgressBar from "../progressBar";
import Skeleton from "../skeleton";
import Text from "../Text";

type Props = {
   id: string
   title: string
   desc: string
   status: number
   progress: number
   createdAt: string
}

export default function ProjectHome({ refreshing }: { refreshing: boolean }) {
   const { decryptToken, token } = useAuthSession()
   const ref = React.useRef<ICarouselInstance>(null);
   const width = Dimensions.get("window").width;
   const { colors } = useTheme();

   // TanStack Query for Projects data
   const { data: homeProjects = [], isLoading } = useQuery({
      queryKey: ['homeData', 'kegiatan'],
      queryFn: async () => {
         const hasil = await decryptToken(String(token?.current))
         const response = await apiGetDataHome({ cat: "kegiatan", user: hasil })
         return response.data as Props[]
      },
      enabled: !!token?.current,
      staleTime: 0,
   })

   return (
      <View style={[Styles.mb05]}>
         <Text style={[Styles.textDefaultSemiBold, Styles.mb10]}>Kegiatan Terupdate</Text>
         {
            isLoading ? (<Skeleton width={100} height={150} borderRadius={10} widthType="percent" />)
               :
               homeProjects.length > 0 ?
                  <Carousel
                     ref={ref}
                     style={{ width: "100%" }}
                     width={width * 0.8}
                     height={220}
                     data={homeProjects}
                     loop={false}
                     autoPlay={false}
                     autoPlayReverse={false}
                     pagingEnabled={true}
                     snapEnabled={true}
                     vertical={false}
                     renderItem={({ index }) => (
                        <PaperGridContent titleTail={1} content="carousel" onPress={() => { router.push(`/project/${homeProjects[index].id}`) }} title={homeProjects[index].title} headerColor="primary">
                           <ProgressBar value={homeProjects[index].progress} category="carousel" />
                           <View style={[Styles.rowSpaceBetween]}>
                              <Text style={[Styles.textDefault, { color: colors.dimmed }]}>{homeProjects[index].createdAt}</Text>
                              <LabelStatus
                                 size="default"
                                 category={
                                    homeProjects[index].status === 0 ? 'secondary' :
                                       homeProjects[index].status === 1 ? 'warning' :
                                          homeProjects[index].status === 2 ? 'success' :
                                             homeProjects[index].status === 3 ? 'error' :
                                                'secondary'
                                 }
                                 text={
                                    homeProjects[index].status === 0 ? 'SEGERA' :
                                       homeProjects[index].status === 1 ? 'DIKERJAKAN' :
                                          homeProjects[index].status === 2 ? 'SELESAI' :
                                             homeProjects[index].status === 3 ? 'DIBATALKAN' :
                                                'SEGERA'
                                 }
                              />
                           </View>
                        </PaperGridContent>
                     )}
                  />
                  :
                  <Text style={[Styles.textDefault, { textAlign: 'center', color: colors.dimmed }]}>Tidak ada data</Text>
         }

      </View>
   )
}