package com.bonus.gateway.config;


import com.google.code.kaptcha.text.TextProducer;
import java.util.Random;

public class MixedTextCreator implements TextProducer {
    private static final String NUMBERS = "23456789";
    private static final String LETTERS = "abcdefghijkmnopqrstuvwxyz";
    private final Random random = new Random();

    @Override
    public String getText() {
        // 确保至少包含2个数字和2个字母
        StringBuilder text = new StringBuilder(6);

        // 添加2个随机数字
        for (int i = 0; i < 2; i++) {
            text.append(NUMBERS.charAt(random.nextInt(NUMBERS.length())));
        }

        // 添加2个随机字母
        for (int i = 0; i < 2; i++) {
            text.append(LETTERS.charAt(random.nextInt(LETTERS.length())));
        }

        // 添加剩余2个随机字符（可以是数字或字母）
        String allChars = NUMBERS + LETTERS;
        for (int i = 0; i < 2; i++) {
            text.append(allChars.charAt(random.nextInt(allChars.length())));
        }

        // 打乱字符顺序
        char[] chars = text.toString().toCharArray();
        for (int i = chars.length - 1; i > 0; i--) {
            int index = random.nextInt(i + 1);
            char temp = chars[index];
            chars[index] = chars[i];
            chars[i] = temp;
        }

        return new String(chars);
    }
}