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() {
   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)
   }

   return (
      <>
         {
            taskCreate.length > 0
            &&
            <>
               <View style={[Styles.mb15, Styles.mt10]}>
                  <Text style={[Styles.textDefaultSemiBold, Styles.mv05]}>
                     Tanggal & Tugas
                  </Text>
                  <View style={[Styles.wrapPaper, { backgroundColor: colors.card, borderColor: colors.background }]}>
                     {
                        taskCreate.map((item: { status: number; title: string; dateStart: string; dateEnd: string; }, index: Key | null | undefined) => {
                           return (
                              <ItemSectionTanggalTugas
                                 key={index}
                                 title={item.title}
                                 dateStart={item.dateStart}
                                 dateEnd={item.dateEnd}
                                 onPress={() => {
                                    setSelect(index)
                                    setModal(true)
                                 }}
                              />
                           );
                        })
                     }

                  </View>
               </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>
            </>
         }
      </>
   )
}