var model = require('../model');
var defaultCommands = require('./default-commands/rest');

function createHandler(params) {
  params = params || {};
  var connection = params.connection;
  var command = params.command;
  var requestBuilder = params.requestBuilder;
  var responseBuilder = params.responseBuilder;

  if (!requestBuilder)
    throw new Error('no request builder for ' + command);

  return (function(request, response) {
    var message = requestBuilder(request);
    var timeout = message.timeout || null;
    connection.emitMessage(
      command,
      message,
      function(error, responseMessage) {
        if (error) {
          var body = responseMessage && responseMessage.body || null;
          response.contentType('application/json');
          response.send(body, error);
        } else {
          var body = responseMessage.body;
          if (responseBuilder)
            body = responseBuilder(body);
          response.contentType('application/json');
          response.send(body, 200);
        }
      },
      { timeout: timeout }
    );
  });
}
exports.createHandler = createHandler;

function getRegisterationMethod(method) {
  method = method || 'GET';
  switch (method.toUpperCase()) {
    case 'PUT':    return 'put';
    case 'POST':   return 'post';
    case 'DELETE': return 'del';
    default:       return 'get';
 }
}
exports.getRegisterationMethod = getRegisterationMethod;

exports.register = function(application, params) {
  params = params || {};
  var connection = params.connection;
  if (!connection)
    throw new Error('Connection to the backend is required!');

  var prefix = params.prefix || '';
  prefix = prefix.replace(/\/$/, '');

  var commandSets = [defaultCommands].concat(params.plugins || []);
  var unifiedCommandSet = {};
  commandSets.forEach(function(commandSet) {
    Object.keys(commandSet).forEach(function(command) {
      var definition = commandSet[command];
      if (!model.isA(definition, model.REST))
        return;
      unifiedCommandSet[command] = definition;
    });
  });

  var registeredCommands = [];
  Object.keys(unifiedCommandSet).forEach(function(command) {
    var definition = unifiedCommandSet[command];
    if (!model.isA(definition, model.REST))
      return;
    var method = getRegisterationMethod(definition.method);
    var handler = createHandler({
      connection:      connection,
      command:         definition.command || command,
      requestBuilder:  definition.requestBuilder,
      responseBuilder: definition.responseBuilder
    });
    application[method](prefix + definition.path, handler);
    registeredCommands.push({ command:    command,
                              definition: definition,
                              handler:    handler });
  });
  return registeredCommands;
}

@@ -223,118 +223,68 @@
 var 
-requestBuilder = params.requestBuilder;%0A  var responseBuilder = params.responseBuilder;%0A%0A  if (!requestBuilder
+definition = params.definition;%0A%0A  if (!definition.toBackend
 )%0A  
@@ -309,27 +309,31 @@
 'no 
-request build
+filt
 er for
+ the backend:
  ' +
@@ -402,30 +402,36 @@
 ssage = 
-requestBuilder
+definition.toBackend
 (request
@@ -830,31 +830,35 @@
     if (
-responseBuilder
+definition.toClient
 )%0A      
@@ -870,31 +870,35 @@
  body = 
-responseBuilder
+definition.toClient
 (body);%0A
@@ -2335,21 +2335,16 @@
 nection:
-     
  connect
@@ -2362,21 +2362,16 @@
 command:
-     
     defi
@@ -2407,101 +2407,30 @@
     
-requestBuilder:  definition.requestBuilder,%0A      responseBuilder: definition.responseBuilder
+definition: definition
 %0A   

Update REST adaptor for the new plugin model