import BorderBottomItem from "@/components/borderBottomItem";
import ButtonTab from "@/components/buttonTab";
import InputSearch from "@/components/inputSearch";
import LabelStatus from "@/components/labelStatus";
import { ColorsStatus } from "@/constants/ColorsStatus";
import Styles from "@/constants/Styles";
import { apiGetDiscussionGeneral } from "@/lib/api";
import { useAuthSession } from "@/providers/AuthProvider";
import { AntDesign, Feather, Ionicons, MaterialIcons } from "@expo/vector-icons";
import { router, useLocalSearchParams } from "expo-router";
import { useEffect, useState } from "react";
import { SafeAreaView, ScrollView, Text, View } from "react-native";
import { useSelector } from "react-redux";


type Props = {
   id: string
   title: string
   desc: string
   status: number
   total_komentar: number
   createdAt: string
}

export default function Discussion() {
   const entityUser = useSelector((state: any) => state.user)
   const { token, decryptToken } = useAuthSession()
   const { active, group } = useLocalSearchParams<{ active?: string, group?: string }>()
   const [search, setSearch] = useState('')
   const [nameGroup, setNameGroup] = useState('')
   const [data, setData] = useState<Props[]>([])
   const update = useSelector((state: any) => state.discussionGeneralDetailUpdate)

   async function handleLoad() {
      try {
         const hasil = await decryptToken(String(token?.current))
         const response = await apiGetDiscussionGeneral({ user: hasil, active: String(active), search: search, group: String(group) })
         setData(response.data)
         setNameGroup(response.filter.name)
      } catch (error) {
         console.error(error)
      }
   }

   useEffect(() => {
      handleLoad()
   }, [active, search, group, update])

   return (
      <SafeAreaView>
         <ScrollView>
            <View style={[Styles.p15, Styles.mb100]}>
               <View style={[Styles.wrapBtnTab]}>
                  <ButtonTab
                     active={active == "false" ? "false" : "true"}
                     value="true"
                     onPress={() => { router.push(`/discussion?active=true&group=${group}&search=${search}`) }}
                     label="Aktif"
                     icon={<Feather name="check-circle" color={active == "false" ? 'black' : 'white'} size={20} />}
                     n={2} />
                  <ButtonTab
                     active={active == "false" ? "false" : "true"}
                     value="false"
                     onPress={() => { router.push(`/discussion?active=false&group=${group}&search=${search}`) }}
                     label="Arsip"
                     icon={<AntDesign name="closecircleo" color={active == "true" ? 'black' : 'white'} size={20} />}
                     n={2} />
               </View>
               <InputSearch onChange={setSearch} />
               {
                  (entityUser.role == "supadmin" || entityUser.role == "developer") &&
                  <View style={[Styles.mv05]}>
                     <Text>Filter : {nameGroup}</Text>
                  </View>
               }
               <View>
                  {
                     data.length > 0
                        ?
                        data.map((item: any, i: number) => {
                           return (
                              <BorderBottomItem
                                 key={i}
                                 onPress={() => { router.push(`/discussion/${item.id}`) }}
                                 borderType="bottom"
                                 icon={
                                    <View style={[Styles.iconContent, ColorsStatus.lightGreen]}>
                                       <MaterialIcons name="chat" size={25} color={'#384288'} />
                                    </View>
                                 }
                                 title={item.title}
                                 subtitle={
                                    active != "false" && <LabelStatus category={item.status === 1 ? "success" : "error"} text={item.status === 1 ? "BUKA" : "TUTUP"} size="small" />
                                 }
                                 rightTopInfo={item.createdAt}
                                 desc={item.desc}
                                 leftBottomInfo={
                                    <View style={[Styles.rowItemsCenter]}>
                                       <Ionicons name="chatbox-ellipses-outline" size={18} color="grey" style={Styles.mr05} />
                                       <Text style={[Styles.textInformation, Styles.cGray, Styles.mb05]}>Diskusikan</Text>
                                    </View>
                                 }
                                 rightBottomInfo={`${item.total_komentar} Komentar`}
                              />
                           )
                        })
                        :
                        <Text style={[Styles.textDefault, Styles.cGray, { textAlign: 'center' }]}>Tidak ada data</Text>
                  }
               </View>
            </View>
         </ScrollView>
      </SafeAreaView>
   );
}