package com.rjconsultores.ventaboletos.utilerias;

import java.util.Date;
import java.util.Random;

import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;




public class PasswordHelper {
	
	private static Logger log = LogManager.getLogger(PasswordHelper.class);
	
	private static PasswordHelper instance;
	private final int MM_MAX = 10;
	
	private String password = "";
	private Date passwordCreated;

	public static PasswordHelper getInstance() {
		if (instance == null)
			instance = new PasswordHelper();
		return instance;
	}
	
	public String generateRandomPassword() {
		int size = 8;
		String capLetter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		String numbers   = "01234567890123456789";
		String finalString = capLetter + numbers; 
		
		Random random = new Random();
		char[] charPassword = new char[size];
		for (int i = 0; i < size; i++) {
			charPassword[i] = finalString.charAt(random.nextInt(finalString.length())); 
		}
		password = String.valueOf(charPassword);
		passwordCreated =  DateUtil.somarMinutos(new Date(), MM_MAX);
		log.info("Gerada Senha de Instalação Valida até: " + passwordCreated.toString());
		return password;
	}
	
	public boolean isValidPassword(String myPassword) {
		return !StringUtils.isBlank(myPassword) &&
			   !StringUtils.isBlank(password) && 
			   password.equals(myPassword) &&
			   DateUtil.compareDate(new Date(), passwordCreated) < 0;
	}
}
