'use strict';
var http = require('http');
var server;
var config = require('config');
var UrlMapper = require('./url_mapper');
var hoistModel = require('hoist-model');
var mongoose = hoistModel._mongoose;
var ApplicationEvent = require('broker/lib/event_types/application_event');
var EventBroker = require('broker/lib/event_broker');
var hoistErrors = require('hoist-errors');
var BBPromise = require('bluebird');
var url = require('url');
var _ = require('lodash');
var logger = require('hoist-logger');
var anyBody = BBPromise.promisify(require('body/any'));
var server = {
  processRequest: function (request, response) {
    var path = url.parse(request.url).pathname;
    BBPromise.try(function parseBody() {

        if (request.method === 'POST') {
          return anyBody(request, response)
            .then(function (body) {
              request.body = body;
            });
        } else {
          return BBPromise.resolve(null);
        }
      })
      .then(function findApplication() {
        return hoistModel.Application.findAsync(UrlMapper.queryFromPath(path));
      })
      //create event for application
      .then(function generateEvent(applications) {
        if (!applications || applications.length < 1) {
          throw new hoistErrors.model.application.NotFoundError();
        }

        var application = applications[0];
        var environment = UrlMapper.environmentFromPath(path);

        var endpoints = application.settings[environment].endpoints;
        var route = UrlMapper.routeFromPath(endpoints, request);
        if (!route) {
          throw new hoistErrors.Http404Error('No Endpoint Found');
        }
        var payload = _.clone(route, true);
        delete payload.event;
        delete payload.method;
        delete payload.authenticate;
        if (request.method === 'POST') {
          // payload = _.extend(_.clone(route, true), request.body);
          payload = _.extend(payload, request.body);
        }

        var sanitizedUrl = UrlMapper.sanitisePath(path).path;

        payload._request = {
          body: request.body,
          headers: request.headers,
          url: sanitizedUrl,
          method: request.method
        };
        delete payload._request.headers.host;

        var ev = new ApplicationEvent({
          applicationId: application._id,
          eventName: route.event,
          environment: environment,
          correlationId: require('uuid').v4(),
          payload: payload
        });
        return ev;
      })
      //post event
      .then(function publishEvent(ev) {
        logger.debug('publishing event', ev);
        logger.info('publishing event',ev.messageId);
        return EventBroker.publish(ev).then(function () {
          return ev;
        });
      }).then(function generateResponse(ev) {
        response.setHeader('X-Hoist-CID', ev.correlationId);
        response.setHeader('X-Hoist-EID', ev.messageId);
        response.writeHead(200);
        response.end();
      }).catch(function processError(err) {

        if (!hoistErrors.isError(err)) {
          logger.error(err);
          err = new hoistErrors.Http500Error(err.message ||
            http.STATUS_CODES[500] + ' - ' + err.name);
        } else {
          logger.warn(err);
        }
        response.writeHead(err.status);
        response.end(err.message);
      }).done();
  },
  createServer: function () {
    return http.createServer(server.processRequest);
  },
  start: function () {
    mongoose.connect(config.get('Hoist.mongo.db'), function () {
      server._server = server.createServer();
      server._server.listen(config.get('Hoist.http.port'), function (err) {
        /* istanbul ignore next */
        if (err) {
          console.error('unable to listen to port', config.get('Hoist.http.port'), err);
          mongoose.disconnect(function () {
            delete server._server;
          });
          return;
        }
        console.log('server listening at port', config.get('Hoist.http.port'));
      });
    });
  },
  /* istanbul ignore next */
  stop: function (cb) {
    /* istanbul ignore next */
    if (server._server) {
      console.log('closing server');
      server._server.close(function () {
        delete server._server;
        mongoose.disconnect(function () {
          cb();
        });
      });
    } else {
      cb();
    }
  }
};
module.exports = server;

@@ -2591,16 +2591,27 @@
 r.debug(
+%7Bevent:ev%7D,
 'publish
@@ -2620,20 +2620,16 @@
 g event'
-, ev
 );%0A     
@@ -2643,16 +2643,39 @@
 er.info(
+%7BeventId:ev.messageId%7D,
 'publish
@@ -2688,21 +2688,8 @@
 ent'
-,ev.messageId
 );%0A 
@@ -2819,24 +2819,137 @@
 sponse(ev) %7B
+%0A%0A        logger.info(%7BeventId:ev.messageId%7D,'sending event response');%0A        logger.debug(%7Bevent:ev%7D,'event');
 %0A        res

modify logging slightly