import Styles from "@/constants/Styles"
import { apiDeleteCalendar } from "@/lib/api"
import { setUpdateCalendar } from "@/lib/calendarUpdate"
import { useAuthSession } from "@/providers/AuthProvider"
import { useTheme } from "@/providers/ThemeProvider"
import { Ionicons, MaterialCommunityIcons, MaterialIcons } 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 = {
   id: string | string[],
   idReminder?: string
}

export default function HeaderRightCalendarDetail({ id, idReminder }: Props) {
   const { colors } = useTheme()
   const [isVisible, setVisible] = useState(false)
   const { token, decryptToken } = useAuthSession()
   const [showDeleteModal, setShowDeleteModal] = useState(false)
   const update = useSelector((state: any) => state.calendarUpdate)
   const dispatch = useDispatch()

   async function handleDeleteCalendar() {
      try {
         const hasil = await decryptToken(String(token?.current));
         const response = await apiDeleteCalendar({ user: hasil }, String(id));
         if (response.success) {
            dispatch(setUpdateCalendar({ ...update, data: !update.data }));
            Toast.show({ type: 'small', text1: response.message, })
            router.back()
         } else {
            Toast.show({ type: 'small', text1: response.message, })
         }
      } catch (error : any ) {
         console.error(error);
         const message = error?.response?.data?.message || "Gagal menghapus 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="account-group-outline" color={colors.text} size={25} />}
                  title="Tambah Anggota"
                  onPress={() => {
                     setVisible(false)
                     router.push(`./${idReminder}/add-member`)
                  }}
               />
               <MenuItemRow
                  icon={<MaterialCommunityIcons name="pencil-outline" color={colors.text} size={25} />}
                  title="Edit"
                  onPress={() => {
                     setVisible(false)
                     router.push(`./${idReminder}/edit`)
                  }}
               />
               <MenuItemRow
                  icon={<Ionicons name="trash-outline" color={colors.text} size={25} />}
                  title="Hapus"
                  onPress={() => {
                     setVisible(false)
                     setTimeout(() => {
                        setShowDeleteModal(true)
                     }, 600)
                  }}
               />
            </View>
         </DrawerBottom>

         <ModalConfirmation
            visible={showDeleteModal}
            title="Konfirmasi"
            message="Apakah anda yakin ingin menghapus data ini? Data ini akan mempengaruhi semua data yang terkait"
            onConfirm={() => {
               setShowDeleteModal(false)
               handleDeleteCalendar()
            }}
            onCancel={() => setShowDeleteModal(false)}
            confirmText="Hapus"
            cancelText="Batal"
         />
      </>
   )
}