import Styles from "@/constants/Styles";
import { apiGetDataHome } from "@/lib/api";
import { useAuthSession } from "@/providers/AuthProvider";
import { useTheme } from "@/providers/ThemeProvider";
import { Feather } from "@expo/vector-icons";
import { router } 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
   name: string
   jumlah: number
}

export default function DivisionHome({ refreshing }: { refreshing: boolean }) {
   const { decryptToken, token } = useAuthSession()
   const { colors } = useTheme();
   const ref = React.useRef<ICarouselInstance>(null)
   const width = Dimensions.get("window").width
   const [data, setData] = useState<Props[]>([])
   const [loading, setLoading] = useState(true)
   const arrSkeleton = Array.from({ length: 2 }, (_, index) => index)

   async function handleData(loading: boolean) {
      try {
         setLoading(loading)
         const hasil = await decryptToken(String(token?.current))
         const response = await apiGetDataHome({ cat: "division", 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.mb15]}>
         <Text style={[Styles.textDefaultSemiBold, Styles.mb10]}>Divisi Teraktif</Text>
         {
            loading ?
               arrSkeleton.map((item, index) => (
                  <Skeleton key={index} width={100} height={80} borderRadius={10} widthType="percent" />
               ))
               :
               data.length > 0 ?
                  data.map((item, index) => (
                     <Pressable style={[Styles.wrapPaper, Styles.mb05, { backgroundColor: colors.card, borderColor: colors.icon + '20' }]} key={index} onPress={() => { router.push(`/division/${item.id}`) }}>
                        <View style={[Styles.rowSpaceBetween, { alignItems: 'center' }]}>
                           <View>
                              <Text style={[Styles.textDefaultSemiBold]}>{item.name}</Text>
                              <Text style={[Styles.textDefault]}>{item.jumlah} Kegiatan</Text>
                           </View>
                           <Feather name="chevron-right" size={20} color={colors.text} />
                        </View>
                     </Pressable>
                  ))
                  // <Carousel
                  //    ref={ref}
                  //    style={{ width: "100%" }}
                  //    width={width * 0.8}
                  //    height={235}
                  //    data={data}
                  //    loop={true}
                  //    autoPlay={false}
                  //    autoPlayReverse={false}
                  //    pagingEnabled={true}
                  //    snapEnabled={true}
                  //    vertical={false}
                  //    renderItem={({ index }) => (
                  //       <PaperGridContent titleTail={1} onPress={() => { router.push(`/division/${data[index].id}`) }} content="carousel" title={data[index].name} headerColor="warning">
                  //          <View>
                  //             <Text style={{ fontSize: 50, textAlign: "center", fontWeight: 'bold' }}>{data[index].jumlah}</Text>
                  //             <Text style={[Styles.textSubtitle, { textAlign: "center" }]}>KEGIATAN</Text>
                  //          </View>
                  //       </PaperGridContent>
                  //    )}
                  // />
                  :
                  <Text style={[Styles.textDefault, { textAlign: 'center', color: colors.dimmed }]}>Tidak ada data</Text>
         }
      </View>
   )
}