module.exports = function(db){
   var express = require('express');
   var router = express.Router();
   var utils = require("../classes/utils");
   var errors = require("../classes/errors");

   // Create a new user
   router.post('/', function(req, res, next){
      var username = req.body.username;
      var password = req.body.password;

      if(utils.isStringNullOrEmpty(username)){
         res.status(400).send(errors.USERNAME_REQUIRED);
         return;
      }
      if(utils.validateTextLength(username, 4)){
         res.status(400).send(errors.USERNAME_LENGTH);
         return;
      }
      if(utils.isStringNullOrEmpty(password)){
         res.status(400).send(errors.PASSWORD_REQUIRED);
         return;
      }
      if(utils.validateTextLength(password, 6)){
         res.status(400).send(errors.PASSWORD_LENGTH);
         return;
      }

      // Before save the user check if the username is unique
      var usersDB = db.collection("users");
      var usersFound = usersDB.find({ "username": utils.generateRegExp(username) });
      usersFound.toArray(function(err, users){
         if(err){
            res.send(errors.DB_ERROR);
            return;
         }
         else if(users.length > 0){
            res.status(400).send(errors.USERNAME_EXSISTS)
         }
         else{
            // after validating the user save it
            var userToSave = {};
            userToSave.username = username;
            userToSave.password = password;
            usersDB.insert(userToSave);
            var sess = req.session;
            sess.userID = userToSave._id;
            sess.username =  userToSave.username;
            res.send(userToSave);
            return;
         }
      });
   });

   // Show the login page
   router.get("/login", function(req, res, next){
      res.render("users/login");
   });

   router.get("/create", function(req, res, next){
      res.render("users/create");
   });

   router.get('/logout', function(req, res, next){
      req.session.destroy(function(err){
         if(err){
            console.error(err);
            return;
         }
      });
      res.render("index");
   });

   // Login an exisiting user
   router.post('/login', function(req, res, next){
      var username = req.body.username;
      var password = req.body.password;

      if(utils.isStringNullOrEmpty(username)){
         res.status(400).send(errors.USERNAME_REQUIRED);
         return;
      }
      if(utils.isStringNullOrEmpty(password)){
         res.status(400).send(errors.PASSWORD_REQUIRED);
         return;
      }

      var usersDB = db.collection("users");
      var usersFound = usersDB.find({
          "username": utils.generateRegExp(username),
          "password": password
       });
      usersFound.toArray(function(err, users){
         if(err){
            res.send(errors.DB_ERROR);
            return;
         }
         else if(users.length == 0){
            res.status(400).send(errors.USER_NOT_FOUND);
            return;
         }
         else{
            var sess = req.session;
            sess.userID = users[0]._id;
            sess.username =  users[0].username;
            res.sendStatus(200);
         }
      });
   });


   return router;
}
