package com.ffii.fhsmsc.modules.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ffii.core.utils.PasswordUtils.IPasswordRule;
import com.ffii.fhsmsc.modules.settings.service.SettingsService;


public class PasswordRule implements IPasswordRule {
  private Integer min;

  private Boolean numberAndAlphabetic;
  private Boolean specialCharacter;
  private Boolean notContainUsername;
  private Boolean notContainThreeConsecutiveCharacters;

public PasswordRule(SettingsService settingsService) {
    if (settingsService == null){
      throw new IllegalArgumentException("settingsService");
    }

    this.min = settingsService.getInt(SettingNames.SYS_PASSWORD_RULE_MIN);
    this.numberAndAlphabetic = settingsService.getBoolean(SettingNames.SYS_PASSWORD_RULE_NUMBER_AND_ALPHABETIC);
    this.specialCharacter = settingsService.getBoolean(SettingNames.SYS_PASSWORD_RULE_SPECIAL_CHAR);
    this.notContainUsername = settingsService.getBoolean(SettingNames.SYS_PASSWORD_RULE_NOT_CONTAIN_USERNAME);
    this.notContainThreeConsecutiveCharacters = settingsService.getBoolean(SettingNames.SYS_PASSWORD_RULE_NOT_CONTAIN_THREE_CONSECUTIVE_CHAR);
  }

  public Integer getMin() {
    return this.min;
  }

  public void setMin(Integer min) {
    this.min = min;
  }

  public Boolean isNumberAndAlphabetic() {
    return this.numberAndAlphabetic;
  }

  public Boolean getNumberAndAlphabetic() {
    return this.numberAndAlphabetic;
  }

  public void setNumberAndAlphabetic(Boolean numberAndAlphabetic) {
    this.numberAndAlphabetic = numberAndAlphabetic;
  }

  public Boolean isSpecialCharacter() {
    return this.specialCharacter;
  }

  public Boolean getSpecialCharacter() {
    return this.specialCharacter;
  }

  public void setSpecialCharacter(Boolean specialCharacter) {
    this.specialCharacter = specialCharacter;
  }

  public Boolean isNotContainUsername() {
    return this.notContainUsername;
  }

  public Boolean getNotContainUsername() {
    return this.notContainUsername;
  }

  public void setNotContainUsername(Boolean notContainUsername) {
    this.notContainUsername = notContainUsername;
  }

  public Boolean isNotContainThreeConsecutiveCharacters() {
    return this.notContainThreeConsecutiveCharacters;
  }

  public Boolean getNotContainThreeConsecutiveCharacters() {
    return this.notContainThreeConsecutiveCharacters;
  }

  public void setNotContainThreeConsecutiveCharacters(Boolean notContainThreeConsecutiveCharacters) {
    this.notContainThreeConsecutiveCharacters = notContainThreeConsecutiveCharacters;
  }

  @JsonIgnore
  public String getWrongMsg() {
    StringBuilder msg = new StringBuilder("Please Following Password Rule:\n");
    msg.append("- Minimum " + getMin() + " Characters.\n");
    if (needNumberAndAlphabetic())
      msg.append("- Need numbers and alphabetic characters.\n");
    if (needSpecialCharacter())
      msg.append("- Need at least one special characters.\n");
    if (needNotContainUsername())
      msg.append("- Cannot contain username.\n");
    if (needNotContainThreeConsecutiveCharacters())
      msg.append("- Cannot contain three consecutive characters.\n");
    return msg.toString();
  }

  @Override
  public boolean needNumberAndAlphabetic() {
    return numberAndAlphabetic;
  }

  @Override
  public boolean needSpecialCharacter() {
    return specialCharacter;
  }

  @Override
  public boolean needNotContainUsername() {
    return notContainUsername;
  }

  @Override
  public boolean needNotContainThreeConsecutiveCharacters() {
    return notContainThreeConsecutiveCharacters;
  }

}