import { BrowserRouter as Router, Navigate, Route, Routes } from 'react-router-dom'
import { lazy, memo, Suspense } from 'react'
import locale from 'antd/lib/locale/ru_RU'
import { ConfigProvider } from 'antd'

import { RootPathContext } from '@asb/context'
import { UserOutlet } from '@components/outlets'
import SuspenseFallback from '@components/SuspenseFallback'
import { getUserToken, NoAccessComponent } from '@utils'
import { OpenAPI } from '@api'

import '@styles/include/antd_theme.less'
import '@styles/App.less'

const Login = lazy(() => import('@pages/public/Login'))
const Register = lazy(() => import('@pages/public/Register'))
const FileDownload = lazy(() => import('@pages/FileDownload'))

const AdminPanel = lazy(() => import('@pages/AdminPanel'))
const Deposit = lazy(() => import('@pages/Deposit'))
const Cluster = lazy(() => import('@pages/Cluster'))
const Well = lazy(() => import('@pages/Well'))

const Measure = lazy(() => import('@pages/Measure'))
const Reports = lazy(() => import('@pages/Reports'))
const WellCase = lazy(() => import('@pages/WellCase'))
const Analytics = lazy(() => import('@pages/Analytics'))
const Documents = lazy(() => import('@pages/Documents'))
const Telemetry = lazy(() => import('@pages/Telemetry'))
const WellOperations = lazy(() => import('@pages/WellOperations'))
const DrillingProgram = lazy(() => import('@pages/DrillingProgram'))

const Tvd = lazy(() => import('@pages/WellOperations/Tvd'))
const WellDrillParams = lazy(() => import('@pages/WellOperations/WellDrillParams'))
const DrillProcessFlow = lazy(() => import('@pages/WellOperations/DrillProcessFlow'))
const WellSectionsStat = lazy(() => import('@pages/WellOperations/WellSectionsStat'))
const WellOperationsEditorFact = lazy(() => import('@pages/WellOperations/OperationEditor/Fact'))
const WellOperationsEditorPlan = lazy(() => import('@pages/WellOperations/OperationEditor/Plan'))

const Archive = lazy(() => import('@pages/Telemetry/Archive'))
const Messages = lazy(() => import('@pages/Telemetry/Messages'))
const Operations = lazy(() => import('@pages/Telemetry/Operations'))
const DashboardNNB = lazy(() => import('@pages/Telemetry/DashboardNNB'))
const TelemetryView = lazy(() => import('@pages/Telemetry/TelemetryView'))
const OperationTime = lazy(() => import('@pages/Telemetry/OperationTime'))

const DailyReport = lazy(() => import('@pages/Reports/DailyReport'))
const DiagramReport = lazy(() => import('@pages/Reports/DiagramReport'))

const Statistics = lazy(() => import('@pages/Analytics/Statistics'))
const WellCompositeEditor = lazy(() => import('@pages/Analytics/WellCompositeEditor'))

//OpenAPI.BASE = 'http://localhost:3000'
OpenAPI.TOKEN = async () => getUserToken() ?? ''
OpenAPI.HEADERS = { 'Content-Type': 'application/json' }

export const App = memo(() => (
    <ConfigProvider locale={locale}>
        <RootPathContext.Provider value={''}>
            <Suspense fallback={<SuspenseFallback style={{ minHeight: '100vh' }} />}>
                <Router>
                    <Routes>
                        <Route index element={<Navigate to={'deposit'} replace />} />
                        <Route path={'*'} element={<NoAccessComponent />} />

                        {/* Public pages */}
                        <Route path={'/login'} element={<Login />} />
                        <Route path={'/register'} element={<Register />} />

                        {/* User pages */}
                        <Route element={<UserOutlet />}>
                            {/* Admin pages */}
                            <Route path={'/admin/*'} element={<AdminPanel />} />

                            {/* Client pages */}
                            <Route path={'/file_download/:idWell/:idFile/*'} element={<FileDownload />} />
                            <Route path={'/deposit/*'} element={<Deposit />} />
                            <Route path={'/cluster/:idCluster'} element={<Cluster />} />
                            <Route path={'/well/:idWell/*'} element={<Well />}>
                                <Route index element={<Navigate to={'telemetry'} replace />} />
                                <Route path={'*'} element={<NoAccessComponent />} />

                                <Route path={'telemetry/*'} element={<Telemetry />}>
                                    <Route index element={<Navigate to={'telemetry'} replace />} />
                                    <Route path={'*'} element={<NoAccessComponent />} />

                                    <Route path={'telemetry'} element={<TelemetryView />} />
                                    <Route path={'messages'} element={<Messages />} />
                                    <Route path={'archive'} element={<Archive />} />
                                    <Route path={'dashboard_nnb'} element={<DashboardNNB />} />
                                    <Route path={'operations'} element={<Operations />} />
                                    <Route path={'operation_time'} element={<OperationTime />} />
                                </Route>
                                <Route path={'reports/*'} element={<Reports />}>
                                    <Route index element={<Navigate to={'diagram_report'} replace />} />
                                    <Route path={'*'} element={<NoAccessComponent />} />

                                    <Route path={'diagram_report'} element={<DiagramReport />} />
                                    <Route path={'daily_report'} element={<DailyReport />} />
                                </Route>
                                <Route path={'analytics/*'} element={<Analytics />}>
                                    <Route index element={<Navigate to={'composite'} replace />} />
                                    <Route path={'*'} element={<NoAccessComponent />} />

                                    <Route path={'composite/*'} element={<WellCompositeEditor />} />
                                    <Route path={'statistics'} element={<Statistics />} />
                                </Route>
                                <Route path={'operations/*'} element={<WellOperations />}>
                                    <Route index element={<Navigate to={'tvd'} replace />} />
                                    <Route path={'*'} element={<NoAccessComponent />} />

                                    <Route path={'tvd'} element={<Tvd />} />
                                    <Route path={'sections'} element={<WellSectionsStat />} />
                                    <Route path={'plan'} element={<WellOperationsEditorPlan />} />
                                    <Route path={'fact'} element={<WellOperationsEditorFact />} />
                                    <Route path={'drillProcessFlow'} element={<DrillProcessFlow />} />
                                    <Route path={'params'} element={<WellDrillParams />} />
                                </Route>
                                <Route path={'document/*'} element={<Documents />} />
                                <Route path={'measure/*'} element={<Measure />} />
                                <Route path={'drillingProgram'} element={<DrillingProgram />} />
                                <Route path={'well_case'} element={<WellCase />} />
                            </Route>
                        </Route>
                    </Routes>
                </Router>
            </Suspense>
        </RootPathContext.Provider>
    </ConfigProvider>
))

export default App
