import Styles from "@/constants/Styles"
import { apiGetDocument } from "@/lib/api"
import { useAuthSession } from "@/providers/AuthProvider"
import { useTheme } from "@/providers/ThemeProvider"
import { AntDesign, Ionicons } from "@expo/vector-icons"
import { useLocalSearchParams } from "expo-router"
import { useEffect, useState } from "react"
import { Pressable, View } from "react-native"
import BorderBottomItem from "../borderBottomItem"
import DrawerBottom from "../drawerBottom"
import Text from "../Text"
import { ModalNewFolder } from "./modalNewFolder"

type Props = {
   open: boolean
   close: (value: boolean) => void
   category: 'copy' | 'move'
   onConfirm: (value: string) => void
   dataChoose: any[]
}

type DataProps = {
   id: string;
   category: string;
   name: string;
   extension: string;
   idStorage: string;
   path: string;
   createdBy: string;
   share: boolean;
   createdAt: string;
   updatedAt: string;
}

type PropsPath = {
   id: string;
   name: string;
};

export default function ModalSalinMove({ open, close, category, onConfirm, dataChoose }: Props) {
   const { colors } = useTheme()
   const [data, setData] = useState<DataProps[]>([])
   const { token, decryptToken } = useAuthSession()
   const [path, setPath] = useState('home')
   const { id } = useLocalSearchParams<{ id: string }>();
   const [dataJalur, setDataJalur] = useState<PropsPath[]>([])

   async function getData() {
      try {
         const hasil = await decryptToken(String(token?.current))
         const response = await apiGetDocument({ user: hasil, path, division: id, category: 'folder' })
         if (response.success) {
            setData(response.data)
            setDataJalur(response.jalur)
         }
      } catch (error) {
         console.error(error)
      }
   }

   useEffect(() => {
      getData()
   }, [path])

   return (
      <DrawerBottom animation="slide" isVisible={open} setVisible={close} title={category == 'copy' ? 'Pilih Lokasi Salin' : 'Pilih Lokasi Pemindahan'} height={75}>
         <View style={[Styles.rowItemsCenter, Styles.mv05]}>
            {
               dataJalur.map((item, index) => (
                  <Pressable
                     key={index}
                     style={[Styles.rowItemsCenter]}
                     onPress={() => {
                        setPath(item.id);
                     }}
                  >
                     {item.id != "home" && (
                        <AntDesign name="right" style={[Styles.mh05, Styles.mt02]} color={colors.text} />
                     )}
                     <Text style={{ color: colors.text }}> {item.name} </Text>
                  </Pressable>
               ))
            }
         </View>
         <View>
            {
               data.length > 0 ?
                  data.map((item, index) => {
                     const found = dataChoose.some((i: any) => i.id == item.id);
                     return (
                        <BorderBottomItem
                           key={index}
                           borderType="bottom"
                           icon={<Ionicons name="folder-open-sharp" style={[found ? Styles.cGray : Styles.cFolder]} size={30} />}
                           title={item.name}
                           titleWeight="normal"
                           bgColor="transparent"
                           onPress={() => {
                              if (found) return;
                              setPath(item.id);
                           }}
                           subtitle={found ? <Text style={[Styles.textInformation, { color: colors.dimmed }]}>Tidak dapat memilih folder ini</Text> : ''}
                        />
                     )
                  }
                  )

                  :
                  <Text style={[Styles.textDefault, { textAlign: 'center', color: colors.dimmed }, Styles.mt15]}>Tidak ada data</Text>
            }
         </View>
         <View style={[Styles.rowOnly, Styles.mt15, Styles.absolute0, { backgroundColor: colors.modalBackground }]}>
            <ModalNewFolder path={path} onCreated={() => getData()} />
            <Pressable style={[Styles.pv05, { width: '50%' }]} onPress={() => onConfirm(path)}>
               <Text style={[Styles.textDefaultSemiBold, { textAlign: 'center' }]}>{category == 'copy' ? 'SALIN' : 'PINDAH'}</Text>
            </Pressable>
         </View>
      </DrawerBottom>
   )
}
