import Styles from "@/constants/Styles"
import { apiDeleteDiscussionGeneral, apiUpdateStatusDiscussionGeneral } from "@/lib/api"
import { setUpdateDiscussionGeneralDetail } from "@/lib/discussionGeneralDetail"
import { useAuthSession } from "@/providers/AuthProvider"
import { 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
   active: boolean
   status: number
}

export default function HeaderRightDiscussionGeneralDetail({ id, active, status }: Props) {
   const { token, decryptToken } = useAuthSession()
   const [isVisible, setVisible] = useState(false)
   const entityUser = useSelector((state: any) => state.user)
   const dispatch = useDispatch()
   const update = useSelector((state: any) => state.discussionGeneralDetailUpdate)

   async function handleUpdateStatus() {
      try {
         const hasil = await decryptToken(String(token?.current))
         const response = await apiUpdateStatusDiscussionGeneral({ id: id, data: { status: status, user: hasil } })
         if (response.success) {
            Toast.show({ type: 'small', text1: 'Berhasil mengupdate data', })
            dispatch(setUpdateDiscussionGeneralDetail(!update))
         }
         setVisible(false)
      } catch (error) {
         console.error(error)
      }
   }

   async function handleDelete() {
      try {
         const hasil = await decryptToken(String(token?.current))
         const response = await apiDeleteDiscussionGeneral({ active: !active, user: hasil }, id)
         Toast.show({ type: 'small', text1: 'Berhasil mengupdate data', })
         dispatch(setUpdateDiscussionGeneralDetail(!update))
      } catch (error) {
         console.error(error)
      } 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="black" size={25} />}
                  title="Anggota"
                  onPress={() => {
                     setVisible(false)
                     router.push(`/discussion/member/${id}`)
                  }}
               />
               {
                  entityUser.role != "user" && entityUser.role != "coadmin" &&
                  <>
                     {
                        active ?
                           (
                              <>
                                 <MenuItemRow
                                    icon={<MaterialCommunityIcons name="pencil-outline" color="black" size={25} />}
                                    title="Edit"
                                    onPress={() => {
                                       setVisible(false)
                                       router.push(`/discussion/edit/${id}`)
                                    }}
                                 />
                                 <MenuItemRow
                                    icon={<MaterialIcons name="close" color="black" size={25} />}
                                    title={status == 1 ? 'Tutup Diskusi' : 'Buka Diskusi'}
                                    onPress={() => {
                                       setVisible(false)
                                       AlertKonfirmasi({
                                          title: 'Konfirmasi',
                                          desc: status == 1 ? 'Apakah anda yakin ingin menutup diskusi?' : 'Apakah anda yakin ingin membuka diskusi?',
                                          onPress: () => {
                                             handleUpdateStatus()
                                          }
                                       })
                                    }}
                                 />
                              </>
                           )
                           :
                           <MenuItemRow
                              icon={<MaterialCommunityIcons name="archive-outline" color="black" size={25} />}
                              title="Aktifkan Diskusi"
                              onPress={() => {
                                 setVisible(false)
                                 AlertKonfirmasi({
                                    title: 'Konfirmasi',
                                    desc: 'Apakah anda yakin ingin mengaktifkan diskusi ini?',
                                    onPress: () => { handleDelete() }
                                 })
                              }}
                           />

                     }
                  </>
               }
            </View>
            {
               entityUser.role != "user" && entityUser.role != "coadmin" && active &&
               <View style={[Styles.rowItemsCenter, Styles.mt15]}>
                  <MenuItemRow
                     icon={<MaterialCommunityIcons name="archive-outline" color="black" size={25} />}
                     title="Arsipkan"
                     onPress={() => {
                        setVisible(false)
                        AlertKonfirmasi({
                           title: 'Konfirmasi',
                           desc: 'Apakah anda yakin ingin mengarsipkan diskusi?',
                           onPress: () => { handleDelete() }
                        })
                     }}
                  />
               </View>
            }
         </DrawerBottom>
      </>
   )
}