
import * as React from "react";
import * as HttpUtils from "utils/HttpUtils";
import * as UrlUtils from "utils/ApiPathConst";
import { useNavigate } from "react-router-dom";
import { useDispatch } from "react-redux";
import { handleLogoutFunction, handleLogin } from 'auth/index';

import Loadable from 'components/Loadable';
const LoadingComponent = Loadable(React.lazy(() => import('pages/extra-pages/LoadingComponent')));
import {
    Grid,
} from '@mui/material';

// ==============================|| DASHBOARD - DEFAULT ||============================== //

const Index = () => {

    const dispatch = useDispatch()
    const navigate = useNavigate()

    React.useEffect(() => {
        goLogin();
    }, []);

    function goLogin(){
        dispatch(handleLogoutFunction());

        let params = new URLSearchParams(window.location.search)
        if(params.get("code")){
            HttpUtils.post({
                url: UrlUtils.GET_SMART_LOGIN,
                params:{
                    code: params.get("code")
                },
                onSuccess: (responseData) => {
                    // console.log("responseData");
                    // console.log(responseData);
                        const userData = {
                          id: responseData.id,
                          fullenName: responseData.name,
                          fullchName: responseData.chName,
                          email: responseData.email,
                          type: responseData.type,
                          role: responseData.role,
                          abilities: responseData.abilities,
                          creditor: responseData.creditor,
                          passwordExpiryDate: responseData.passwordExpiryDate,
                          orgPaymentRecord: responseData.orgPaymentRecord,
                          orgDnRecord: responseData.orgDnRecord,
                          //locale: responseData.preferLocale,
                          //avatar: require('src/assets/images/users/avatar-3.png').default,
                        }
                         const data = { ...userData, accessToken: responseData.accessToken, refreshToken: responseData.refreshToken }
                        // if(responseData.type === "GLD"){
                        //     // setLocale("en");
                        //     localStorage.setItem('locale','en');
                        // }else{
                        //     if (responseData.preferLocale ==="zh_HK"){
                        //         // setLocale("zh-HK");
                        //         localStorage.setItem('locale','zh-HK');
                        //     }
                        //     if (responseData.preferLocale ==="zh-CN"){
                        //         // setLocale("zh-CN");
                        //         localStorage.setItem('locale','zh-CN');
                        //     }
                        //     if (response.data.preferLocale ==="en"){
                        //         // setLocale("zh-CN");
                        //         localStorage.setItem('locale','en');
                        //     }
                        // }
                        dispatch(handleLogin(data))
                        navigate('/iamsmart/loginSucess', {
                            state: {
                                loginMethod: 'iAmSmart',
                                iAmSmartNewlyLinked: responseData.iAmSmartNewlyLinked === true,
                            },
                        });
                },
                onFail: (response)=>{
                    console.log("onFail");
                    console.log(response);
                    window.location.assign("/iamsmart/loginFail");
                },
                onError:(error)=>{
                    console.log("onError");
                    console.log(error);
                    if(error?.response?.data?.exception == "msg: please verify email."){
                        window.location.assign("/iamsmart/notverify");
                    }else{
                        window.location.assign("/iamsmart/loginFail");
                    }
                }
            });
        }else{
            console.log("Fail");
            window.location.assign("/iamsmart/loginFail");
        }
    }

    return (
        <Grid container sx={{ minHeight: '87vh', mb: 3 }} direction="column" justifyContent="center" alignItems="center">
            <Grid item>
                <LoadingComponent />
            </Grid>
        </Grid>
    );
};

export default Index;