package com.zhangmeng.online.exam.ui.components;

import com.zhangmeng.online.exam.ui.utils.OptionsUtils;
import javafx.beans.property.SimpleStringProperty;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

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


/**
 * @author zm
 * @date 2025/3/4 14:34
 * @version: 1.0
 */
public class MultiChoiceComponent extends VBox {

    // 预设正确答案（选项标识）
    private final Set<String> correctAnswers = Set.of("A", "C");

    private List<CheckBox> checkBoxes = new ArrayList<>();

    private HBox hBox = new HBox();

    private int number_of_responses = 0;//作答次数

    private final SimpleStringProperty result_answer = new SimpleStringProperty(); // 最终答案


    public MultiChoiceComponent(String question, Set<String> options) {
        Set<String> selected = new HashSet<>();
        // 题目组件
        Label questionLabel = new Label(question);

        this.getChildren().add(questionLabel);

        VBox optionsBox = new VBox(5);
        // 创建选项复选框
        for (String option : options) {
            CheckBox cb = createCheckBox(option);
            checkBoxes.add(cb);
            optionsBox.getChildren().add(cb);
        }

        // 功能组件
        Button submitBtn = new Button("提交答案");
        Label resultLabel = new Label();

        // 提交事件处理
        submitBtn.setOnAction(e -> {
            selected.clear();
            int i = 0;
            for (CheckBox option : checkBoxes) {
                if (option.isSelected()){
                    selected.add(OptionsUtils.getOption(options.size(), i));
                }
                i++;
            }

            if (selected.isEmpty()) {
                resultLabel.setText("⚠ 请至少选择一个选项");
            } else if (selected.equals(correctAnswers)) {
                resultLabel.setText("✅ 全部正确！");
            } else {
                resultLabel.setText("❌ 正确答案：" + correctAnswers);
            }
        });

        // 布局容器
        this.setSpacing(10);
        this.setPadding(new Insets(15));
        this.getChildren().addAll( optionsBox, submitBtn, resultLabel);

    }

    // 创建带样式的复选框‌:ml-citation{ref="5,6" data="citationList"}
    private CheckBox createCheckBox(String text) {
        CheckBox cb = new CheckBox(text);
        cb.setStyle("-fx-font-size: 14px; -fx-text-fill: #444;");
        return cb;
    }

    public void setIndexButton(Button button) {


    }
}
