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

import com.zhangmeng.online.exam.ui.api.ApiUtils;
import javafx.beans.property.SimpleStringProperty;
import javafx.geometry.Insets;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

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

/**
 * .radio-button {
 * -fx-font-size: 14px;
 * -fx-text-fill: #333;
 * }
 * .correct { -fx-text-fill: green; }
 * .wrong { -fx-text-fill: red; }
 * <p>
 * 单选题组件
 *
 * @author zm
 * @date 2025/3/4 14:13
 * @version: 1.0
 */
public class SingleChoiceComponent extends VBox {

    private String TYPE = "single_choice";

    private Button indexButton;

    private int index;//该组件索引

    private Map<String,Object> context = new HashMap<>();

    public Map<String, Object> getContext() {
        return context;
    }

    public void setContext(Map<String, Object> context) {
        this.context = context;
    }

    private String correctAnswer = null; // 预设正确答案

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

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

    private HBox hBox = new HBox();

    private Label questionLabel;

    public void setBackground() {
        this.indexButton.setStyle("-fx-background-color: #4698ed;");
    }

    public SingleChoiceComponent(String question, List<Map<String, Object>> options,int index) {
        this.index = index;
        this.setStyle("-fx-background-color: white; -fx-border-color: #eadcdc;" +
                "-fx-border-radius: 10px; -fx-border-width: 1px; -fx-border-style: solid;-fx-background-radius: 10px;");

        // 创建题目组件
         questionLabel = new Label(question);

         hBox.getChildren().add(questionLabel);

        this.getChildren().add(hBox);
        ToggleGroup answerGroup = new ToggleGroup(); // 单选按钮组‌:ml-citation{ref="1,2" data="citationList"}
        // 创建选项按钮
        for (Map<String, Object> option : options) {
            RadioButton optionBtn = createRadioButton(option, answerGroup);
            this.getChildren().add(optionBtn);
        }

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

        // 设置提交事件
//        submitBtn.setOnAction(e -> {
//            RadioButton selected = (RadioButton) answerGroup.getSelectedToggle();
//            if (number_of_responses == 0){
//                if (selected != null) {
//                    String answer = selected.getText().substring(0,1);
//                    resultLabel.setText(answer.equals(correctAnswer) ? "✅ 回答正确" : "❌ 正确答案：" + correctAnswer);
//
//                    if (answer.equals(correctAnswer)){
//                        resultLabel.setStyle("-fx-text-fill: #2ecc71;");
//                    }else{
//                        resultLabel.setStyle("-fx-text-fill: #e74c3c;");
//                    }
//                    number_of_responses++;
//                } else {
//                    resultLabel.setText("⚠ 请先选择答案");
//                }
//            }else {
//                resultLabel.setText("⚠ 请勿重复提交");
//            }
//        });

//        answerGroup.selectedToggleProperty().addListener((ov, old_value, new_value) -> {
//            RadioButton selected = (RadioButton) new_value;
//            if (number_of_responses == 0){
//                if (selected != null) {
//                    String answer = selected.getText().substring(0,1);
//                    resultLabel.setText(answer.equals(correctAnswer) ? "✅ 回答正确" : "❌ 正确答案：" + correctAnswer);
//
//                    if (answer.equals(correctAnswer)){
//                        resultLabel.setStyle("-fx-text-fill: #2ecc71;");
//                    }else{
//                        resultLabel.setStyle("-fx-text-fill: #e74c3c;");
//                    }
//                    number_of_responses++;
//                } else {
//                    resultLabel.setText("⚠ 请先选择答案");
//                }
//            }
//        });

        answerGroup.selectedToggleProperty().addListener((ov, old_value, new_value) -> {
            RadioButton selected = (RadioButton) new_value;
            if (selected != null) {

                Map<String, Object> option  = (Map<String, Object>) selected.getUserData();
                number_of_responses++;
                context.put("option_id", option.get("id"));
                result_answer.set(option.get("id").toString());
                //提交数据
                ApiUtils.submitAnswer(context);

                //改变颜色
                setBackground();


                resultLabel.setText("你的选择："+ option.get("option"));
                resultLabel.setStyle("-fx-text-fill: #2ecc71;");

            } else {
                resultLabel.setText("⚠ 请先选择答案");
            }
        });

        // 布局容器
        this.setSpacing(10);
        this.setPadding(new Insets(15));
        this.hBox.getChildren().add(1,resultLabel);

    }

    // 封装单选按钮创建方法‌:ml-citation{ref="2,3" data="citationList"}
    private RadioButton createRadioButton(Map<String, Object> option, ToggleGroup group) {
        RadioButton rb = new RadioButton(option.get("text").toString());
        rb.setToggleGroup(group);
        rb.setPadding(new Insets(5));
        rb.setUserData(option);
        return rb;
    }

    public String getCorrectAnswer() {
        return correctAnswer;
    }

    public void setCorrectAnswer(String correctAnswer) {
        this.correctAnswer = correctAnswer;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }


    public String getTYPE() {
        return TYPE;
    }

    public void setTYPE(String TYPE) {
        this.TYPE = TYPE;
    }

    public Button getIndexButton() {
        return indexButton;
    }

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