package com.oguerreiro.resilient;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

public class GeneratePasswordBCript {

  /**
   * Generates a BCrypt password encoded, to use in user direct insert in database
   * 
   * @param args
   */
  public static void main(String[] args) {
    List<String> passwords = new ArrayList<String>();
    passwords.add("cpss");
    passwords.add("cbernardo");
    passwords.add("chicau");
    passwords.add("paulap");
    passwords.add("susana.santos");
    passwords.add("manuel.salvador");
    passwords.add("vitoria.ferreira");
    passwords.add("pfluz");
    passwords.add("sribeiro");
    passwords.add("patricia.ferreira");
    passwords.add("jose.malcato");
    passwords.add("maria.ravara");

    String pwdAppend = "#";
    List<String> numbers = GeneratePasswordBCript.getRandom(passwords.size());

    for (String user : passwords) {
      String pwdRandom = numbers.get(0);
      numbers.remove(0);
      String pwd = user + pwdAppend + pwdRandom;
      String pwdCrypt = new BCryptPasswordEncoder().encode(pwd);
      System.out.println(user + "              :: " + pwd + " = " + pwdCrypt);
    }

  }

  private static List<String> getRandom(int size) {
    Set<String> numbers = new HashSet<>();

    while (numbers.size() < size) {
      int random = ThreadLocalRandom.current().nextInt(10000, 100000); // 10000 to 99999
      numbers.add(String.valueOf(random));
    }

    // Convert to a list to allow indexed access
    return new ArrayList<>(numbers);
  }
}
