import React, { useEffect, useState } from 'react';
import './App.css';
import { Client } from './client';
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
import { DesignPage } from './views/Design/Design';
import { GmView } from './views/GmView/GmView';
import { WebsocketProvider } from './components/WebsocketProvider';
import { PlayerView } from './views/PlayerView/PlayerView';
import Candela from './plugins/Candela';

interface AppProps {
    client: Client;
}

const App = ({ client }: AppProps) => {
    console.log("rendering app");
    const [websocketUrl, setWebsocketUrl] = useState<string | undefined>(undefined);

    useEffect(() => {
        client.registerWebsocket().then((url) => setWebsocketUrl(url))
    }, [client]);

    let router =
        createBrowserRouter([
            {
                path: "/gm",
                element: websocketUrl ? <WebsocketProvider websocketUrl={websocketUrl}> <GmView client={client} /> </WebsocketProvider> : <div> </div>
            },
            {
                path: "/",
                element: websocketUrl ? <WebsocketProvider websocketUrl={websocketUrl}> <PlayerView client={client} /> </WebsocketProvider> : <div> </div>
            },
            {
                path: "/candela",
                element: <Candela.CharsheetElement />
            },
            {
                path: "/design",
                element: <DesignPage />
            }
        ]);
    return (
        <div className="App">
            <RouterProvider router={router} />
        </div>
    );
}

export default App;
