var fs = require('fs');
var connect = require('connect');
var ConfigFile = require('./config/file');
var ConfigRedis = require('./config/redis');
var StoreLocal = require('./store/local');
var StoreS3 = require('./store/s3');
var middleware = require('./middleware');

var SuperstaticServer = function (options) {
  this._server = options.server;
  this._port = options.port;
  this._host = options.host;
  
  this.settings = options.settings;
  this.store = options.store;
  this.routes = options.routes;
};

SuperstaticServer.createServer = function (serverOptions) {
  var settings;
  var store;
<<<<<<< HEAD
  var routes = serverOptions.routes || {};
  
  // TODO: put this in a factory?
  // or is this the factory?
=======
  var routes = serverOptions.routes || [];
>>>>>>> 49951a8f2a122464fdb371c1b1115cecbe52b5d1
  
  if (serverOptions.settings.type === 'file') {
    settings = new ConfigFile(serverOptions.settings.options);
  }
  
  if (serverOptions.settings.type === 'redis') {
    settings = new ConfigRedis(serverOptions.settings.options);
  }
  
  if (serverOptions.store.type === 'local') {
    store = new StoreLocal(serverOptions.store.options);
  }
  
  if (serverOptions.store.type === 's3') {
    store = new StoreS3(serverOptions.store.options); 
  }
  
  var server = new SuperstaticServer({
    settings: settings,
    store: store,
    routes: routes,
    port: serverOptions.port,
    host: serverOptions.host,
  });
  
  return server;
};

SuperstaticServer.prototype._createServer = function () {
  // The order of these middleware modules is DEATHLY important!!!
  return this._server = connect()
<<<<<<< HEAD
    .use(connect.logger())
=======
>>>>>>> 49951a8f2a122464fdb371c1b1115cecbe52b5d1
    .use(connect.query())
    .use(middleware.router(this.settings, this.store, this.routes))
    .use(middleware.restful())
    .use(middleware.settingsCache)
    .use(middleware.static)
    .use(middleware.customRoute)
    .use(middleware.directoryIndex)
    .use(middleware.cleanUrls)
    .use(middleware.removeTrailingSlash)
    .use(connect.compress())
    .use(middleware.notFound)
    .use(middleware.responder);
};

SuperstaticServer.prototype.start = function (callback) {
  this._createServer();
  var openServer = this._openServer = this._server.listen(this._port, this._host, callback);
  openServer._connects = openServer._connects || {};
  
  // Track all connections
  openServer.on('connection', function (connection) {
    var key = connection.remoteAddress + ':' + connection.remotePort;
    openServer._connects[key] = connection;
    
    connection.once('close', function() {
      delete openServer._connects[key];
    });
  });
};

SuperstaticServer.prototype.stop = function (callback) {
  var openServer = this._openServer;
  
  // Close all connections
  Object.keys(openServer._connects).forEach(function (key) {
    var connection = openServer._connects[key];
    return connection && connection.destroy();
  });
  
  openServer.close(function () {
    openServer.removeAllListeners();
    callback();
  });
};

SuperstaticServer.prototype.route = function (routeOptions) {
  this.routes.push(routeOptions);
};

module.exports = SuperstaticServer;
@@ -596,229 +596,50 @@
 re;%0A
-%3C%3C%3C%3C%3C%3C%3C HEAD%0A  var routes = serverOptions.routes %7C%7C %7B%7D;%0A  %0A  // TODO: put this in a factory?%0A  // or is this the factory?%0A=======%0A  var routes = serverOptions.routes %7C%7C %5B%5D;%0A%3E%3E%3E%3E%3E%3E%3E 49951a8f2a122464fdb371c1b1115cecbe52b5d1
+  var routes = serverOptions.routes %7C%7C %5B%5D;
 %0A  %0A

Fix conflic