package com.zhangmeng.online.exam.ui.api.model;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.zhangmeng.online.exam.ui.api.ApiUtils;
import com.zhangmeng.online.exam.ui.api.DataLoad;
import com.zhangmeng.online.exam.ui.api.form.QuestionForm;
import com.zhangmeng.online.exam.ui.api.form.UserForm;
import com.zhangmeng.online.exam.ui.controller.QuestionEditController;
import com.zhangmeng.online.exam.ui.module.QuestionType;
import com.zhangmeng.online.exam.ui.module.SubjectType;
import com.zhangmeng.online.exam.ui.service.QuestionService;
import com.zhangmeng.online.exam.ui.service.SubjectService;
import com.zhangmeng.online.exam.ui.utils.AlertUtils;
import com.zhangmeng.online.exam.ui.utils.FxUtils;
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
import javafx.beans.property.SimpleStringProperty;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.MenuItem;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.*;

import static com.zhangmeng.online.exam.ui.api.ApiUtils.API_URL;

/**
 * @author zm
 * @date 2025/3/14 16:20
 * @version: 1.0
 */
public class QuestionDataLoad implements DataLoad {

    private Parent form;

    @Override
    public ApiUtils.DataView loadData(Integer pageNum, Integer pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("pageNum", pageNum.toString());
        params.put("pageSize", pageSize.toString());
        String userListData = HttpUtils.GET(API_URL + "/question/list", params);
        JSONObject jsonObject = JSON.parseObject(userListData);
        JSONArray data = jsonObject.getJSONArray("data");
        Integer total = jsonObject.getInteger("total");

        List<Map<String, SimpleStringProperty>> questionMapList = new ArrayList<>();
        for (Object datum : data) {
            JSONObject permission = (JSONObject) datum;
            Map<String, SimpleStringProperty> userMap = new HashMap<>();
            userMap.put("序号", new SimpleStringProperty(permission.getString("id")));
            userMap.put("题目", new SimpleStringProperty(permission.getString("name")));
            userMap.put("题目类型", new SimpleStringProperty(permission.getString("type")));
            userMap.put("所属学科", new SimpleStringProperty(permission.getString("subject")));
            userMap.put("分值", new SimpleStringProperty(permission.getString("score")));
            userMap.put("id", new SimpleStringProperty(permission.getString("id")));
            userMap.put("分值_score", new SimpleStringProperty(permission.getString("score")));
            userMap.put("题目_name", new SimpleStringProperty(permission.getString("name")));
            questionMapList.add(userMap);
        }
        return new ApiUtils.DataView(Arrays.asList("序号", "题目", "题目类型", "所属学科", "分值"), questionMapList).setDataLoad(this).setPageNum(pageNum).setPageSize(pageSize).setTotal(total);
    }


    @Override
    public void setForm(Parent view) {
        this.form = view;
    }

    @Override
    public Parent getForm() {
        return this.form;
    }

    @Override
    public void deleteData(String id) {

    }

    @Override
    public Map<String, Object> saveForm(Map<String, Object> map) {
        return null;
    }

    @Override
    public void editData(String id, Stage stage) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        String result = HttpUtils.GET(ApiUtils.API_URL + "/question/getQuestionById", map);
        JSONObject jsonObject = JSON.parseObject(result);
        JSONObject data = jsonObject.getJSONObject("data");
        String question_id = data.getString("id");
        String name = data.getString("name");
        String type = data.getString("type");
        String subject = data.getString("subject");
        String score = data.getString("score");
        String explanation = data.getString("explanation");
        QuestionForm questionForm = new QuestionForm();
        QuestionEditController controller = questionForm.getController();
        SubjectType st = null;
        for (SubjectType subjectType : SubjectService.getALLSubjectType()){
            if(subjectType.getName().equals(subject)){
                st = subjectType;
                break;
            }
        }
        QuestionType qt = null;
        for (QuestionType questionType : QuestionService.getALLQuestionType()) {
            if (questionType.getDesc().equals(type)) {
                qt = questionType;
                break;
            }
        }

        controller.subjectComboBox.setValue(st);
        controller.typeComboBox.setValue(qt);
        Map<String, Object> questionMap = new HashMap<>();
        questionMap.put("id", question_id);
        questionMap.put("question", name);
        questionMap.put("type", type);
        questionMap.put("subject", subject);
        questionMap.put("score", score);
        questionMap.put("explanation", explanation);
        controller.setQuestion(questionMap);

        FxUtils.alert("编辑问题", questionForm, stage,true);
    }

    @Override
    public void updateData(String id, String name, String oldValue, String newValue) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("fieldName", name);
        map.put("oldValue", oldValue);
        map.put("newValue", newValue);
        String result = HttpUtils.POST(ApiUtils.API_URL + "/question/update", map);
        JSONObject jsonObject = JSON.parseObject(result);
        if (jsonObject.getIntValue("code") == 200) {
            AlertUtils.alert_msg("更新成功！");
        } else {
            AlertUtils.alert_warning("更新失败！");
        }
    }


    @Override
    public List<MenuItem> getContextMenu() {
        return null;
    }

    @Override
    public ApiUtils.DataView searchData(Map<String, Object> searchText, int pageNum, int pageSize) {
        return null;
    }
}
