import Styles from "@/constants/Styles";
import { apiGetDataHome } from "@/lib/api";
import { useAuthSession } from "@/providers/AuthProvider";
import { useTheme } from "@/providers/ThemeProvider";
import { router } from "expo-router";
import React, { useEffect, useState } 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 [data, setData] = useState<Props[]>([])
   const [loading, setLoading] = useState(true)
   const { colors } = useTheme();

   async function handleData(loading: boolean) {
      try {
         setLoading(loading)
         const hasil = await decryptToken(String(token?.current))
         const response = await apiGetDataHome({ cat: "kegiatan", user: hasil })
         setData(response.data)
      } catch (error) {
         console.error(error)
      } finally {
         setLoading(false)
      }
   }

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

   useEffect(() => {
      handleData(true)
   }, []);

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

      </View>
   )
}