 ###
 
 _ = require "lodash"
+assert = require "assert"
 uuid = require "node-uuid"
 asciify = require "asciify"
 connect = require "connect"

http = require "http"
util = require "util"

# This middleware captures the relevant (and unrelevant) data when
# the request comes in and attaches the data to the request so it
# can later be used by whoever might needs this. At the moment it
# just captures some rudimentary data, such as timestamp and UUID.
module.exports.capture = (kernel) ->
    (request, response, next) ->
        sent = request.headersSent
        request.uuid = uuid.v1()
        request.date = new Date
        request.kernel = kernel
        next() unless sent

# This middleware is a little handy utility that merges the set
# of parameters, specifically the ones transferred via query or
# via body mechanism into one object that can be used to easily
# access the parameters without thinking about transfer mechanism.
module.exports.params = (kernel) ->
    (request, response, next) ->
        body = request.body or {}
        query = request.query or {}
        request.params = Object.create {}
        _.extend request.params, query
        _.extend request.params, body
        next() unless request.headersSent

# A middleware that adds a `redirect` method to the response object.
# This redirects to the supplied URL with the 302 status code and
# the corresponding reason phrase. This method also sets some of the
# necessary headers, such as nullary `Content-Length` and some other.
module.exports.redirect = (kernel) ->
    (request, response, next) ->
        response.redirect = (url, status) ->
            relocated = status or 302
            codes = http.STATUS_CODES
            message = codes[relocated]
            response.setHeader "Location", url
            response.setHeader "Content-Length", 0
            response.writeHead relocated, message
            response.end undefined
        next() unless request.headersSent

# A middleware that adds a `send` method to the response object.
# This allows for automatic setting of `Content-Type` headers

            _.find merged, handles
        next() unless request.headersSent

# A middleware that adds a `redirect` method to the response object.
# This redirects to the supplied URL with the 302 status code and
# the corresponding reason phrase. This method also sets some of the
# necessary headers, such as nullary `Content-Length` and some other.
module.exports.redirect = (kernel) ->
    (request, response, next) ->
        response.redirect = (url, status) ->
            relocated = status or 302
            codes = http.STATUS_CODES
            message = codes[relocated]
            response.setHeader "Location", url
            response.setHeader "Content-Length", 0
            response.writeHead relocated, message
            response.end undefined
            next() unless request.headersSent

# This middleware is a little handy utility that merges the set
# of parameters, specifically the ones transferred via query or
# via body mechanism into one object that can be used to easily
# access the parameters without thinking about transfer mechanism.
module.exports.params = (kernel) ->
    (request, response, next) ->
        body = request.body or {}
        query = request.query or {}
        request.params = Object.create {}
        _.extend request.params, query
        _.extend request.params, body
        next() unless request.headersSent

# This middleware captures the relevant (and unrelevant) data when
# the request comes in and attaches the data to the request so it
# can later be used by whoever might needs this. At the moment it
# just captures some rudimentary data, such as timestamp and UUID.
module.exports.capture = (kernel) ->
    (request, response, next) ->
        sent = request.headersSent
        request.uuid = uuid.v1()
        request.date = new Date
        request.kernel = kernel
        next() unless sent
