import Styles from "@/constants/Styles"
import { apiGetDivisionOneFeature } from "@/lib/api"
import { useAuthSession } from "@/providers/AuthProvider"
import { AntDesign, MaterialCommunityIcons } from "@expo/vector-icons"
import { router, useLocalSearchParams } from "expo-router"
import { useEffect, useState } from "react"
import { View } from "react-native"
import { useSelector } from "react-redux"
import ButtonMenuHeader from "../buttonMenuHeader"
import DrawerBottom from "../drawerBottom"
import MenuItemRow from "../menuItemRow"

export default function HeaderRightCalendarList() {
   const [isVisible, setVisible] = useState(false)
   const { token, decryptToken } = useAuthSession()
   const { id } = useLocalSearchParams<{ id: string }>();
   const entityUser = useSelector((state: any) => state.user);
   const [isMemberDivision, setIsMemberDivision] = useState(false);

   async function handleCheckMember() {
      try {
         const hasil = await decryptToken(String(token?.current));
         const response = await apiGetDivisionOneFeature({
            id,
            user: hasil,
            cat: "check-member",
         });

         setIsMemberDivision(response.data);
      } catch (error) {
         console.error(error);
      }
   }

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


   return (
      <>
         <ButtonMenuHeader onPress={() => { setVisible(true) }} />
         <DrawerBottom animation="slide" isVisible={isVisible} setVisible={setVisible} title="Menu">
            <View style={Styles.rowItemsCenter}>
               {
                  (entityUser.role == "user" || entityUser.role == "coadmin") && !isMemberDivision ? (
                     <></>
                  ) : (
                     <MenuItemRow
                        icon={<AntDesign name="pluscircle" color="black" size={25} />}
                        title="Tambah Acara"
                        onPress={() => {
                           setVisible(false)
                           router.push('./calendar/create')
                        }}
                     />
                  )
               }
               <MenuItemRow
                  icon={<MaterialCommunityIcons name="history" color="black" size={25} />}
                  title="Riwayat"
                  onPress={() => {
                     setVisible(false)
                     router.push('./calendar/history')
                  }}
               />
            </View>
         </DrawerBottom>
      </>
   )
}