import React, { useEffect } from 'react';
import { ThemeProvider, CssBaseline, createMuiTheme } from '@material-ui/core';
import { grey } from '@material-ui/core/colors';
import AppBar, { AppBarTab } from './appbar/AppBar';
import QueryWindow from './windows/query/QueryWindow';
import ArtistWindow from './windows/artist/ArtistWindow';
import AlbumWindow from './windows/album/AlbumWindow';
import TagWindow from './windows/tag/TagWindow';
import SongWindow from './windows/song/SongWindow';
import ManageTagsWindow from './windows/manage_tags/ManageTagsWindow';
import { BrowserRouter, Switch, Route, Redirect, useHistory } from 'react-router-dom';
import LoginWindow from './windows/login/LoginWindow';
import { useAuth, ProvideAuth } from '../lib/useAuth';
import RegisterWindow from './windows/register/RegisterWindow';
import SettingsWindow from './windows/settings/SettingsWindow';
import { ErrorBoundary } from 'react-error-boundary';
import { ProvideIntegrations } from '../lib/integration/useIntegrations';
import ManageLinksWindow from './windows/manage_links/ManageLinksWindow';
import ManageWindow, { ManageWhat } from './windows/manage/ManageWindow';

const darkTheme = createMuiTheme({
    palette: {
        type: 'dark',
        primary: {
            main: grey[100],
        }
    },
});

function PrivateRoute(props: any) {
    const { children, ...rest } = props;
    let auth = useAuth();
    return <Route {...rest}
        render={({ location }) =>
            auth.user ? (
                children
            ) : (
                    <Redirect
                        to={{
                            pathname: "/login",
                            state: { from: location }
                        }}
                    />
                )
        }
    />
}

export default function MainWindow(props: any) {
    return <ThemeProvider theme={darkTheme}>
        <CssBaseline />
        <ProvideAuth>
            <ProvideIntegrations>
                <BrowserRouter>
                    <Switch>
                        <Route exact path="/">
                            <Redirect to={"/query"} />
                        </Route>
                        <Route path="/login">
                            <AppBar selectedTab={null} />
                            <LoginWindow />
                        </Route>
                        <Route path="/register">
                            <AppBar selectedTab={null} />
                            <RegisterWindow />
                        </Route>
                        <PrivateRoute path="/settings">
                            <AppBar selectedTab={null} />
                            <SettingsWindow />
                        </PrivateRoute>
                        <PrivateRoute path="/query">
                            <AppBar selectedTab={AppBarTab.Query} />
                            <QueryWindow />
                        </PrivateRoute>
                        <PrivateRoute path="/artist/:id">
                            <AppBar selectedTab={AppBarTab.Browse} />
                            <ArtistWindow />
                        </PrivateRoute>
                        <PrivateRoute path="/tag/:id">
                            <AppBar selectedTab={AppBarTab.Browse} />
                            <TagWindow />
                        </PrivateRoute>
                        <PrivateRoute path="/album/:id">
                            <AppBar selectedTab={AppBarTab.Browse} />
                            <AlbumWindow />
                        </PrivateRoute>
                        <PrivateRoute path="/song/:id">
                            <AppBar selectedTab={AppBarTab.Browse} />
                            <SongWindow />
                        </PrivateRoute>
                        <PrivateRoute path="/manage/tags">
                            <AppBar selectedTab={AppBarTab.Manage} />
                            <ManageWindow selectedWindow={ManageWhat.Tags} />
                        </PrivateRoute>
                        <PrivateRoute path="/manage/links">
                            <AppBar selectedTab={AppBarTab.Manage} />
                            <ManageWindow selectedWindow={ManageWhat.Links} />
                        </PrivateRoute>
                        <PrivateRoute exact path="/manage">
                            <Redirect to={"/manage/tags"} />
                        </PrivateRoute>
                    </Switch>
                </BrowserRouter>
            </ProvideIntegrations>
        </ProvideAuth>
    </ThemeProvider >
}