var express = require('express');
var router = express.Router();
var ntr = require("../neutral-functions.js")

/* GET home page. */
router.get('/', function(req, res, next) {

  
    var check = ntr.checkToken(req, res) 
  
    if(check.name != false) {
  
      res.redirect(302, "/")
     
    } else {
  
      res.render('login', {error: ""});
    }
  
});

router.post("/", function(req, res, next) {

    var check = ntr.checkToken(req, res) 
    
    if(check.name == false) {
  
      res.send({"result":"failed", "content":"ERROR_TOKEN_NOT_VALID"})
      
      
    } else {

        if(check.permLevel == 4) {

            const bod = req.body

            if(bod.request == "add") {

               

                var level = 1;

                if(bod.value.permLevel == 'Niveau 1 : (Utilisateur)') {
                    level = 1

                } else if(bod.value.permLevel == 'Niveau 2 : (Editeur)') {
                    level = 2

                } else if(bod.value.permLevel == 'Niveau 3 : (Operateur)') {
                    level = 3

                } else if(bod.value.permLevel == 'Niveau 4 : (Administrateur)') {
                    level = 4

                } 

                const resp = ntr.createUser(bod.value.username, bod.value.password, level, bod.value.fullname)

                if(resp == "USER_ALREADY_EXIST") {

                    res.send({"result":"success", "content":"L'utilisateur existe déjà."})


                } else if(resp == 'USER_CREATED') {

                    res.send({"result":"success", "content":"VALID"})
                } else {

                    res.send({"result":"failed", "content":"ERROR_USER_NOT_WORK", "additional": "REQUEST_MISSING"})
                }



            } else if(bod.request == "get") {

               
                var users = []

                for(var element of ntr.getUsers().keys()) {

                    users.push(element)

                };

                res.send({"result": "success", "content": users})
            
            } else if(bod.request == "del") {

                const resp = ntr.deleteUser(bod.value)

                if(resp == "USER_ALREADY_DELETED") {

                    res.send({"result":"success", "content":"L'utilisateur n'existe déjà plus."})


                } else if(resp == 'USER_DELETED') {

                    res.send({"result":"success", "content":"VALID"})
                } else {

                    res.send({"result":"failed", "content":"ERROR_USER_NOT_WORK", "additional": "REQUEST_MISSING"})
                }

            
            
            } else if(bod.request == "deco") {

                ntr.deleteToken(bod.value)
                res.send({"result":"success", "content":"VALID"})
            } else if(bod.request == "edit") {

                const resp = ntr.editUser(bod.value)

                if(resp == true) {
                    res.send({"result":"success","content":"DATA_VALID"})

                } else {

                    res.send({"result":"success","content":"EDIT_USER_FAILED"})
                }

                
            
            }else {

                res.send({"result":"failed", "content":"ERROR_REQUEST_MISSING_FIELDS", "additional": "REQUEST_MISSING"})
            }

          
           
        } else {

            res.send({"result":"failed", "content":"ERROR_USER_PERMISSION_TOO_LOW"})
        }

    }

})

module.exports = router;
