import {AuthRequiredMiddleware, Request, right, Route, SessionAuthMiddleware} from '@extollo/lib'
import {Dash} from '../controllers/Dash.controller'
import CobaltMiddleware from '../middlewares/Cobalt.middleware'
import {ResourceAPI} from '../controllers/cobalt/ResourceAPI.controller'
import {Interface} from '../controllers/cobalt/Interface.controller'
import {Dash2} from '../controllers/Dash2.controller'

const parseKey = (request: Request) => right(request.safe('key').string())
const parseId = (request: Request) => {
    request.safe('id').present()
    return right(request.input('id') as number|string)
}

Route
    .group('/dash2', () => {
        Route.get('/')
            .calls<Dash2>(Dash2, d => d.index)

        Route.get('/cobalt')
            .calls<Dash2>(Dash2, d => d.cobalt)

        Route.get('/cobalt/settings')
            .calls<Dash2>(Dash2, d => d.cobaltSettings)

        Route.get('/cobalt/resource/:key/list')
            .calls<Dash2>(Dash2, d => d.cobaltResourceList)
    })
    .pre(SessionAuthMiddleware)
    .pre(AuthRequiredMiddleware)
    .pre(CobaltMiddleware)

Route
    .group('/dash', () => {
        Route.get('/')
            .alias('@dash')
            .calls<Dash>(Dash, dash => dash.main)

        Route.group('/cobalt/resource', () => {
            Route.get('/:key/configure')
                .parameterMiddleware(parseKey)
                .calls(ResourceAPI, (api: ResourceAPI) => api.configure)

            Route.get('/:key')
                .parameterMiddleware(parseKey)
                .calls(ResourceAPI, (api: ResourceAPI) => api.read)

            Route.get('/:key/:id')
                .parameterMiddleware(parseKey)
                .parameterMiddleware(parseId)
                .calls(ResourceAPI, (api: ResourceAPI) => api.readOne)

            Route.put('/:key')
                .parameterMiddleware(parseKey)
                .passingRequest()
                .calls(ResourceAPI, (api: ResourceAPI) => api.create)

            Route.patch('/:key/:id')
                .parameterMiddleware(parseKey)
                .parameterMiddleware(parseId)
                .passingRequest()
                .calls(ResourceAPI, (api: ResourceAPI) => api.update)

            Route.delete('/:key/:id')
                .parameterMiddleware(parseKey)
                .parameterMiddleware(parseId)
                .calls(ResourceAPI, (api: ResourceAPI) => api.delete)
        })

        Route.get('/cobalt/listing/:key')
            .parameterMiddleware(parseKey)
            .calls(Interface, (i: Interface) => i.listing)

        Route.get('/cobalt/form/:key')
            .parameterMiddleware(parseKey)
            .calls(Interface, (i: Interface) => i.insertForm)

        Route.get('/cobalt/form/:key/:id')
            .parameterMiddleware(parseKey)
            .parameterMiddleware(parseId)
            .calls(Interface, (i: Interface) => i.updateForm)

        Route.get('/cobalt/form/:key/:id/view')
            .parameterMiddleware(parseKey)
            .parameterMiddleware(parseId)
            .calls(Interface, (i: Interface) => i.viewForm)
    })
    .pre(SessionAuthMiddleware)
    .pre(AuthRequiredMiddleware)
    .pre(CobaltMiddleware)
