

import TabBar from "@/components/navigation/TabBar";
import { rpxToPx } from "@/utils/tools";
import { View, Text, Image, ScrollView, Button } from "@tarojs/components";
import Taro, { useRouter, useShareAppMessage } from "@tarojs/taro";
import { useDispatch, useSelector } from "react-redux";
import { useEffect, useState } from "react";
import { followUser, getFriendDashBoard, getFriendMoments, getMyFriends, getUserHome } from "@/services/friend";
import FriendGuide from "./guide";
import EmptyContent from "./empty_content";
import MomentItem from "./moment_item";
import { useTranslation } from "react-i18next";
import { windows } from "@/services/health";
import { setFastWithSleep, setFinishSetup, setLongFast, setRefreshs, setWindows } from "@/store/health";
import { getInfoSuccess } from "@/store/user";
import FriendMain from "./friend_main";


export default function Friend() {
    const dispatch = useDispatch();
    const user = useSelector((state: any) => state.user);
    const [loaded, setLoaded] = useState(false)

    useEffect(() => {
        if (process.env.TARO_ENV == 'weapp') {
            loadWXCache()
        }
        else {
            loadRNCache()
        }
    }, [])

    useEffect(() => {
        getWindows()
    }, [user.isLogin])

    function getWindows() {
        windows().then(res => {
            dispatch(setFastWithSleep((res as any).fast_with_sleep))
            dispatch(setWindows((res as any).windows))
            dispatch(setLongFast((res as any).long_fast))
            dispatch(setRefreshs((res as any).refresh_timestamps))
            dispatch(setFinishSetup((res as any).finish_setup))
        }).catch(e => {
        })
    }

    function loadWXCache() {
        var gps = Taro.getStorageSync('gps')
        if (gps) {
            global.locationDetail = JSON.parse(gps)
        }
        global.memberAlert = Taro.getStorageSync('memberAlert') || false
        var userData = Taro.getStorageSync('userData')
        if (userData) {
            console.log('load user cache')
            dispatch(getInfoSuccess(JSON.parse(userData)));
        }
        setLoaded(true)
    }

    async function loadRNCache() {
        var showDayRing = await getStorage('showDayRing') || false;
        var showNightRing = await getStorage('showNightRing') || false;
        global.memberAlert = await getStorage('memberAlert') || false
        var gps = await getStorage('gps')
        if (gps) {
            global.locationDetail = JSON.parse(gps)
        }
        var userData = await getStorage('userData')
        console.log(userData)
        if (userData) {
            dispatch(getInfoSuccess(JSON.parse(userData)));
        }
        setLoaded(true)
    }

    async function getStorage(key: string) {
        try {
            const res = await Taro.getStorage({ key });
            return res.data;
        } catch {
            return '';
        }
    }
    if (!loaded)
        return <View />
    return <FriendMain />
}