import React from 'react'
import { Route, IndexRoute } from 'react-router'
import CoreLayout from 'layouts/CoreLayout'

import {
    TerminalView,
    BasicSQLiView,
    NetworkScanningView,
    LoginView,
    BrowserView,
    DesignView,
    VLabView,
    VLTypesListView,
    VLLevelsView,
    IoTyLabView
} from 'views'

import {
  // Browser,
  CodeEditor
} from 'components'

import {auth} from 'utils/auth'

function checkAuth (nextState, replace) {
  if (auth.loggedIn()) {
    replace({
      pathname: '/labs',
      state: { nextPathname: nextState.location.pathname }
    })
  }
}

function requireAuth (nextState, replace) {
  if (!auth.loggedIn()) {
    replace({
      pathname: '/login',
      state: { nextPathname: nextState.location.pathname }
    })
  }
}

const routes = [
  <Route path='/terminal/:vlqType' component={TerminalView} onEnter={requireAuth} />,
  <Route path='/basicsqli' component={BasicSQLiView} onEnter={requireAuth} />,
  <Route path='/network-scanning' component={NetworkScanningView} onEnter={requireAuth} />,
  <Route path='/iot' component={IoTyLabView} />
]

// Categorize routes. Sort them in alphabetical order for each category
export default (store) => (

  <Route component={CoreLayout} path='/'>
    <IndexRoute component={LoginView} onEnter={checkAuth} />
    <Route path='/login' component={LoginView} onEnter={checkAuth} />
    <Route path='/labs' component={VLTypesListView} onEnter={requireAuth} />
    <Route path='/levels' component={VLLevelsView} onEnter={requireAuth} />
    <Route path='/vl' component={VLabView} onEnter={requireAuth} />

    <Route path='/terminal/:vlqType' component={TerminalView} onEnter={requireAuth} />
    <Route path='/basicsqli' component={BasicSQLiView} onEnter={requireAuth} />
    <Route path='/network-scanning' component={NetworkScanningView} onEnter={requireAuth} />
    <Route path='/iot' component={IoTyLabView} />

    <Route path='/browser' component={BrowserView} />
    <Route path='/browser?q=:url' component={BrowserView} />
    <Route path='/code' component={CodeEditor} />
    <Route path='/design' component={DesignView} />
  </Route>
)
