import Styles from "@/constants/Styles"
import { apiCreateGroup } from "@/lib/api"
import { setUpdateGroup } from "@/lib/groupSlice"
import { useAuthSession } from "@/providers/AuthProvider"
import { useTheme } from "@/providers/ThemeProvider"
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 { colors } = useTheme()
   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((prev) => ({ ...prev, title: true }))
         } else {
            setError((prev) => ({ ...prev, title: false }))
         }
      }
   }


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

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

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