import HeaderRightAnnouncementDetail from "@/components/announcement/headerAnnouncementDetail";
import ButtonBackHeader from "@/components/buttonBackHeader";
import Styles from "@/constants/Styles";
import { apiGetAnnouncementOne } from "@/lib/api";
import { useAuthSession } from "@/providers/AuthProvider";
import { AntDesign, Entypo, MaterialIcons } from "@expo/vector-icons";
import { router, Stack, useLocalSearchParams } from "expo-router";
import { useEffect, useState } from "react";
import { Dimensions, SafeAreaView, ScrollView, Text, View } from "react-native";
import RenderHTML from 'react-native-render-html';
import { useSelector } from "react-redux";

type Props = {
   id: string
   title: string
   desc: string
}

export default function DetailAnnouncement() {
   const { id } = useLocalSearchParams<{ id: string }>();
   const { token, decryptToken } = useAuthSession()
   const [data, setData] = useState<Props>({ id: '', title: '', desc: '' })
   const [dataMember, setDataMember] = useState<any>({})
   const update = useSelector((state: any) => state.announcementUpdate)
   const entityUser = useSelector((state: any) => state.user)
   const contentWidth = Dimensions.get('window').width

   async function handleLoad() {
      try {
         const hasil = await decryptToken(String(token?.current))
         const response = await apiGetAnnouncementOne({ id: id, user: hasil })
         setData(response.data)
         setDataMember(response.member)
      } catch (error) {
         console.error(error)
      }
   }

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

   return (
      <SafeAreaView>
         <Stack.Screen
            options={{
               headerLeft: () => <ButtonBackHeader onPress={() => { router.back() }} />,
               headerTitle: 'Pengumuman',
               headerTitleAlign: 'center',
               headerRight: () => entityUser.role != 'user' && entityUser.role != 'coadmin' ? <HeaderRightAnnouncementDetail id={id} /> : <></>,
            }}
         />
         <ScrollView>
            <View style={[Styles.p15]}>
               <View style={[Styles.wrapPaper]}>
                  <View style={Styles.rowItemsCenter}>
                     <MaterialIcons name="campaign" size={30} color="black" style={Styles.mr05} />
                     <Text style={[Styles.textDefaultSemiBold]}>{data?.title}</Text>
                  </View>
                  <View style={[Styles.mt10]}>
                     <RenderHTML
                        contentWidth={contentWidth}
                        source={{ html: data?.desc }}
                     />
                  </View>
               </View>
               <View style={[Styles.wrapPaper, Styles.mv15]}>
                  {
                     Object.keys(dataMember).map((v: any, i: any) => {
                        return (
                           <View key={i} style={[Styles.mb05]}>
                              <Text style={[Styles.textDefaultSemiBold]}>{dataMember[v]?.[0].group}</Text>
                              {
                                 dataMember[v].map((item: any, x: any) => {
                                    return (
                                       <View key={x} style={[Styles.rowItemsCenter]}>
                                          <Entypo name="dot-single" size={24} color="black" />
                                          <Text style={[Styles.textDefault]} numberOfLines={1} ellipsizeMode='tail'>{item.division}</Text>
                                       </View>
                                    )
                                 })
                              }

                           </View>
                        )
                     })
                  }
               </View>
            </View>
         </ScrollView>
      </SafeAreaView>
   )
}