var express = require('express');
var router = express.Router();
var password = require('bcrypt-password');
var request = require('request');
var HttpStatus = require('http-status-codes');

var API_URL = "http://skvs"

if (process.env.NODE_ENV == 'development') {
  API_URL = "http://127.0.0.1:8080"
}

function error(statusCode, message) {
  statusCode = statusCode == undefined ? 500 : statusCode;
  if (message == undefined) {
    message = HttpStatus.getStatusText(statusCode);
  }
  var err = new Error(message);
  err.status = statusCode;
  return  err;
}

router.post('/login', function (req, res, next) {
    var secret = req.body['password'];
    if (secret == undefined || secret == "") {
      res.json()
    } else {
      password.hash(secret, function (error, digest) {
        request({
          url: API_URL + '/password',
          json: true
        }, function (error, response, result) {
            if (response.statusCode === HttpStatus.NOT_FOUND) {
              next(error(HttpStatus.NOT_FOUND, 'Password not set.'));
            } else if (response.statusCode === HttpStatus.OK) {
              var saved_digest = result['value'];

              password.check(digest, saved_digest, function (error, match) {
                if (match === true) {
                  req.session.logged_in = true;
                  res.json({status: "Ok"})
                } else {
                  // TODO: check for error?
                  next(error(HttpStatus.FORBIDDEN, 'Wrong Password, access denied.'));
                }
              });
            }
          }
        );
      });
    }
  }
);

router.post('/logout', function (req, res, next) {
    // TODO: Error handling ;)
    req.session = null;
    res.json({status: "Ok"})
  }
);


router.post('/password', function (req, res, next) {
    var secret = req.body['password'];
    if (secret != undefined && secret != "") {
      // TODO: Add more Password requirments!
      // * Check if password was confirmed
      // * Check if old password is correct!
      password.hash(secret, function (error, digest) {
        var options = {
          url: API_URL + '/password',
          method: 'POST',
          json: true,
          form: {
            value: digest
          }
        }
        request(options, function(err, response, result) {
          if (response.statusCode === HttpStatus.OK) {
            res.json({status: "Ok"})
          } else {
            next(error(HttpStatus.INTERNAL_SERVER_ERROR, 'Password could not be set.'));
          }
        });
      });
    } else {
      next(error(HttpStatus.NOT_FOUND, 'No password given.'));
    }
  }
);


module.exports = router;

@@ -310,16 +310,23 @@
 on error
+_helper
 (statusC
@@ -341,16 +341,16 @@
 sage) %7B%0A
-
   status
@@ -989,32 +989,39 @@
       next(error
+_helper
 (HttpStatus.NOT_
@@ -1460,32 +1460,39 @@
       next(error
+_helper
 (HttpStatus.FORB
@@ -2463,32 +2463,39 @@
       next(error
+_helper
 (HttpStatus.INTE
@@ -2588,24 +2588,24 @@
    %7D else %7B%0A
-
       next(e
@@ -2608,16 +2608,23 @@
 xt(error
+_helper
 (HttpSta

rename error handler to avoid name clash