var IdentityType = require('../models/IdentityType');
var Util = require('./Util');
var User = require('../models/User');
var oidc = require('../models/OIDC');
var async = require('async');

module.exports = {
  // identityTypesGet:       Util.middleware([Util.check_access_token, identityTypesGet]),
  identityTypesGet:       identityTypesGet,
  identityTypesPost :     Util.middleware([Util.check_access_token, identityTypesPost]),
  identityTypesIdGet :    Util.middleware([Util.check_access_token, identityTypesIdGet]),
  identityTypesIdDelete : Util.middleware([Util.check_access_token, identityTypesIdDelete])
};

var notFound = {
  code : 404,
  message : "Tipo de identificador no encontrado"
};



function identityTypesGet(req, res) {
  IdentityType
  .find( {erased:false} )
  .exec(function (err, identityTypes) {
    if (err){
      req.error(err);
      res.status(500).json(err);
    } else {
      /*identityTypes.forEach(
        function (identityType) {
          identityType.__v = undefined;
          identityType.erased = undefined;
        }
      );*/
      res.status(200).json(identityTypes.map(function(it) {
        delete it.__verison;
        delete it.erased;
        return it;
      }));
    }
  });
}


function identityTypesPost(req, res) {
  var newIdentityType = new IdentityType();
  newIdentityType.code = req.body.code;
  newIdentityType.name = req.body.name;
  newIdentityType.countryCode = req.body.countryCode;
  newIdentityType.comment = req.body.comment;
  newIdentityType.save(function (err, identityType) {
    if(err){
      req.error(err);
      res.status(500).json(err);
    } else {
      var response = {code:201, id:identityType._id};
      res.location('/identitytypes/' + identityType.id);
      res.status(201).json(response);
    }
  }); //save
}


function identityTypesIdGet(req, res) {
  if (req.swagger.params.id.value.match(/^[0-9a-fA-F]{24}$/)) {
    IdentityType
      .findById( req.swagger.params.id.value )
      .exec(function (err, identityType) {
        if (err){
          req.error(err);
          res.status(500).json(err);
        } else {
          if (!identityType) {
            res.status(404).json(notFound);
          } else {
            res.status(200).json(identityType);
          }
        }
      });
  } else {
    req.notice(err);
    res.status(404).json(notFound);
  }
}


function identityTypesIdDelete(req, res) {
  if (req.swagger.params.id.value.match(/^[0-9a-fA-F]{24}$/)) {
   IdentityType.findById(req.swagger.params.id.value, function(err, identityType) {
     if (err){
       req.error(err);
       res.status(500).json(err);
     }
     else {
       if(!identityType){
         req.notice(notFound);
         res.status(404).json(notFound);
       }
       else{
         identityType.erased = true;
         // save the identityType
         identityType.save(function(err) {
           if (err){
            req.error(err);
            res.status(404).json(notFound);
          } else {
            var response = {code:204, id:identityType._id};
            res.status(204).json(response);
           }
         });
       }
     }
   });
  } else {
    req.notice(notFound);
    res.status(404).json(notFound);
  }
}
