import { ColorsStatus } from "@/constants/ColorsStatus"
import Styles from "@/constants/Styles"
import { apiGetDivisionOneFeature } from "@/lib/api"
import { useAuthSession } from "@/providers/AuthProvider"
import { AntDesign, Feather, MaterialIcons, SimpleLineIcons } from "@expo/vector-icons"
import { router, useLocalSearchParams } from "expo-router"
import { useEffect, useState } from "react"
import { View } from "react-native"
import { useTheme } from "@/providers/ThemeProvider"
import Text from "../Text"
import FiturGridItem from "./FiturGridItem"

type Props = {
   tugas: number
   dokumen: number
   diskusi: number
   kalender: number
}

export default function FiturDivisionDetail({ refreshing }: { refreshing: boolean }) {
   const { colors } = useTheme();
   const { token, decryptToken } = useAuthSession()
   const { id } = useLocalSearchParams<{ id: string }>()
   const [data, setData] = useState<Props>({
      tugas: 0,
      dokumen: 0,
      diskusi: 0,
      kalender: 0,
   })

   async function handleLoad() {
      try {
         const hasil = await decryptToken(String(token?.current))
         const response = await apiGetDivisionOneFeature({ user: hasil, id, cat: 'jumlah' })
         setData(response.data)
      } catch (error) {
         console.error(error)
      }
   }

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

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

   return (
      <View style={[Styles.mb15]}>
         <Text style={[Styles.textDefaultSemiBold, Styles.mv05]}>Fitur</Text>
         <View style={{ flexDirection: 'row', flexWrap: 'wrap', justifyContent: 'space-between' }}>
            <FiturGridItem
               title="Tugas"
               subtitle={`${data.tugas} Tugas`}
               icon={<AntDesign name="filetext1" size={28} color={colors.text} />}
               onPress={() => { router.push(`/division/${id}/task?status=0`) }}
            />

            <FiturGridItem
               title="Dokumen"
               subtitle={`${data.dokumen} File`}
               icon={<Feather name="paperclip" size={28} color={colors.text} />}
               onPress={() => { router.push(`/division/${id}/document`) }}
            />

            <FiturGridItem
               title="Diskusi"
               subtitle={`${data.diskusi} Diskusi`}
               icon={<SimpleLineIcons name="bubbles" size={28} color={colors.text} />}
               onPress={() => { router.push(`/division/${id}/discussion?active=true`) }}
            />

            <FiturGridItem
               title="Kalender"
               subtitle={`${data.kalender} Acara`}
               icon={<AntDesign name="calendar" size={28} color={colors.text} />}
               onPress={() => { router.push(`/division/${id}/calendar`) }}
            />
         </View>
      </View>
   )
}