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 AlertKonfirmasi from "../alertKonfirmasi"
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 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) {
         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={<MaterialIcons name="groups" 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" color={colors.text} size={25} />}
                  title="Hapus"
                  onPress={() => {
                     setVisible(false)
                     AlertKonfirmasi({
                        title: 'Konfirmasi',
                        desc: 'Apakah anda yakin ingin menghapus data ini? Data ini akan mempengaruhi semua data yang terkait',
                        onPress: () => {
                           handleDeleteCalendar()
                        }
                     })
                  }}
               />
            </View>
         </DrawerBottom>
      </>
   )
}