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

import javafx.beans.property.SimpleIntegerProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;

import java.util.HashMap;
import java.util.Map;

/**
 * 考试按钮组件
 *
 * @author zm
 * @date 2025/3/21 13:59
 * @version: 1.0
 */
public class ExamButtonComponent extends FlowPane {

    private ExamComponent examComponent;

    private SimpleIntegerProperty countButton = new SimpleIntegerProperty(0);

    private final Map<Button, Integer> buttonMap = new HashMap<>();


    public ExamButtonComponent(int count, ExamComponent examComponent) {

        this.getStylesheets().add(getClass().getResource("/css/button.css").toExternalForm());
        this.examComponent = examComponent;
        this.countButton.set(count);
        this.setHgap(10);
        this.setVgap(10);
        this.setPadding(new Insets(10));
        for (int i = 1; i <= count; i++) {
            Button button = new Button(String.valueOf(i));
            button.setPrefWidth(45);
            button.setOnAction(event -> {
                //滚动到指定题目
                int currentIndex = buttonMap.get(button);
                examComponent.scrollToIndex(currentIndex);
            });
            buttonMap.put(button, i);
            Node node = examComponent.getNode(i);

            if (node instanceof SingleChoiceComponent singleChoiceComponent) {
                singleChoiceComponent.setIndexButton(button);
            }
            if (node instanceof MultiChoiceComponent multiChoiceComponent) {
                multiChoiceComponent.setIndexButton(button);
            }
            if (node instanceof TrueFalseComponent judgementComponent) {
                judgementComponent.setIndexButton(button);
            }
            if (node instanceof FillBlankComponent fillBlankComponent) {
                fillBlankComponent.setIndexButton(button);
            }
            if (node instanceof ShortAnswerComponent shortAnswerComponent) {
                shortAnswerComponent.setIndexButton(button);
            }

            this.getChildren().add(button);
        }


    }


}
