var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var bodyParser = require('body-parser');

var api = require('./routes/api');
var faceBK = require('./auth/facebook');
var app = express();
var session = require('express-session');

//var http = require("http").Server(app).listen(80);
//console.log("Server Started!");
//DB

//file
var upload = require("express-fileupload");
//var morgan = require('morgan');
var mongoose = require('mongoose');
var passport = require('passport');
//var session = require('express-session');
var config = require('./config/database');

//DB
//mongoose.Promise = require('bluebird');
mongoose.connect(config.database);
app.use(function(req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
    res.setHeader('Access-Control-Allow-Credentials', true);
    next();
});
app.use(session({
  secret: 's3cr3t',
  resave: true,
  saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());

app.use(logger('dev'));
app.use(bodyParser.json());

// app.use(upload());

app.use(bodyParser.urlencoded({'extended':'false'}));
//app.use(express.static(path.join(__dirname, 'dist')));
app.use(express.static(path.join(__dirname, 'other')));
//bizarrr

app.use('/public',express.static(path.join(__dirname, './public')));
app.use('/books', express.static(path.join(__dirname, 'dist')));

app.use('/home', express.static(path.join(__dirname, 'dist')));
app.use('/main/:id', express.static(path.join(__dirname, 'dist')));
app.use('/login', express.static(path.join(__dirname, 'dist')));
app.use('/signup', express.static(path.join(__dirname, 'dist')));
app.use('/download/:id', express.static(path.join(__dirname, 'dist')));

app.use('/api', api);
app.set('view engine', 'pug');
//app.use('/api', faceBK);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});


module.exports = app;
