import HeaderRightAnnouncementDetail from "@/components/announcement/headerAnnouncementDetail";
import ButtonBackHeader from "@/components/buttonBackHeader";
import Skeleton from "@/components/skeleton";
import Text from '@/components/Text';
import Styles from "@/constants/Styles";
import { apiGetAnnouncementOne } from "@/lib/api";
import { useAuthSession } from "@/providers/AuthProvider";
import { Entypo, MaterialIcons } from "@expo/vector-icons";
import { router, Stack, useLocalSearchParams } from "expo-router";
import React, { useEffect, useState } from "react";
import { Dimensions, RefreshControl, SafeAreaView, ScrollView, View } from "react-native";
import RenderHTML from 'react-native-render-html';
import Toast from "react-native-toast-message";
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
   const [loading, setLoading] = useState(true)
   const arrSkeleton = Array.from({ length: 2 }, (_, index) => index)
   const [refreshing, setRefreshing] = useState(false)

   async function handleLoad(loading: boolean) {
      try {
         setLoading(loading)
         const hasil = await decryptToken(String(token?.current))
         const response = await apiGetAnnouncementOne({ id: id, user: hasil })
         if (response.success) {
            setData(response.data)
            setDataMember(response.member)
         } else {
            Toast.show({ type: 'small', text1: response.message })
         }
      } catch (error) {
         console.error(error)
         Toast.show({ type: 'small', text1: 'Gagal mengambil data' })
      } finally {
         setLoading(false)
      }
   }

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

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

   function hasHtmlTags(text: string) {
      const htmlRegex = /<[a-z][\s\S]*>/i;
      return htmlRegex.test(text);
   };

   const handleRefresh = async () => {
      setRefreshing(true)
      handleLoad(false)
      await new Promise(resolve => setTimeout(resolve, 2000));
      setRefreshing(false)
   };

   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
            showsVerticalScrollIndicator={false}
            style={[Styles.h100]}
            refreshControl={
               <RefreshControl
                  refreshing={refreshing}
                  onRefresh={() => handleRefresh()}
               />
            }
         >
            <View style={[Styles.p15]}>
               <View style={[Styles.wrapPaper]}>
                  {
                     loading ?
                        <View>
                           <View style={[Styles.rowOnly]}>
                              <Skeleton width={30} height={30} borderRadius={10} />
                              <View style={[{ flex: 1 }, Styles.ph05]}>
                                 <Skeleton width={100} widthType="percent" height={30} borderRadius={10} />
                              </View>
                           </View>
                           <Skeleton width={100} widthType="percent" height={10} borderRadius={10} />
                           <Skeleton width={100} widthType="percent" height={10} borderRadius={10} />
                           <Skeleton width={100} widthType="percent" height={10} borderRadius={10} />
                        </View>
                        :
                        <>
                           <View style={[Styles.rowItemsCenter, { alignItems: 'flex-start' }]}>
                              <MaterialIcons name="campaign" size={30} color="black" style={Styles.mr05} />
                              <Text style={[Styles.textDefaultSemiBold, Styles.w90]}>{data?.title}</Text>
                           </View>
                           <View style={[Styles.mt10]}>
                              {
                                 hasHtmlTags(data?.desc) ?
                                    <RenderHTML
                                       contentWidth={contentWidth}
                                       source={{ html: data?.desc }}
                                       baseStyle={{ color: 'black' }}
                                    />
                                    :
                                    <Text>{data?.desc}</Text>
                              }
                           </View>
                        </>
                  }

               </View>
               <View style={[Styles.wrapPaper, Styles.mv15]}>
                  {
                     loading ?
                        arrSkeleton.map((item, index) => {
                           return (
                              <View key={index}>
                                 <Skeleton width={30} widthType="percent" height={10} borderRadius={10} />
                                 <Skeleton width={100} widthType="percent" height={10} borderRadius={10} />
                                 <Skeleton width={100} widthType="percent" height={10} borderRadius={10} />
                              </View>
                           )
                        })
                        :
                        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, Styles.w90]}>
                                             <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>
   )
}