import React from "react";
import {
  Route,
  RouterProvider,
  createBrowserRouter,
  createRoutesFromElements,
} from "react-router-dom";
import "./App.css";
import { AuthApi } from "./api/AuthApi";
import { ServerApi } from "./api/ServerApi";
import {
  CreateApiTokenRoute,
  EditApiTokenRoute,
} from "./routes/EditAPITokenRoute";
import {
  CreateNWFilterRoute,
  EditNWFilterRoute,
} from "./routes/EditNWFilterRoute";
import {
  CreateNetworkRoute,
  EditNetworkRoute,
} from "./routes/EditNetworkRoute";
import { CreateVMRoute, EditVMRoute } from "./routes/EditVMRoute";
import { HomeRoute } from "./routes/HomeRoute";
import { IsoFilesRoute } from "./routes/IsoFilesRoute";
import { NetworkFiltersListRoute } from "./routes/NetworkFiltersListRoute";
import { NetworksListRoute } from "./routes/NetworksListRoute";
import { NotFoundRoute } from "./routes/NotFound";
import { SysInfoRoute } from "./routes/SysInfoRoute";
import { TokensListRoute } from "./routes/TokensListRoute";
import { VMListRoute } from "./routes/VMListRoute";
import { VMRoute } from "./routes/VMRoute";
import { VNCRoute } from "./routes/VNCRoute";
import { ViewApiTokenRoute } from "./routes/ViewApiTokenRoute";
import { ViewNWFilterRoute } from "./routes/ViewNWFilterRoute";
import { ViewNetworkRoute } from "./routes/ViewNetworkRoute";
import { LoginRoute } from "./routes/auth/LoginRoute";
import { OIDCCbRoute } from "./routes/auth/OIDCCbRoute";
import { BaseAuthenticatedPage } from "./widgets/BaseAuthenticatedPage";
import { BaseLoginPage } from "./widgets/BaseLoginPage";

interface AuthContext {
  signedIn: boolean;
  setSignedIn: (signedIn: boolean) => void;
}

const AuthContextK = React.createContext<AuthContext | null>(null);

export function App() {
  const [signedIn, setSignedIn] = React.useState(AuthApi.SignedIn);

  const context: AuthContext = {
    signedIn: signedIn,
    setSignedIn: (s) => {
      setSignedIn(s);
      location.reload();
    },
  };

  const router = createBrowserRouter(
    createRoutesFromElements(
      signedIn || ServerApi.Config.auth_disabled ? (
        <Route path="*" element={<BaseAuthenticatedPage />}>
          <Route path="" element={<HomeRoute />} />

          <Route path="iso" element={<IsoFilesRoute />} />

          <Route path="vms" element={<VMListRoute />} />
          <Route path="vms/new" element={<CreateVMRoute />} />
          <Route path="vm/:uuid" element={<VMRoute />} />
          <Route path="vm/:uuid/edit" element={<EditVMRoute />} />
          <Route path="vm/:uuid/vnc" element={<VNCRoute />} />

          <Route path="net" element={<NetworksListRoute />} />
          <Route path="net/new" element={<CreateNetworkRoute />} />
          <Route path="net/:uuid" element={<ViewNetworkRoute />} />
          <Route path="net/:uuid/edit" element={<EditNetworkRoute />} />

          <Route path="nwfilter" element={<NetworkFiltersListRoute />} />
          <Route path="nwfilter/new" element={<CreateNWFilterRoute />} />
          <Route path="nwfilter/:uuid" element={<ViewNWFilterRoute />} />
          <Route path="nwfilter/:uuid/edit" element={<EditNWFilterRoute />} />

          <Route path="tokens" element={<TokensListRoute />} />
          <Route path="token/new" element={<CreateApiTokenRoute />} />
          <Route path="token/:id" element={<ViewApiTokenRoute />} />
          <Route path="token/:id/edit" element={<EditApiTokenRoute />} />

          <Route path="sysinfo" element={<SysInfoRoute />} />
          <Route path="*" element={<NotFoundRoute />} />
        </Route>
      ) : (
        <Route path="*" element={<BaseLoginPage />}>
          <Route path="" element={<LoginRoute />} />
          <Route path="oidc_cb" element={<OIDCCbRoute />} />
          <Route path="*" element={<NotFoundRoute />} />
        </Route>
      )
    )
  );

  return (
    <AuthContextK value={context}>
      <RouterProvider router={router} />
    </AuthContextK>
  );
}

export function useAuth(): AuthContext {
  return React.use(AuthContextK)!;
}
