import {lazy, useContext} from 'react';

import Require2FA from '../components/Require2FA';
import Loadable from 'components/Loadable';
import MainLayout from "../layout/MainLayout";
import {handleRouteAbility} from "../utils/CommonFunction";
import {Navigate} from "react-router";
import AbilityContext from "../components/AbilityProvider";
import { apiPath, appURL, GET_PDF_PATH } from "../auth/utils";

// render - login
const ClientSearchPage = Loadable(lazy( () => import('pages/client/ClientSearchPage')));
const ClientMaintainPage = Loadable(lazy( () => import('pages/client/ClientMaintainPage')));
const PdfMaintainPage = Loadable(lazy(() => import('pages/pdf/PdfMaintainPage')));
const PdfFormUpAndDown = Loadable(lazy(() => import('pages/pdf/PdfFormUpAndDown')));
const PdfViewer = Loadable(lazy(() => import('pages/pdf/PdfViewer')));
const PdfSearchPage = Loadable(lazy(() => import('pages/pdf/PdfSearchPage')));
const TemplateSearchPage = Loadable(lazy(() => import('pages/pdf/TemplateSearchPage')));

// ==============================|| AUTH ROUTING ||============================== //

const ClientRoutes =() => {
    const ability = useContext(AbilityContext);

    return {
        path: '/',
        element: <MainLayout />,
        children: [
            
            {
                path: 'client',
                element:  (
                    <Require2FA>
                    {handleRouteAbility(
                        ability.can('VIEW', 'CLIENT'),
                        <ClientSearchPage />,
                        <Navigate to="/" />
                    )}
                    </Require2FA>
                ),
            },
            {
                path: 'client/maintain/:id',
                element:  (
                    <Require2FA>
                    {
                    handleRouteAbility(
                        ability.can('VIEW', 'CLIENT'),
                        <ClientMaintainPage />,
                        <Navigate to="/" />
                    )}
                    </Require2FA>
                ),
            },
            {
              path: '/pdf/:id',
              element:  (
                    <Require2FA>
                    {handleRouteAbility(
                        ability.can('VIEW', 'CLIENT'),
                        <PdfSearchPage />,
                        <Navigate to="/" />
                    )}
                    </Require2FA>
              ),
            },
            {
                path: '/pdf/maintain/:id',
                element:  (
                    <Require2FA>
                    {handleRouteAbility(
                        ability.can('VIEW', 'CLIENT'),
                        <PdfMaintainPage />,
                        <Navigate to="/" />
                    )}
                    </Require2FA>
                ),
            },
            {
                path: '/template/',
                element:  (
                    <Require2FA>
                    {handleRouteAbility(
                        ability.can('VIEW', 'TEMPLATE'),
                        <TemplateSearchPage />,
                        <Navigate to="/" />
                    )}
                    </Require2FA>
                ),
            },
            {
                path: '/pdf/form-up-down/:id',
                element:  (
                    <Require2FA>
                    {handleRouteAbility(
                        ability.can('VIEW', 'CLIENT'),
                        <PdfFormUpAndDown />,
                        <Navigate to="/" />
                    )}
                    </Require2FA>
                ),
              },
            {
                path: '/pdf/newIDA/:id',
                element:  (
                    handleRouteAbility(
                        ability.can('VIEW', 'CLIENT'),
                        <PdfViewer pdfUrl={`${apiPath}/pdf2/get?id=1`} 
                            formCode='IDA'
                        />,
                        <Navigate to="/" />
                    )
                ),
              },
            {
                path: '/pdf/newFNA/:id',
                element:  (
                    handleRouteAbility(
                        ability.can('VIEW', 'CLIENT'),
                        <PdfViewer pdfUrl={`${apiPath}/pdf2/get?id=2`} 
                            formCode='FNA'
                        />,
                        <Navigate to="/" />
                    )
                ),
              },
              
            {
                path: '/pdf/newHSBCFIN/:id',
                element:  (
                    handleRouteAbility(
                        ability.can('VIEW', 'CLIENT'),
                        <PdfViewer pdfUrl={`${apiPath}/pdf2/get?id=3`} 
                            formCode='HSBCFIN'
                        />,
                        <Navigate to="/" />
                        
                    )
                ),
              },
         ]
    };
};

export default ClientRoutes;
