import Styles from "@/constants/Styles"
import { apiCreateGroup } from "@/lib/api"
import { setUpdateGroup } from "@/lib/groupSlice"
import { useAuthSession } from "@/providers/AuthProvider"
import { AntDesign } from "@expo/vector-icons"
import { useState } from "react"
import { View } from "react-native"
import Toast from "react-native-toast-message"
import { useDispatch, useSelector } from "react-redux"
import { ButtonForm } from "../buttonForm"
import ButtonMenuHeader from "../buttonMenuHeader"
import DrawerBottom from "../drawerBottom"
import { InputForm } from "../inputForm"
import MenuItemRow from "../menuItemRow"

export default function HeaderRightGroupList() {
   const dispatch = useDispatch()
   const update = useSelector((state: any) => state.groupUpdate)
   const { token, decryptToken } = useAuthSession()
   const [isVisible, setVisible] = useState(false)
   const [isVisibleTambah, setVisibleTambah] = useState(false)
   const [title, setTitle] = useState('')
   const [error, setError] = useState({
      title: false,
   });

   async function handleTambah() {
      try {
         const hasil = await decryptToken(String(token?.current))
         const response = await apiCreateGroup({ user: hasil, name: title })
         dispatch(setUpdateGroup(!update))
      } catch (error) {
         console.error(error)
      } finally {
         setVisibleTambah(false)
         setVisible(false)
         Toast.show({ type: 'small', text1: 'Berhasil menambahkan data', })
      }
   }

   function onCheck() {
      if (Object.values(error).some((v) => v == true))
         return false
      handleTambah()
   }

   function validationForm(val: any, cat: 'title') {
      if (cat === 'title') {
         setTitle(val)
         if (val == "" || val.length < 3) {
            setError({ ...error, title: true })
         } else {
            setError({ ...error, title: false })
         }
      }
   }


   return (
      <>
         <ButtonMenuHeader onPress={() => { setVisible(true) }} />
         <DrawerBottom animation="slide" isVisible={isVisible} setVisible={setVisible} title="Menu">
            <View style={Styles.rowItemsCenter}>
               <MenuItemRow
                  icon={<AntDesign name="pluscircle" color="black" size={25} />}
                  title="Tambah Lembaga"
                  onPress={() => {
                     setVisible(false);
                     setTimeout(
                        () => {
                           setVisibleTambah(true)
                        },
                        100,
                     );

                  }}
               />
            </View>
         </DrawerBottom>

         <DrawerBottom animation="none" height={30} isVisible={isVisibleTambah} setVisible={setVisibleTambah} title="Tambah Lembaga Desa">
            <View style={{ flex: 1 }}>
               <View>
                  <InputForm
                     type="default"
                     placeholder="Nama Lembaga Desa"
                     required label="Lembaga Desa"
                     error={error.title}
                     errorText="Lembaga Desa tidak boleh kosong & minimal 3 karakter"
                     onChange={(val) => { validationForm(val, 'title') }}
                  />
               </View>
               <View>
                  <ButtonForm text="SIMPAN" onPress={() => { onCheck() }} />
               </View>
            </View>
         </DrawerBottom>
      </>
   )
}