import Styles from "@/constants/Styles"
import { apiDeleteUser } from "@/lib/api"
import { setUpdateMember } from "@/lib/memberSlice"
import { useAuthSession } from "@/providers/AuthProvider"
import { useTheme } from "@/providers/ThemeProvider"
import { MaterialCommunityIcons } from "@expo/vector-icons"
import { router } from "expo-router"
import { useState } from "react"
import { View } from "react-native"
import Toast from "react-native-toast-message"
import { useDispatch, useSelector } from "react-redux"
import ModalConfirmation from "../ModalConfirmation"
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)
   const update = useSelector((state: any) => state.memberUpdate)
   const [showModal, setShowModal] = useState(false)
   const { colors } = useTheme();
   const dispatch = useDispatch()

   async function handleActive() {
      try {
         const hasil = await decryptToken(String(token?.current))
         const response = await apiDeleteUser({ user: hasil, isActive: active }, id)
         if (response.success) {
            Toast.show({ type: 'small', text1: 'Berhasil mengupdate data', })
            dispatch(setUpdateMember(!update))
         } else {
            Toast.show({ type: 'small', text1: response.message, })
         }
      } catch (error : any ) {
         console.error(error);
         const message = error?.response?.data?.message || "Gagal mengupdate data"

         Toast.show({ type: 'small', text1: message })
      } finally {
         setVisible(false)
      }

   }

   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={colors.text} size={25} />}
                  title={active ? "Non Aktifkan" : "Aktifkan"}
                  onPress={() => {
                     setVisible(false)
                     setTimeout(() => {
                        setShowModal(true)
                     }, 600)
                  }}
               />
               <MenuItemRow
                  icon={<MaterialCommunityIcons name="pencil-outline" color={colors.text} size={25} />}
                  title="Edit"
                  onPress={() => {
                     setVisible(false)
                     router.push(`/member/edit/${id}`)
                  }}
               />
            </View>
         </DrawerBottom>

         <ModalConfirmation
            visible={showModal}
            title="Konfirmasi"
            message={active ? 'Apakah anda yakin ingin menonaktifkan user?' : 'Apakah anda yakin ingin mengaktifkan user?'}
            onConfirm={() => {
               setShowModal(false)
               handleActive()
            }}
            onCancel={() => setShowModal(false)}
            confirmText="Konfirmasi"
            cancelText="Batal"
         />
      </>
   )
}