
import * as React from "react";
import * as HttpUtils from "utils/HttpUtils";
import * as UrlUtils from "utils/ApiPathConst";
import { useNavigate } from "react-router-dom";
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 navigate = useNavigate()

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

    function isIAmSmartAccountAlreadyRegistered(error) {
        const ex = error?.response?.data?.exception
        const httpStatus = error?.response?.status
        return httpStatus === 409 || ex === 'msg: account created'
    }

    function getPrfile(){
        
        let params = new URLSearchParams(window.location.search)
        if(params.get("code")){
            HttpUtils.post({
                url: UrlUtils.GET_SMART_PROFILE,
                params:{
                    code: params.get("code")
                },
                onSuccess: (responseData) => {
                    navigate('/iAmSmartRegisterFrom', { state: { responseData: responseData } });
                },
                onFail: (response)=>{
                    console.log("Fail");
                    console.log(response);
                    window.location.assign("/register");
                },
                onError:(error)=>{
                    console.log(error);
                    if (isIAmSmartAccountAlreadyRegistered(error)) {
                        navigate("/iamsmart/pleaseLogin", { state: { registryMethod: "iAmSmart" } });
                    } else {
                        window.location.assign("/register");
                    }
                }
            });
        }else{
            window.location.assign("/register");
            // navigate('/iAmSmartRegisterFrom', { state: { responseData: {
            //     data: "{\"emailAddress\":\"test@gmail.com\",\"postalAddress\":{\"EngPremisesAddress\":{\"EngDistrict\":{\"DcDistrict\":\"KC\",\"Sub-district\":\"TSING YI\"},\"EngEstate\":{\"EstateName\":\"Cheung Hang Estate\",\"EngPhase\":{\"PhaseName\":\"N/A\"}},\"BuildingName\":\"Hang Lai House\",\"EngBlock\":{\"BlockDescriptor\":\"Block\",\"BlockNo\":\"2\"},\"Region\":\"NT\",\"EngStreet\":{\"StreetName\":\"Liu To Road\",\"BuildingNoFrom\":\"6\"},\"Eng3dAddress\":{\"EngFloor\":{\"FloorNum\":\"33\"},\"EngUnit\":{\"UnitDescriptor\":\"Room\",\"UnitNo\":\"3301\"}}}},\"mobileNumber\":{\"CountryCode\":\"852\",\"SubscriberNumber\":\"99999999\"},\"residentialAddress\":{\"ChiPremisesAddress\":{\"Chi3dAddress\":{\"ChiUnit\":{\"UnitDescriptor\":\"室\",\"UnitNo\":\"1010\"},\"ChiFloor\":{\"FloorNum\":\"10\"}},\"ChiBlock\":{\"BlockDescriptor\":\"座\",\"BlockNo\":\"2\"},\"BuildingName\":\"亨麗樓(第2座)\",\"ChiDistrict\":{\"DcDistrict\":\"KC\",\"Sub-district\":\"青衣\"},\"Region\":\"新界\",\"ChiEstate\":{\"EstateName\":\"長亨邨\"},\"ChiStreet\":{\"StreetName\":\"寮肚路\",\"BuildingNoFrom\":\"6\"}}},\"enName\":{\"UnstructuredName\":\"Testing Co One\"},\"idNo\":{\"Identification\":\"G561107\",\"CheckDigit\":\"4\"},\"chName\":{\"ChineseName\":\"測試商一\"}}"
            // } } });
    
        }
    }

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

export default Index;