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

import com.zhangmeng.online.exam.ui.module.QuestionOption;
import com.zhangmeng.online.exam.ui.service.OptionService;
import com.zhangmeng.online.exam.ui.utils.AlertUtils;
import com.zhangmeng.online.exam.ui.utils.FxUtils;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.stage.Stage;

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

import static com.zhangmeng.online.exam.ui.utils.FxUtils.Question_DynamicTableComponent;

/**
 * @author zm
 * @date 2025/4/11 12:11
 * @version: 1.0
 */
public class OptionEditController {


    @FXML
    public RadioButton trueRadioButton;

    @FXML
    public RadioButton falseRadioButton;

    @FXML
    public TextField optionNameField;

    @FXML
    public TextField optionContentField;

    @FXML
    public TextArea explanationTextArea;

    private QuestionOption questionOption;

    public void setQuestionOption(QuestionOption questionOption) {
        this.questionOption = questionOption;
        this.optionNameField.setText(questionOption.getName());
        this.optionContentField.setText(questionOption.getOptionContent());
        this.explanationTextArea.setText(questionOption.getExplanation());
        if (questionOption.isAnswer()) {
            this.trueRadioButton.setSelected(true);
            this.falseRadioButton.setSelected(false);
        } else {
            this.trueRadioButton.setSelected(false);
            this.falseRadioButton.setSelected(true);
        }
    }

    private final SimpleBooleanProperty isFlag = new SimpleBooleanProperty(true);

    @FXML
    public void initialize() {
        ToggleGroup tg = new ToggleGroup();
        trueRadioButton.setToggleGroup(tg);
        trueRadioButton.setUserData(true);
        falseRadioButton.setToggleGroup(tg);
        falseRadioButton.setUserData(false);
        tg.selectToggle(trueRadioButton);//默认选项

        tg.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
            @Override
            public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {
                RadioButton radioButton = (RadioButton) newValue;
                radioButton.getUserData();
                isFlag.set((boolean) radioButton.getUserData());
            }
        });
    }

    @FXML
    public void handleSave(ActionEvent actionEvent) {
        if (optionNameField.getText().trim().isEmpty()) {
            AlertUtils.alert_warning("选项名称不能为空");
            return;
        }
        if (optionContentField.getText().trim().isEmpty()) {
            AlertUtils.alert_warning("选项内容不能为空");
            return;
        }
        Map<String, Object> option = new HashMap<>();
        option.put("id", questionOption == null ? "" : questionOption.getId());
        option.put("name", optionNameField.getText().trim());
        option.put("content", optionContentField.getText().trim());
        option.put("isFlag", isFlag.get());
        option.put("explanation", explanationTextArea.getText() == null ? "" : explanationTextArea.getText().trim());
        boolean result = OptionService.saveOption(option);
        if (result) {
            AlertUtils.alert_msg("保存成功");
            closeDialog();
            FxUtils.FX_BEANS.get(Question_DynamicTableComponent).flushData();
        }else {
            AlertUtils.alert_warning("保存失败");
        }
    }

    @FXML
    public void handleCancel(ActionEvent actionEvent) {
        closeDialog();
    }

    private void closeDialog() {
        Stage stage = (Stage) trueRadioButton.getScene().getWindow();
        stage.close();
    }

    public QuestionOption getQuestionOption() {
        return questionOption;
    }


}
