package service.generator;

import java.util.Random;

public class SeniorQuestionGenerator implements QuestionGenerator {
    private final Random rand = new Random();
    private static final String[] TRIG_FUNCS = {"sin", "cos", "tan"};
    private static final int[] COMMON_ANGLES = {0, 30, 45, 60, 90,135,180};
    private static final int[] TAN_SAFE_ANGLES = {0, 30, 45, 60,135};

    @Override
    public String generateQuestion() {
        int numOperands = rand.nextInt(3) + 2; // 2-4个操作数以控制复杂度
        String[] operands = new String[numOperands];

        for (int i = 0; i < numOperands; i++) {
            operands[i] = String.valueOf(num(10));
        }

        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:
            default:
                return String.format("%s %c (%s %c (%s %c %s))", operands[0], opHigh(), operands[1], opLow(), operands[2], opLow(), operands[3]);
        }
    }

    // --- 辅助方法 ---
    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() {
        String func = TRIG_FUNCS[rand.nextInt(TRIG_FUNCS.length)];
        int angle;

        if (func.equals("tan")) {
            angle = TAN_SAFE_ANGLES[rand.nextInt(TAN_SAFE_ANGLES.length)];
        } else {
            angle = COMMON_ANGLES[rand.nextInt(COMMON_ANGLES.length)];
        }
        return String.format("%s(%d°)", func, angle);
    }
}