import Styles from "@/constants/Styles"
import { apiDeleteUser } from "@/lib/api"
import { useAuthSession } from "@/providers/AuthProvider"
import { MaterialCommunityIcons } from "@expo/vector-icons"
import { router } from "expo-router"
import { useState } from "react"
import { ToastAndroid, View } from "react-native"
import AlertKonfirmasi from "../alertKonfirmasi"
import ButtonMenuHeader from "../buttonMenuHeader"
import DrawerBottom from "../drawerBottom"
import MenuItemRow from "../menuItemRow"

type Props = {
   active: any,
   id: string
}

export default function HeaderRightMemberDetail({ active, id }: Props) {
   const { token, decryptToken } = useAuthSession()
   const [isVisible, setVisible] = useState(false)

   async function handleActive() {
      try {
         const hasil = await decryptToken(String(token?.current))
         const response = await apiDeleteUser({ user: hasil, isActive: active }, id)
      } catch (error) {
         console.error(error)
      } finally {
         setVisible(false)
         ToastAndroid.show('Berhasil mengupdate data', ToastAndroid.SHORT)
         router.replace(`/member/${id}`);
      }

   }

   return (
      <>
         <ButtonMenuHeader onPress={() => { setVisible(true) }} />
         <DrawerBottom animation="slide" isVisible={isVisible} setVisible={setVisible} title="Menu">
            <View style={Styles.rowItemsCenter}>
               <MenuItemRow
                  icon={<MaterialCommunityIcons name="toggle-switch-off-outline" color="black" size={25} />}
                  title={active ? "Non Aktifkan" : "Aktifkan"}
                  onPress={() => {
                     AlertKonfirmasi({
                        title: 'Konfirmasi',
                        desc: active ? 'Apakah anda yakin ingin menonaktifkan user?' : 'Apakah anda yakin ingin mengaktifkan user?',
                        onPress: () => {
                           handleActive()
                        }
                     })
                  }}
               />
               <MenuItemRow
                  icon={<MaterialCommunityIcons name="pencil-outline" color="black" size={25} />}
                  title="Edit"
                  onPress={() => {
                     setVisible(false)
                     router.push(`/member/edit/${id}`)
                  }}
               />
            </View>
         </DrawerBottom>
      </>
   )
}