;(function($) {
  $.fn.strength = function(options) {
    var settings = $.extend({
      type: 'site' // app | site
    }, options);

    return this.each(function() {
      var
      $this = $(this),
      messages = {
        app: {
          message: [
            '',
            'Senha muito fraca',
            'Senha fraca',
            'Senha média',
            'Senha ótima',
          ],
          color: [
            '',
            'mdl-color-text--red',
            'mdl-color-text--orange',
            'mdl-color-text--blue',
            'mdl-color-text--green'
          ]
        },
        site: {
          message: [
            '* Senha',
            '* Senha: Muito fraca',
            '* Senha: Fraca',
            '* Senha: Média',
            '* Senha: Ótima'
          ],
          color: [
            '',
            'text-error',
            'text-warning',
            'text-info',
            'text-success'
          ]
        }
      };

      /**
       * checkPasswordStrength - Verifica a força da senha
       *
       * @param  {type} elementWritingValue Obrigatória Valor do campo da senha
       * @return {type}                                 Pontuação da senha
       */
      function checkPasswordStrength(elementWritingValue) {
        var
        character = elementWritingValue.length * 0.2,
        upperCase = (elementWritingValue.match(new RegExp('[A-Z]'))) ? 0.5 : 0,
        lowerCase = (elementWritingValue.match(new RegExp('[a-z]'))) ? 0.5 : 0,
        number = (elementWritingValue.match(new RegExp('[0-9]'))) ? 0.3 : 0,
        special = (elementWritingValue.match(new RegExp('([!,%,&,@,#,$,^,*,?,_,-,~,\.,\,])'))) ? 0.7 : 0;

        return character + upperCase + lowerCase + number + special;
      }

      /**
       * displayPasswordStrength - Mostra a força da senha
       *
       * @param  {type} elementWriting Obrigatória Campo da senha
       */
      function displayPasswordStrength(elementWriting) {
        var
        total = checkPasswordStrength(elementWriting.val()),
        elementMessage = (settings.type == 'app') ? $('<span>') : elementWriting.prev('label');

        if (settings.type == 'site') {
          elementMessage.removeClass('text-error text-warning text-info text-success text-dark');
        }

        if (total == 0) {
          elementMessage.text(messages[settings.type]['message'][0]);
        } else if (total < 2) {
          elementMessage.text(messages[settings.type]['message'][1]).addClass(messages[settings.type]['color'][1]);
        } else if (total < 3) {
          elementMessage.text(messages[settings.type]['message'][2]).addClass(messages[settings.type]['color'][2]);
        } else if(total < 4) {
          elementMessage.text(messages[settings.type]['message'][3]).addClass(messages[settings.type]['color'][3]);
        } else {
          elementMessage.text(messages[settings.type]['message'][4]).addClass(messages[settings.type]['color'][4]);
        }

        if (settings.type == 'app') {
          elementMessage.addClass('ftr-password-strong-message');
          elementWriting.siblings('.ftr-password-strong-message').remove();
          elementWriting.closest('div[class^=mdl], label[class^=mdl], div[class*=mdl], label[class*=mdl]').append(elementMessage);
        }
      }

      $this.on('keyup', function() {
        displayPasswordStrength($(this));
      });

    });
  }
})(jQuery);
