import Styles from "@/constants/Styles";
import { deleteTaskCreate } from "@/lib/taskCreate";
import { useTheme } from "@/providers/ThemeProvider";
import { Ionicons } from "@expo/vector-icons";
import { Key, useState } from "react";
import { View } from "react-native";
import { useDispatch, useSelector } from "react-redux";
import DrawerBottom from "../drawerBottom";
import ItemSectionTanggalTugas from "../itemSectionTanggalTugas";
import MenuItemRow from "../menuItemRow";
import Text from "../Text";

export default function SectionListAddTask({ showTitle = true }: { showTitle?: boolean }) {
   const { colors } = useTheme();
   const taskCreate = useSelector((state: any) => state.taskCreate)
   const [select, setSelect] = useState<any>(null)
   const [isModal, setModal] = useState(false)
   const dispatch = useDispatch()

   function handleDelete() {
      dispatch(deleteTaskCreate(select))
      setModal(false)
   }

   const items = taskCreate.map((item: { status: number; title: string; dateStart: string; dateEnd: string; }, index: Key | null | undefined) => (
      <ItemSectionTanggalTugas
         key={index}
         title={item.title}
         dateStart={item.dateStart}
         dateEnd={item.dateEnd}
         onPress={() => {
            setSelect(index)
            setModal(true)
         }}
      />
   ))

   return (
      <>
         {taskCreate.length > 0 && (
            <>
               {showTitle ? (
                  <View style={[Styles.mb15, Styles.mt10]}>
                     <Text style={[Styles.textDefaultSemiBold, Styles.mv05]}>Tanggal & Tugas</Text>
                     {items}
                  </View>
               ) : (
                  <View>{items}</View>
               )}
               <DrawerBottom animation="slide" isVisible={isModal} setVisible={setModal} title="Menu">
                  <View style={Styles.rowItemsCenter}>
                     <MenuItemRow
                        icon={<Ionicons name="trash-outline" color={colors.text} size={25} />}
                        title="Hapus Tugas"
                        onPress={() => { handleDelete() }}
                     />
                  </View>
               </DrawerBottom>
            </>
         )}
      </>
   )
}