import Styles from "@/constants/Styles"
import { apiUpdateStatusDivision } from "@/lib/api"
import { setUpdateDivision } from "@/lib/divisionUpdate"
import { useAuthSession } from "@/providers/AuthProvider"
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"
import { useTheme } from "@/providers/ThemeProvider"

type Props = {
   id: string | string[]
   active: boolean | undefined
}

export default function HeaderRightDivisionInfo({ id, active }: Props) {
   const { colors } = useTheme();
   const [isVisible, setVisible] = useState(false)
   const { token, decryptToken } = useAuthSession()
   const [showModal, setShowModal] = useState(false)
   const update = useSelector((state: any) => state.divisionUpdate)
   const dispatch = useDispatch()

   async function handleUpdateStatus() {
      try {
         const hasil = await decryptToken(String(token?.current))
         const response = await apiUpdateStatusDivision({ data: { user: hasil, isActive: Boolean(active) }, id: String(id) })
         if (response.success) {
            dispatch(setUpdateDivision(!update))
            Toast.show({ type: 'small', text1: 'Berhasil mengubah status', })
         } else {
            Toast.show({ type: 'small', text1: response.message, })
         }
      } catch (error) {
         console.error(error)
         Toast.show({ type: 'small', text1: 'Terjadi kesalahan', })
      } 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="pencil-outline" color={colors.text} size={25} />}
                  title="Edit Divisi"
                  onPress={() => {
                     setVisible(false)
                     router.push(`/division/${id}/edit`)
                  }}
               />
               <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)
                  }}
               />
            </View>
         </DrawerBottom>

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