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

import javafx.geometry.Insets;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;

import java.util.Arrays;
import java.util.List;

/**
 * 填空题组件
 *
 * @author zm
 * @date 2025/3/4 15:12
 * @version: 1.0
 */
public class FillBlankComponent extends VBox {

    private Button indexButton;

    // 题目数据模型
    private static class Question {
        String content;  // 如 "Java中多线程可通过实现__1__接口或继承__2__类"
        List<String> answers; // 如 ["Runnable", "Thread"]
    }

    // 示例题目
//    private final Question currentQuestion = new Question();
//
//    {
//        currentQuestion.content = "Java中多线程可通过实现__1__接口或继承__2__类";
//        currentQuestion.answers = Arrays.asList("Runnable", "Thread");
//    }


    public FillBlankComponent(String content, List<String> answers) {

         Question currentQuestion = new Question();
        currentQuestion.content = "Java中多线程可通过实现__1__接口或继承__2__类";
        currentQuestion.answers = Arrays.asList("Runnable", "Thread");

        // 题目文本处理（将答案位置替换为输入框）
        String[] splitContent = currentQuestion.content.split("__\\d+__");
        HBox questionBox = new HBox(10);
        for (int i = 0; i < splitContent.length; i++) {

            Label label = new Label(splitContent[i]);
            label.setFont(Font.font(16));

            questionBox.getChildren().add(label);
            if (i < splitContent.length - 1) {
                TextField inputField = new TextField();
                inputField.setStyle("-fx-border-width: 0px;");
                inputField.setPromptText("填空" + (i + 1));
                questionBox.getChildren().add(inputField);
            }
        }

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

        // 提交事件处理
        submitBtn.setOnAction(e -> {
            List<TextField> inputFields = questionBox.getChildren()
                    .filtered(node -> node instanceof TextField)
                    .stream().map(node -> (TextField) node).toList();

            StringBuilder feedback = new StringBuilder();
            int correctCount = 0;
            for (int i = 0; i < inputFields.size(); i++) {
                String userAnswer = inputFields.get(i).getText().trim();
                if (userAnswer.equalsIgnoreCase(currentQuestion.answers.get(i))) {
                    correctCount++;
                } else {
                    feedback.append("❌ 填空").append(i + 1)
                            .append("正确答案：").append(currentQuestion.answers.get(i)).append("\n");
                }
            }

            if (correctCount == currentQuestion.answers.size()) {
                resultLabel.setText("✅ 全部正确！");
                resultLabel.setStyle("-fx-text-fill: #2ecc71;");
            } else {
                resultLabel.setText(feedback.toString());
                resultLabel.setStyle("-fx-text-fill: #e74c3c;");
            }
        });

        // 主布局
        this.setSpacing(20);
        this.setPadding(new Insets(20));
        this.getChildren().addAll(questionBox, submitBtn, resultLabel);
    }

    public Button getIndexButton() {
        return indexButton;
    }

    public void setIndexButton(Button indexButton) {
        this.indexButton = indexButton;
    }
}
