import { View, Text, StyleSheet, ImageBackground } from "react-native"
import { FlatList } from "react-native-gesture-handler"
import { CITIES_DATA, City, FAVORITE_CITY_DATA } from "../data/stub"
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { VilleCompopo } from "../components/VilleCompopo";
import { TopBar } from "../components/TopBar";
import CityDetails from "./CityDetails";
import { useDispatch, useSelector } from "react-redux";
import { useEffect } from "react";
import { getFavoriteCity } from "../redux/actions/getFavoriteCity";


export default function Home(navigation){

    
    const insets = useSafeAreaInsets();
    const statusBarHeight = insets.top;

    const favoriteCity = useSelector(state => state.appReducer.favoriteCity);

    const dispatch = useDispatch();
    
    useEffect(() => {
      const loadFavoriteCity = async () => {
        await dispatch(getFavoriteCity());
      };
      loadFavoriteCity();
    }, [dispatch]);

    return (
        <View style={{alignItems: "center"}}>
            <TopBar/>
            {(() => {
              if (favoriteCity != null){
                return (
                    <Text>{favoriteCity.name}</Text>
                )
              }
              else{
                return(
                    <Text>Pas de city</Text>
                  )
              }
            })()}
        </View>
    )
}


const leStyle = StyleSheet.create({
    container: {
        alignItems: 'center',
    },
});