import { h, render } from 'preact'
import { Provider } from 'react-redux'
import { BrowserRouter, Switch, Route, Redirect } from 'react-router-dom'

import { store, history } from './store'
import * as socket from './socket'
import util from './util'

import Auth from './auth'
import { Header, AudioPlayer } from './common'
import System from './system/system.component'
import Dashboard from './dashboard/dashboard.component'
import modules from './modules'

const module_list = Object.keys(modules).map(name => {
  const module = modules[name]
  return (
    <Route path={'/' + module.name} component={module.router} />
  )
})

const ModuleRouter = () => {
  return (
    <Switch></Switch>
  )  
}

const app = (
  <Provider store={store}>
    <Auth.Gate>
      <BrowserRouter>
        <div className='everybody'>
          <Route path='/' children={(props) => <div>{console.log(props.location.pathname)}</div>} />
          <Route exact path='/system' component={System} />
          <Route exact path='/dashboard' component={Dashboard} />
          <Route exact path='/logout' component={Auth.Logout} />
          <Route exact path='/login' component={() => { console.log('pziss'); <Redirect to='/' /> }} />
          <Route exact path='/signup' component={() => { console.log('pziss'); <Redirect to='/' /> }} />
          {module_list}
          <Route exact path='/' component={Dashboard} />
          <Route path='/' component={Header} />
        </div>
      </BrowserRouter>
      <AudioPlayer />
    </Auth.Gate>
  </Provider>
)

render(app, document.getElementById('container'))
