import GuideOverlay from "@/components/GuideOverlay";
import BorderBottomItem from "@/components/borderBottomItem";
import { ButtonForm } from "@/components/buttonForm";
import ButtonTab from "@/components/buttonTab";
import DrawerBottom from "@/components/drawerBottom";
import { InputForm } from "@/components/inputForm";
import InputSearch from "@/components/inputSearch";
import LabelStatus from "@/components/labelStatus";
import MenuItemRow from "@/components/menuItemRow";
import ModalConfirmation from "@/components/ModalConfirmation";
import SkeletonTwoItem from "@/components/skeletonTwoItem";
import Text from "@/components/Text";
import WrapTab from "@/components/wrapTab";
import Styles from "@/constants/Styles";
import { apiDeletePosition, apiEditPosition, apiGetPosition } from "@/lib/api";
import { setUpdatePosition } from "@/lib/positionSlice";
import { GUIDE_POSITION } from "@/lib/guideSteps";
import { useGuide } from "@/lib/useGuide";
import { useAuthSession } from "@/providers/AuthProvider";
import { useTheme } from "@/providers/ThemeProvider";
import { AntDesign, Feather, MaterialCommunityIcons } from "@expo/vector-icons";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { useLocalSearchParams } from "expo-router";
import { useEffect, useMemo, useState } from "react";
import { RefreshControl, View, VirtualizedList } from "react-native";
import Toast from "react-native-toast-message";
import { useDispatch, useSelector } from "react-redux";

type Props = {
   name: string;
   idGroup: string;
   group: string;
   id: string;
   isActive: boolean
}

export default function Index() {
   const { token, decryptToken } = useAuthSession()
   const { colors } = useTheme()
   const { active, group } = useLocalSearchParams<{ active?: string, group?: string }>()
   const [status, setStatus] = useState<'true' | 'false'>(active == 'false' ? 'false' : 'true')
   const entityUser = useSelector((state: any) => state.user)
   const [isModal, setModal] = useState(false)
   const [isVisibleEdit, setVisibleEdit] = useState(false)
   const [search, setSearch] = useState('')
   const [loadingSubmit, setLoadingSubmit] = useState(false)
   const [chooseData, setChooseData] = useState({ name: '', id: '', active: false, idGroup: '' })
   const [error, setError] = useState({
      name: false,
   });
   const queryClient = useQueryClient()
   const [refreshing, setRefreshing] = useState(false)
   const [showDeleteModal, setShowDeleteModal] = useState(false)
   const { visible: guideVisible, dismiss: dismissGuide } = useGuide('position')

   const dispatch = useDispatch()
   const update = useSelector((state: any) => state.positionUpdate)

   // TanStack Query for Positions
   const {
      data: queryData,
      isLoading,
      refetch
   } = useQuery({
      queryKey: ['positions', { status, search, group }],
      queryFn: async () => {
         const hasil = await decryptToken(String(token?.current))
         const response = await apiGetPosition({
            user: hasil,
            active: status,
            search: search,
            group: String(group)
         })
         return response;
      },
      enabled: !!token?.current,
      staleTime: 0,
   })

   const data = useMemo(() => queryData?.data || [], [queryData])
   const nameGroup = useMemo(() => queryData?.filter?.name || "", [queryData])

   useEffect(() => {
      refetch()
   }, [update, refetch])


   function handleChooseData(id: string, name: string, active: boolean, group: string) {
      setChooseData({ id, name, active, idGroup: group })
      setModal(true)
   }

   async function handleDelete() {
      try {
         const hasil = await decryptToken(String(token?.current))
         const response = await apiDeletePosition({ user: hasil, isActive: chooseData.active }, chooseData.id)
         dispatch(setUpdatePosition(!update))
      } catch (error: any) {
         console.error(error);
         const message = error?.response?.data?.message || "Gagal menghapus data"

         Toast.show({ type: 'small', text1: message })
      } finally {
         setModal(false)
         Toast.show({ type: 'small', text1: 'Berhasil mengupdate data', })
      }
   }


   async function handleEdit() {
      try {
         setLoadingSubmit(true)
         const hasil = await decryptToken(String(token?.current))
         const response = await apiEditPosition({ user: hasil, name: chooseData.name, idGroup: chooseData.idGroup }, chooseData.id)
         if (response.success) {
            dispatch(setUpdatePosition(!update))
         } else {
            Toast.show({ type: 'small', text1: response.message, })
         }
      } catch (error: any) {
         console.error(error);
         const message = error?.response?.data?.message || "Gagal mengubah data"

         Toast.show({ type: 'small', text1: message })
      } finally {
         setLoadingSubmit(false)
         setVisibleEdit(false)
         setModal(false)
      }

   }

   function validationForm(value: string) {
      setChooseData({ ...chooseData, name: value })
      if (value == "") {
         setError({ ...error, name: true })
      } else {
         setError({ ...error, name: false })
      }
   }

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

   const arrSkeleton = [0, 1, 2, 3, 4]

   const handleRefresh = async () => {
      setRefreshing(true)
      await queryClient.invalidateQueries({ queryKey: ['positions'] })
      setRefreshing(false)
   };


   const getItem = (_data: unknown, index: number): Props => ({
      id: data[index].id,
      name: data[index].name,
      idGroup: data[index].idGroup,
      group: data[index].group,
      isActive: data[index].isActive,
   });

   return (
      <View style={[Styles.p15, Styles.flex1, { backgroundColor: colors.background }]}>
         <GuideOverlay visible={guideVisible} steps={GUIDE_POSITION} onDismiss={dismissGuide} />
         <View>
            <WrapTab>
               <ButtonTab
                  active={status == "false" ? "false" : "true"}
                  value="true"
                  onPress={() => { setStatus("true") }}
                  label="Aktif"
                  icon={<Feather name="check-circle" color={status == "true" ? 'white' : colors.dimmed} size={20} />}
                  n={2} />
               <ButtonTab
                  active={status == "false" ? "false" : "true"}
                  value="false"
                  onPress={() => { setStatus("false") }}
                  label="Tidak Aktif"
                  icon={<AntDesign name="closecircleo" color={status == "false" ? 'white' : colors.dimmed} size={20} />}
                  n={2} />
            </WrapTab>
            <InputSearch onChange={setSearch} />
            {
               (entityUser.role == "supadmin" || entityUser.role == "developer") &&
               <View style={[Styles.mt10, Styles.rowOnly]}>
                  <Text>Filter :</Text>
                  <LabelStatus size="small" category="secondary" text={nameGroup} style={[Styles.mh05]} />
               </View>
            }
         </View>
         <View style={[Styles.flex2, Styles.mt10]}>
            {
               isLoading ?
                  arrSkeleton.map((item, index) => {
                     return (
                        <SkeletonTwoItem key={index} />
                     )
                  })
                  :
                  data.length > 0 ?
                     <VirtualizedList
                        data={data}
                        getItemCount={() => data.length}
                        getItem={getItem}
                        renderItem={({ item, index }: { item: Props, index: number }) => {
                           return (
                              <BorderBottomItem
                                 key={index}
                                 onPress={() => {
                                    entityUser.role != "user" &&
                                       handleChooseData(item.id, item.name, item.isActive, item.idGroup)
                                 }}
                                 borderType="all"
                                 icon={
                                    <View style={[Styles.iconContent]}>
                                       <MaterialCommunityIcons name="account-tie-outline" size={25} color={'black'} />
                                    </View>
                                 }
                                 title={item.name}
                                 subtitle={item.group}
                              />
                           )
                        }}
                        keyExtractor={(item, index) => String(index)}
                        showsVerticalScrollIndicator={false}
                        refreshControl={
                           <RefreshControl
                              refreshing={refreshing}
                              onRefresh={handleRefresh}
                              tintColor={colors.icon}
                           />
                        }
                     />
                     :
                     <Text style={[Styles.textDefault, Styles.textCenter, { color: colors.dimmed }]}>Tidak ada data</Text>
            }
         </View>
         <DrawerBottom animation="slide" isVisible={isModal} setVisible={() => setModal(false)} title={chooseData.name}>
            <View style={Styles.rowItemsCenter}>
               <MenuItemRow
                  icon={<MaterialCommunityIcons name="toggle-switch-off-outline" color={colors.text} size={25} />}
                  title={chooseData.active ? 'Non Aktifkan' : "Aktifkan"}
                  onPress={() => {
                     setModal(false)
                     setTimeout(() => {
                        setShowDeleteModal(true)
                     }, 600)
                  }}
               />
               <MenuItemRow
                  icon={<MaterialCommunityIcons name="pencil-outline" color={colors.text} size={25} />}
                  title="Edit"
                  onPress={() => {
                     setModal(false)
                     setTimeout(() => {
                        setVisibleEdit(true)
                     }, 600)
                  }}
               />
            </View>
         </DrawerBottom>


         <DrawerBottom animation="none" keyboard height={30} backdropPressable={false} isVisible={isVisibleEdit} setVisible={() => setVisibleEdit(false)} title="Edit Jabatan">
            <View style={[Styles.justifySpaceBetween, Styles.flex1]}>
               <View>
                  <InputForm
                     type="default"
                     placeholder="Nama Jabatan"
                     required
                     label="Jabatan"
                     bg={"transparent"}
                     value={chooseData.name}
                     onChange={(val) => { validationForm(val) }}
                     error={error.name}
                     errorText="Nama jabatan tidak boleh kosong"
                  />
               </View>
               <View style={Styles.mb30}>
                  <ButtonForm text="SIMPAN" onPress={() => { handleEdit() }} disabled={Object.values(error).some((v) => v == true) || chooseData.name == "" || loadingSubmit} />
               </View>
            </View>
         </DrawerBottom>

         <ModalConfirmation
            visible={showDeleteModal}
            title="Konfirmasi"
            message={chooseData.active ? 'Apakah anda yakin ingin menonaktifkan data?' : 'Apakah anda yakin ingin mengaktifkan data?'}
            onConfirm={() => {
               setShowDeleteModal(false)
               handleDelete()
            }}
            onCancel={() => setShowDeleteModal(false)}
            confirmText={chooseData.active ? "Nonaktifkan" : "Aktifkan"}
            cancelText="Batal"
         />
      </View>
   )
}