
// 引入Provider
import { Provider } from 'react-redux';
// 引入仓库
import store from './store';
import Head from './layout/head/Head.tsx';
import Body from './layout/body/Body.tsx';
// import Foot from './layout/foot/Foot.tsx';
import {
    GlobalContext,
    GlobalData,
    GlobalDataAction,
    GlobalDataActionType,
    GlobalDispatchContext,
} from "./context/GlobalContext.ts";
import React, { Reducer, useReducer, useState, useEffect } from "react";

import {
    message
} from 'antd';
import { get } from "./util/AjaxUtils.ts";
const App: React.FC = () => {
    const [isTokenFetched, setIsTokenFetched] = useState(false);
    const globalDataReducer = (state: GlobalData, action: GlobalDataAction) => {
        if (action.type == GlobalDataActionType.REFRESH_SELF) {
            if (action.user) {
                state.user.balance = action.user.balance;
                state.user.userId = action.user.userId;
                state.user.nickname = action.user.nickname;
                state.user.username = action.user.username;
                state.user.hasUserInfo = action.user.hasUserInfo;
            }
        }
        return {
            ...state
        }
    }
    const [globalData, dispatch] = useReducer<Reducer<GlobalData, GlobalDataAction>>(globalDataReducer, {
        user: {
            balance: '0',
            userId: '',
            username: '',
            nickname: '',
            hasUserInfo: false
        }
    });
    const [messageApi] = message.useMessage();
    //  const nav = useNavigate();
    useEffect(() => {

        get<any>({
            messageApi,
            url: '/api/user-info/get-user-self',
            onSuccess({ data }: any) {
                // const currentToken = sessionStorage.getItem('token');
                sessionStorage.setItem('token', data.accessToken);
                const token = sessionStorage.getItem('token');
                if (token) {
                    // 若 token 存在，设置 isTokenFetched 为 true
                    setIsTokenFetched(true);
                }
            }
        })

    }, []);
    // const token = sessionStorage.getItem('token');
    // useEffect(() => {
    //     if (token) {
    //         setIsTokenFetched(true)
    //     }


    // }, [token]);
    // 如果 token 还未获取，显示加载页面
    // if (!isTokenFetched) {
    //     return (
    //         <div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100vh' }}>
    //             <p>正在加载，请稍候...</p>
    //         </div>
    //     );
    // }
    if (isTokenFetched) {
        return (
            <div>

                <Provider store={store}>
                    <GlobalContext.Provider value={globalData}>
                        <GlobalDispatchContext.Provider value={dispatch}>
                            <Head />
                            <Body />
                            {/* <Foot/> */}
                        </GlobalDispatchContext.Provider>
                    </GlobalContext.Provider>
                </Provider>
            </div>
        );
    }

};

export default App;