import CaraouselHome2 from "@/components/home/carouselHome2";
import ChartDokumenHome from "@/components/home/chartDokumenHome";
import ChartProgresHome from "@/components/home/chartProgresHome";
import DisccussionHome from "@/components/home/discussionHome";
import DivisionHome from "@/components/home/divisionHome";
import EventHome from "@/components/home/eventHome";
import { HeaderRightHome } from "@/components/home/headerRightHome";
import ProjectHome from "@/components/home/projectHome";
import Text from "@/components/Text";
import Styles from "@/constants/Styles";
import { apiGetProfile } from "@/lib/api";
import { setEntities } from "@/lib/entitiesSlice";
import { useAuthSession } from "@/providers/AuthProvider";
import { useTheme } from "@/providers/ThemeProvider";
import { LinearGradient } from "expo-linear-gradient";
import { Stack } from "expo-router";
import { useEffect, useState } from "react";
import { Dimensions, Platform, RefreshControl, SafeAreaView, ScrollView, View } from "react-native";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import { useDispatch, useSelector } from "react-redux";


export default function Home() {
   const entities = useSelector((state: any) => state.entities)
   const dispatch = useDispatch()
   const { token, decryptToken, signOut } = useAuthSession()
   const { colors } = useTheme();
   const insets = useSafeAreaInsets()
   const [refreshing, setRefreshing] = useState(false)

   useEffect(() => {
      handleUserLogin()
   }, [dispatch]);

   async function handleUserLogin() {
      const hasil = await decryptToken(String(token?.current))
      apiGetProfile({ id: hasil })
         .then((data) => dispatch(setEntities(data.data)))
         .catch((error) => {
            signOut()
         });
   }

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

   return (
      <SafeAreaView style={[Styles.flex1, { backgroundColor: colors.background }]}>
         <Stack.Screen
            options={{
               title: 'Home',
               headerTitle: entities.village,
               header: () => (
                  <View style={[Styles.rowItemsCenter, Styles.ph20, Platform.OS === 'ios' ? Styles.pb07 : Styles.pb13, { backgroundColor: colors.header, paddingTop: Platform.OS === 'ios' ? insets.top : 10 }]}>
                     <Text style={Styles.textHeaderHome}>{entities.village}</Text>
                     <HeaderRightHome />
                  </View>
               ),
            }}
         />
         <ScrollView
            refreshControl={
               <RefreshControl
                  refreshing={refreshing}
                  onRefresh={handleRefresh}
                  tintColor={colors.icon}
               />
            }
            showsVerticalScrollIndicator={false}
            style={[Styles.h100, { backgroundColor: colors.background }]}
         >
            <LinearGradient
               colors={[colors.header, colors.header, colors.header, colors.header, colors.homeGradient]}
               style={[
                  Styles.posAbsolute,
                  Styles.zIndexMinus1,
                  {
                     width: Dimensions.get('window').width * 1.5,
                     height: Dimensions.get('window').width * 1.5,
                     borderRadius: Dimensions.get('window').width * 0.5,
                     top: -Dimensions.get('window').width * 1, // Positioned to show the bottom part of the circle as an arc
                     left: -Dimensions.get('window').width * 0.25,
                  }
               ]}
            />
            {/* <CaraouselHome refreshing={refreshing} /> */}
            <View style={[Styles.ph15]}>
               <CaraouselHome2 refreshing={refreshing} />
               {/* <FiturHome /> */}
               <ProjectHome refreshing={refreshing} />
               <DivisionHome refreshing={refreshing} />
               <ChartProgresHome refreshing={refreshing} />
               <ChartDokumenHome refreshing={refreshing} />
               <EventHome refreshing={refreshing} />
               <DisccussionHome refreshing={refreshing} />
            </View>
         </ScrollView>
      </SafeAreaView>
   )
}