var
  _ = require("lodash"),
  path = require("path"),
  schemas = require("./schemas"),
  ROOT = "/surrogate/",
  API = ROOT + "api/";


module.exports = function () {
  var self = this;

  this.server.route({
    method: "GET",
    path: ROOT + "{param*}",
    handler: {
      directory: {
        path: path.join(__dirname, "../frontend-dist")
      }
    }
  });

  this.server.route({
    method: "GET",
    path: API + "routes",
    handler: function (request, reply) {
      var routes = _.chain(self.routes)
        .cloneDeep()
        .map(function (route) {
          var handlers = _.map(route.handlers, function (handler, name) {
            return _.extend({}, handler, {
              options: _.keys(handler.options),
              name: name
            });
          });

          return {
            name: route.name,
            method: route.method,
            path: route.path,
            selectedHandler: route.selectedHandler,
            handlers: handlers
          };
        })
        .value();

      reply(routes)
        .type("application/json");
    }
  });

  this.server.route({
    method: "PUT",
    path: API + "route/handler",
    handler: function (request, reply) {
      var
        payload = request.payload,
        route = _.find(self.routes, { method: payload.method, path: payload.path });

      schemas.validateSetHandler(payload, route);
      route.selectedHandler = payload.selectedHandler;
      reply("OK");
    }
  });

  this.server.route({
    method: "PUT",
    path: API + "route/selected-option",
    handler: function (request, reply) {
      var
        payload = request.payload,
        route = _.find(self.routes, { method: payload.method, path: payload.path });

      schemas.validateSetOption(payload, route);

      route.handlers[payload.handler].selectedOption = payload.selectedOption;
      reply("OK");
    }
  });

  this.server.route({
    method: "PUT",
    path: API + "route/selected-options",
    handler: function (request, reply) {
      var
        payload = request.payload,
        route = _.find(self.routes, { method: payload.method, path: payload.path });

      schemas.validateSetOptions(payload, route);

      route.handlers[payload.handler].selectedOptions = payload.selectedOptions;
      reply("OK");
    }
  });

  this.server.route({
    method: "PUT",
    path: API + "delay",
    handler: function (request, reply) {
      schemas.validateApiUpdate(request.payload, schemas.delay);
      this.delay = request.payload.delay;
      reply("OK");
    }
  });

  this.server.route({
    method: "POST",
    path: API + "reset",
    handler: function (request, reply) {
      self.reset(true, function () {
        reply("OK");
      });
    }
  });
};

@@ -1,12 +1,47 @@
 var
+ frontendDir,%0A  fs = require(%22fs%22),
 %0A
-
   _ = re
@@ -165,16 +165,183 @@
 api/%22;%0A%0A
+frontendDir = fs.existsSync(path.join(__dirname, %22../frontend-release%22)) ?%0A  path.join(__dirname, %22../frontend-release%22) :%0A  path.join(__dirname, %22../frontend-dist%22);%0A
 %0Amodule.
@@ -506,41 +506,16 @@
 th: 
-path.join(__dirname, %22../
 frontend
 -dis
@@ -510,23 +510,19 @@
 frontend
--dist%22)
+Dir
 %0A      %7D

check for release directory to determine what frontend code to serve up