"use strict"

var _ = require('underscore'),
  env = process.env.NODE_ENV || 'development',
  http = require('http'),
  Intercom = require('facet-intercom'),
  ResponseHandler = require('facet-response-handler'),
  Core = require('facet-core').Core;


var Platform = function() {
  // registry for storing uninstantiated api module classes
  this.apiClasses = {};

  // registry for storing api module instances
  this.apiInstances = {};

  this.moduleOptions = {
    // currently used to disbale mongoose's index checking at startup
    environment: process.env.NODE_ENV || 'development',
    
    // instance of EventEmitter2 used for inter/intra module communication
    intercom: new Intercom(),

    // allow toggling api user access checking for CRUD funcs
    doAccessCheck: false,

    // defines the middleware function format to use for auto route binding
    middlewareType: 'connect'

    // supported values are basic & jwt, basic is default
    // apiAuthMethod: 'basic',
    
    // optional, used for jwt implementation
    // apiSecret: 'SOME_SECRET_STRING',
  };
};



Platform.prototype.setModuleOptions = function(options) {
  this.moduleOptions = _.extend(this.moduleOptions, options);

  // ensure a db key exists
  if( !this.moduleOptions.db ) {
    var mongoose = require('mongoose');
    mongoose.connect( options.dbServer, { server: { socketOptions: { keepAlive: 1 } } });
    mongoose.connection.on( 'error', console.error.bind( console, 'connection error:' ) );  
    this.moduleOptions.db = mongoose;
  }

  return this;
};


Platform.prototype.useModules = function(modules) {
  if(!modules) {
    throw new Error('An object containing module aliases and classes is required.');
  }

  for(var key in modules) {
    this._apiClasses[key] = modules[key];
  }

  return this;
}

Platform.prototype.getModuleOptions = function() {
  return this.moduleOptions;
};


Platform.prototype.init = function(express) {
  // instantiate registered modules
  for(var key in this._apiClasses) {
    var apiClass = this._apiClasses[key];
    this._apiInstances[key] = new apiClass(this.getModuleOptions());
  }

  // instantiate response listener
  var responseHandler = new ResponseHandler(this.moduleOptions);
  
  // run the core facetInit class to emit the nodeStack object
  // which contains the req, res and next for the current request
  var first = Object.keys(this._apiInstances)[0];
  express.use(first.facetInit());
};


Platform.prototype.getModule = function(module, instantiated) {
  if(instantiated === undefined) {
    instantiated = true;
  }

  return (instantiated) ? this._apiInstances[module] : this._apiClasses[module];
}

module.exports = exports = Platform;

@@ -340,16 +340,17 @@
 %0A  this.
+_
 apiClass
@@ -413,16 +413,17 @@
 %0A  this.
+_
 apiInsta
@@ -433,32 +433,33 @@
 s = %7B%7D;%0A%0A  this.
+_
 moduleOptions = 
@@ -1144,24 +1144,25 @@
 s) %7B%0A  this.
+_
 moduleOption
@@ -1175,24 +1175,25 @@
 extend(this.
+_
 moduleOption
@@ -1242,24 +1242,25 @@
   if( !this.
+_
 moduleOption
@@ -1496,24 +1496,25 @@
   %0A    this.
+_
 moduleOption
@@ -1868,24 +1868,25 @@
 return this.
+_
 moduleOption
@@ -2212,16 +2212,17 @@
 er(this.
+_
 moduleOp
@@ -2411,16 +2411,19 @@
 es)%5B0%5D;%0A
+  %0A
   expres
@@ -2428,21 +2428,41 @@
 ess.use(
+this._apiInstances%5B
 first
+%5D
 .facetIn

fix init process