'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _fsExtra = require('fs-extra');

var _fsExtra2 = _interopRequireDefault(_fsExtra);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

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

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var route = function route(req, res) {
  var resHtml = '';
  var page;
  var template;
  var tmp;
  if (typeof req.body.token !== 'undefined' && req.body.token !== null && typeof req.body.password !== 'undefined' && req.body.password !== null && typeof req.body['repeat-password'] !== 'undefined' && req.body['repeat-password'] !== null) {
    if (req.body.password !== req.body['repeat-password']) {

      page = _path2.default.join(__dirname + '/../../../views/users/reset.html');
      if (_cli.coreUtils.file.exist(page)) {
        resHtml = _fsExtra2.default.readFileSync(page, 'utf8');
      }

      template = _cli.Handlebars.compile(resHtml, { noEscape: true });

      tmp = template({
        csrfToken: res.locals.csrfToken,
        config: JSON.stringify(_cli.config),
        express: {
          req: req,
          res: res
        },
        token: req.body.token,
        info: 'Emails are not the same'
      });

      return res.send(tmp);
    }
    _cli.User.utils.findByResetPasswordToken(req.body.token, function (err, userToReset) {
      var msg = '';
      if (err) {
        msg = 'Error';
      } else if (typeof userToReset === 'undefined' || userToReset === null) {
        msg = 'Invalid token';
      } else {
        var d = new Date().getTime();
        d = (d - userToReset.resetPasswordExpires) / 1000 / 60;
        if (d > 0) {
          msg = 'Token expired';
        }
      }
      if (msg !== '') {

        page = _path2.default.join(__dirname + '/../../../views/users/reset.html');
        if (_cli.coreUtils.file.exist(page)) {
          resHtml = _fsExtra2.default.readFileSync(page, 'utf8');
        }

        template = _cli.Handlebars.compile(resHtml, { noEscape: true });

        tmp = template({
          csrfToken: res.locals.csrfToken,
          config: JSON.stringify(_cli.config),
          express: {
            req: req,
            res: res
          },
          token: req.body.token,
          info: msg
        });

        return res.send(tmp);
      }

      userToReset.password = req.body.password;
      var resUpdatePassword = _cli.User.operations.updatePassword(userToReset, req.body.password);
      if (resUpdatePassword.success === 1) {
        var login = _cli.config.users.login;
        res.redirect(login);
      } else {
        page = _path2.default.join(__dirname + '/../../../views/users/reset.html');
        if (_cli.coreUtils.file.exist(page)) {
          resHtml = _fsExtra2.default.readFileSync(page, 'utf8');
        }

        template = _cli.Handlebars.compile(resHtml, { noEscape: true });

        tmp = template({
          csrfToken: res.locals.csrfToken,
          config: JSON.stringify(_cli.config),
          express: {
            req: req,
            res: res
          },
          token: req.body.token,
          info: resUpdatePassword.message
        });

        return res.send(tmp);
      }
    });
  } else if (typeof req.body.token !== 'undefined' && req.body.token !== null) {
    res.redirect('/abe/users/reset?token=' + req.body.token);
  } else {
    res.redirect('/abe/users/forgot');
  }
};

exports.default = route;