import React 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 } from 'react-router-dom';
import LoginWindow from './windows/login/LoginWindow';
import { useAuth } from '../lib/useAuth';
import RegisterWindow from './windows/register/RegisterWindow';

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 />
        <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="/query">
                    <AppBar selectedTab={AppBarTab.Query} />
                    <QueryWindow />
                </PrivateRoute>
                <PrivateRoute path="/artist/:id">
                    <AppBar selectedTab={null} />
                    <ArtistWindow />
                </PrivateRoute>
                <PrivateRoute path="/tag/:id">
                    <AppBar selectedTab={null} />
                    <TagWindow />
                </PrivateRoute>
                <PrivateRoute path="/album/:id">
                    <AppBar selectedTab={null} />
                    <AlbumWindow />
                </PrivateRoute>
                <PrivateRoute path="/song/:id">
                    <AppBar selectedTab={null} />
                    <SongWindow />
                </PrivateRoute>
                <PrivateRoute path="/tags">
                    <AppBar selectedTab={AppBarTab.Tags} />
                    <ManageTagsWindow />
                </PrivateRoute>
            </Switch>
        </BrowserRouter>
    </ThemeProvider>
}