'use strict';

angular
  .module('sentinelConsoleApp')
  .factory('A3WPasswordService', [
    '$http',
    'Constants',
    function () {
      const eyeOpenClass =  'glyphicon glyphicon-eye-open';
      const eyeCloseClass = 'glyphicon glyphicon-eye-close';
      return {
        init() {
          return {
            eyeOpenClass: eyeOpenClass,
            eyeCloseClass: eyeCloseClass,
            iconClass: eyeOpenClass,
            inputType: 'text',
            showEyeIcon: false
          };
        },
        showHidePassword(passwordInfo) {
          passwordInfo.inputType = passwordInfo.inputType === 'password' ? 'text' : 'password';
          passwordInfo.iconClass = passwordInfo.inputType === 'password' ? passwordInfo.eyeOpenClass : passwordInfo.eyeCloseClass;
        },
        update(passwordInfo, newText) {
          if (newText !== undefined && newText.length > 0) {
            passwordInfo.showEyeIcon = true;
          } else {
            passwordInfo.showEyeIcon = false;
            // Esta parte es para el caso en el que se borre el contenido del password estando
            // en modo VISIBLE (type=text). Se reinicia el estado del campo, o sea, atributo type
            // igual a password e icono de ojo abierto.
            passwordInfo.inputType = 'password';
            passwordInfo.iconClass = passwordInfo.eyeOpenClass;
          }
        }
      };
    }
  ]);
