package service.generator;

import java.util.Random;
public class JuniorQuestionGenerator implements QuestionGenerator {
    private final Random rand = new Random();

    @Override
    public String generateQuestion() {
        int numOperands = rand.nextInt(4) + 2; // 2-5个操作数
        String[] operands = new String[numOperands];

        // 先填充普通数字
        for (int i = 0; i < numOperands; i++) {
            operands[i] = String.valueOf(num(15));
        }

        // 随机替换一个为特殊运算
        int specialIndex = rand.nextInt(numOperands);
        operands[specialIndex] = specialOp();

        // 根据操作数数量选择模板
        switch (numOperands) {
            case 2:
                return String.format("%s %c %s", operands[0], op(), operands[1]);
            case 3:
                return String.format("(%s %c %s) %c %s", operands[0], opLow(), operands[1], opHigh(), operands[2]);
            case 4:
                return String.format("(%s %c %s) %c (%s %c %s)", operands[0], opHigh(), operands[1], opLow(), operands[2], opLow(), operands[3]);
            case 5:
            default:
                return String.format("((%s %c %s) %c %s) %c (%s %c %s)", operands[0], opLow(), operands[1], opHigh(), operands[2], opLow(), operands[3], opLow(), operands[4]);
        }
    }

    // --- 辅助方法 ---
    private int num(int bound) { return rand.nextInt(bound) + 1; }
    private char op() { return "+-×".charAt(rand.nextInt(3)); }
    private char opLow() { return "+-".charAt(rand.nextInt(2)); }
    private char opHigh() { return '×'; }

    private String specialOp() {
        if (rand.nextBoolean()) {
            return (rand.nextInt(9) + 2) + "^2"; // 2^2 to 10^2
        } else {
            int base = rand.nextInt(10) + 1;
            return "√(" + (base * base) + ")";
        }
    }
}