package thyyxxk.webserver.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class PasswordGenerator {
    public static final String[] SOURCE =  {"abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "0123456789", "!@#$%^&*()_+,.<>/?{}[]"};

    public static void main(String[] args) throws Exception {
        List<Integer> usedSourceIndexes = new ArrayList<>();
        int passwordLength = 16;
        String password;
        for (int i = 0; i < passwordLength; i++) {
            do {
                password = generatePassword(usedSourceIndexes, passwordLength);
            } while (usedSourceIndexes.size() < SOURCE.length);
            System.out.println(password);
        }
    }

    public static String generatePassword(List<Integer> usedSourceIndexes, int passwordLength) {
        usedSourceIndexes.clear();
        StringBuilder passwordBuilder = new StringBuilder();
        for (int i = 0; i < passwordLength; i++) {
            int sourceIndex = new Random().nextInt(SOURCE.length);
            if (!usedSourceIndexes.contains(sourceIndex)) {
                usedSourceIndexes.add(sourceIndex);
            }
            int sourceItemIndex = new Random().nextInt(SOURCE[sourceIndex].length());
            passwordBuilder.append(SOURCE[sourceIndex].split("")[sourceItemIndex]);
        }
        return passwordBuilder.toString();
    }
}