'use strict';
var P = require('bluebird')
var bodyParser = require('body-parser')
var express = require('express')
var expressSession = require('express-session')
var flash = require('connect-flash')
var fs = require('graceful-fs')
var http = require('http')
var worker = require('infant').worker
var moment = require('moment')
var morgan = require('morgan')
var path = require('path')
var S = require('string')
var RedisStore = require('connect-redis')(expressSession)
var URI = require('URIjs')

var app = express()
var config = require('../config')
var routes = require('./routes')
var server = http.createServer(app)
var sessionStore = new RedisStore()

var sequelize = require('../helpers/sequelize')()

var viewFolder = __dirname + '/views'

//make some promises
P.promisifyAll(server)

// middleware stack
//app.disable('etag')
app.set('trust proxy',true)
app.set('views',viewFolder)
app.set('view engine','jade')
app.use(morgan('dev'))
app.use(express.static(__dirname + '/public',{maxAge: 3600000}))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))


/**
 * Global template vars
 * @type {object}
 * @return {string}
 */
app.locals = {
  moment: moment,
  app: {
    title: config.title
  }
}

// development only
if('development' === app.get('env')){
  /**
   * Setup pretty code in dev
   * @type {boolean}
   */
  app.locals.pretty = true
}

//static pages
app.get('/',routes.index)

//render stuff out of the views folder
app.get('/:view',function(req,res){
  if(
    fs.existsSync(path.resolve(path.join(viewFolder,req.params.view + '.jade')))
  ){
    res.render(req.params.view)
  } else {
    res.status(404).end('File not found')
  }

})


/**
 * Start main
 * @param {function} done
 */
exports.start = function(done){
  server.listenAsync(+config.main.port,config.main.host)
    .then(function(){
      done()
    })
    .catch(done)
}


/**
 * Stop main
 * @param {function} done
 */
exports.stop = function(done){
  server.close()
  done()
}

if(require.main === module){
  worker(
    server,
    config.name + ':main:worker',
    function(done){
      exports.start(done)
    },
    function(done){
      exports.stop(done)
    }
  )
}

@@ -112,93 +112,8 @@
 s')%0A
-var expressSession = require('express-session')%0Avar flash = require('connect-flash')%0A
 var 
@@ -136,24 +136,24 @@
 aceful-fs')%0A
+
 var http = r
@@ -297,119 +297,8 @@
 th')
-%0Avar S = require('string')%0Avar RedisStore = require('connect-redis')(expressSession)%0Avar URI = require('URIjs')
 %0A%0Ava
@@ -422,94 +422,8 @@
 pp)%0A
-var sessionStore = new RedisStore()%0A%0Avar sequelize = require('../helpers/sequelize')()
 %0A%0Ava

drop unused deps