import React, { useEffect, useState } from 'react';
import logo from './logo.svg';
import './App.css';
import { WebsocketPlayingFieldComponent } from './components/PlayingField/PlayingField';
import { Client } from './client';
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
import { GmPlayingFieldComponent } from './components/GmPlayingField/GmPlayingField';

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: <GmPlayingFieldComponent client={client} />
            },
            {
                path: "/",
                element: websocketUrl ? <WebsocketPlayingFieldComponent websocketUrl={websocketUrl} /> : <div> </div>
            }
        ]);
    return (
        <div className="App">
            <RouterProvider router={router} />
        </div>
    );
}

export default App;
