'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.add = add;
exports.deactivate = deactivate;
exports.activate = activate;
exports.remove = remove;
exports.update = update;
exports.updatePassword = updatePassword;

var _cli = require('../../cli');

function add(newUser) {
  var xss = _cli.coreUtils.text.checkXss(newUser);
  if (xss.success === 0) {
    return xss;
  }
  var sameEmail = _cli.User.utils.checkSameEmail(newUser);
  if (sameEmail.success === 0) {
    return sameEmail;
  }

  _cli.User.utils.getRole(newUser);
  var bdd = _cli.User.manager.instance.get();
  var lastId = 0;
  for (var i = 0, len = bdd.length; i < len; i++) {
    lastId = parseInt(bdd[i].id);
  }
  newUser.id = lastId + 1;
  newUser.actif = 0;
  var cPassword = _cli.User.utils.commonPassword(newUser);
  if (cPassword.success === 0) {
    return cPassword;
  }

  newUser.password = _cli.User.utils.encryptPassword(10, newUser.password);
  bdd.push(newUser);

  _cli.User.manager.instance.update(bdd);

  return {
    success: 1,
    user: newUser
  };
}

function deactivate(id) {
  var bdd = _cli.User.manager.instance.get();
  id = parseInt(id);
  for (var i = 0, len = bdd.length; i < len; i++) {
    var user = bdd[i];
    if (parseInt(user.id) === id) {
      bdd[i].actif = 0;
    }
  }
  _cli.User.manager.instance.update(bdd);
  return bdd;
}

function activate(id) {
  var bdd = _cli.User.manager.instance.get();
  id = parseInt(id);
  for (var i = 0, len = bdd.length; i < len; i++) {
    var user = bdd[i];
    if (parseInt(user.id) === id) {
      bdd[i].actif = 1;
    }
  }
  _cli.User.manager.instance.update(bdd);
  return bdd;
}

function remove(id) {
  var bdd = _cli.User.manager.instance.get();
  id = parseInt(id);
  var newBdd = [];
  for (var i = 0, len = bdd.length; i < len; i++) {
    var user = bdd[i];
    if (parseInt(user.id) !== id) {
      newBdd.push(user);
    }
  }
  bdd = newBdd;
  _cli.User.manager.instance.update(bdd);
  return bdd;
}

function update(data) {
  var xss = _cli.coreUtils.text.checkXss(data);
  if (xss.success === 0) {
    return xss;
  }
  var sameEmail = _cli.User.utils.checkSameEmail(data);
  if (sameEmail.success === 0) {
    return sameEmail;
  }

  _cli.User.utils.getRole(data);

  var bdd = _cli.User.manager.instance.get();
  var id = parseInt(data.id);
  for (var i = 0, len = bdd.length; i < len; i++) {
    var user = bdd[i];
    if (parseInt(user.id) === id) {
      Array.prototype.forEach.call(Object.keys(data), function (key) {
        user[key] = data[key];
      });
    }
  }
  bdd = _cli.User.manager.instance.update(bdd);

  return {
    success: 1,
    user: data
  };
}

function updatePassword(data, password) {
  var cPassword = _cli.User.utils.commonPassword(data);
  if (cPassword.success === 0) {
    return cPassword;
  }

  var bdd = _cli.User.manager.instance.get();
  var id = parseInt(data.id);
  for (var i = 0, len = bdd.length; i < len; i++) {
    var user = bdd[i];
    if (parseInt(user.id) === id) {
      user.password = _cli.User.utils.encryptPassword(10, password);
    }
  }

  bdd = _cli.User.manager.instance.update(bdd);

  return {
    success: 1,
    user: data
  };
}