package com.ffii.fpsms.modules.common;

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


public class PasswordRule implements IPasswordRule {
  private Integer min;
  private Integer max;

  private Boolean number;
  private Boolean upperEng;
  private Boolean lowerEng;
  private Boolean specialChar;

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

    this.min = settingsService.getInt(SettingNames.SYS_PASSWORD_RULE_MIN);
    this.max = settingsService.getInt(SettingNames.SYS_PASSWORD_RULE_MAX);
    this.number = settingsService.getBoolean(SettingNames.SYS_PASSWORD_RULE_NUMBER);
    this.upperEng = settingsService.getBoolean(SettingNames.SYS_PASSWORD_RULE_UPPER_ENG);
    this.lowerEng = settingsService.getBoolean(SettingNames.SYS_PASSWORD_RULE_LOWER_ENG);
    this.specialChar = settingsService.getBoolean(SettingNames.SYS_PASSWORD_RULE_SPECIAL);
  }

  @Override
  public int getMin() {
    return min;
  }

  @Override
  public int getMax() {
    return max;
  }

  @Override
  public boolean needNumberChar() {
    return number;
  }

  @Override
  public boolean needUpperEngChar() {
    return upperEng;
  }

  @Override
  public boolean needLowerEngChar() {
    return lowerEng;
  }

  @Override
  public boolean needSpecialChar() {
    return specialChar;
  }

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

  public void setMax(Integer max) {
    this.max = max;
  }

  public Boolean getNumber() {
    return number;
  }

  public void setNumber(Boolean number) {
    this.number = number;
  }

  public Boolean getUpperEng() {
    return upperEng;
  }

  public void setUpperEng(Boolean upperEng) {
    this.upperEng = upperEng;
  }

  public Boolean getLowerEng() {
    return lowerEng;
  }

  public void setLowerEng(Boolean lowerEng) {
    this.lowerEng = lowerEng;
  }

  public Boolean getSpecialChar() {
    return specialChar;
  }

  public void setSpecialChar(Boolean specialChar) {
    this.specialChar = specialChar;
  }

  @JsonIgnore
  public String getWrongMsg() {
    StringBuilder msg = new StringBuilder("Please Following Password Rule.\n");
    msg.append("Minimum " + getMin() + " Characters\n");
    msg.append("Maximum " + getMax() + " Characters\n");
    if (needNumberChar())
      msg.append("Numbers\n");
    if (needLowerEngChar())
      msg.append("Lower-Case Letters\n");
    if (needUpperEngChar())
      msg.append("Capital Letters\n");
    if (needSpecialChar())
      msg.append("Symbols\n");
    return msg.toString();
  }

}