/*******************************************************************************
 * Copyright 2Fi Business Solutions Ltd.
 *
 * This code is copyrighted. Under no circumstances should any party, people,
 * or organization should redistribute any portions of this code in any form,
 * either verbatim or through electronic media, to any third parties, unless
 * under explicit written permission by 2Fi Business Solutions Ltd.
 ******************************************************************************/
package com.ffii.core;

import com.ffii.core.setting.service.SettingsService;
import com.ffii.core.utils.BooleanUtils;

public abstract class Settings {

	/*
	 * System-wide settings
	 */

	/** Define all available language names as comma separated string */
	public static final String SYS_AVAILABLE_LANGUAGES = "SYS.availableLanguages";

	/** Define all available locales as comma separated string */
	public static final String SYS_AVAILABLE_LOCALES = "SYS.availableLocales";

	/** Define the system default locale as string */
	public static final String SYS_DEFAULT_LOCALE = "SYS.defaultLocale";

	/** Define the system available currencies as comma separated string */
	public static final String SYS_CURRENCIES = "SYS.currencies";

	/** Define the system modules (authorities.module) */
	public static final String SYS_ROLE_MODULES = "SYS.modules";

	/*
	 * Mail settings
	 */

	/** Mail - SMTP host */
	public static final String MAIL_SMTP_HOST = "MAIL.smtp.host";

	/** Mail - SMTP port */
	public static final String MAIL_SMTP_PORT = "MAIL.smtp.port";

	/** Mail - SMTP username */
	public static final String MAIL_SMTP_USERNAME = "MAIL.smtp.username";

	/** Mail - SMTP password */
	public static final String MAIL_SMTP_PASSWORD = "MAIL.smtp.password";

	public static final String SYS_PASSWORD_RULE_MIN = "SYS.password.rule.length.min";
	public static final String SYS_PASSWORD_RULE_MAX = "SYS.password.rule.length.max";
	public static final String SYS_PASSWORD_RULE_NUMBER = "SYS.password.rule.number";
	public static final String SYS_PASSWORD_RULE_UPPER_ENG = "SYS.password.rule.upper.eng";
	public static final String SYS_PASSWORD_RULE_LOWER_ENG = "SYS.password.rule.lower.eng";
	public static final String SYS_PASSWORD_RULE_SPECIAL = "SYS.password.rule.special";

	public static final String JS_VERSION = "JS.version";

	public static class MailSMTP {
		private String host;
		private String port;
		private String username;
		private String password;

		public MailSMTP(SettingsService settingsService) {
			if (settingsService == null) throw new NullPointerException("SettingsService cannot be null");

			this.host = settingsService.getString(MAIL_SMTP_HOST);
			this.port = settingsService.getString(MAIL_SMTP_PORT);
			this.username = settingsService.getString(MAIL_SMTP_USERNAME);
			this.password = settingsService.getString(MAIL_SMTP_PASSWORD);
		}

		public String getHost() {
			return host;
		}

		public String getPort() {
			return port;
		}

		public String getUsername() {
			return username;
		}

		public String getPassword() {
			return password;
		}

	}

	public static class PasswordRule {
		private int min;
		private int max;

		private boolean number;
		private boolean upperEng;
		private boolean lowerEng;
		private boolean specialChar;

		public PasswordRule(SettingsService settingsService) {
			if (settingsService == null) throw new NullPointerException("SettingsService cannot be null");

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

		public int getMin() {
			return min;
		}

		public int getMax() {
			return max;
		}

		public boolean needNumberChar() {
			return number;
		}

		public boolean needUpperEngChar() {
			return upperEng;
		}

		public boolean needLowerEngChar() {
			return lowerEng;
		}

		public boolean needSpecialChar() {
			return specialChar;
		}

		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();
		}

	}
}