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

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.zhangmeng.online.exam.ui.api.ApiUtils;
import com.zhangmeng.online.exam.ui.module.QuestionType;
import com.zhangmeng.online.exam.ui.utils.AlertUtils;
import com.zhangmeng.online.exam.ui.utils.HttpUtils;

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

/**
 * @author zm
 * @date 2025/4/1 12:04
 * @version: 1.0
 */
public class QuestionService {


    public static List<QuestionType> getALLQuestionType() {
        Map<String, Object> params = new HashMap<>();
        params.put("token", UserService.getCurrentUser().getToken());
//        params.put("token", HttpUtils.Token);
        String response = HttpUtils.GET(ApiUtils.API_URL + "/question/getAllQuestionType", params);
        JSONObject jsonObject = JSONObject.parseObject(response);
        List<QuestionType> questionTypes = new ArrayList<>();
        if (jsonObject.getIntValue("code") == 200) {
            questionTypes = JSONArray.parseArray(jsonObject.getString("data"), QuestionType.class);
        }
        return questionTypes;
    }


    public static void saveQuestion(String type, Long subject_id, String question, String score, String explanation, Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("token", UserService.getCurrentUser().getToken());
        params.put("type", type);
        params.put("id", id);
        params.put("subject_id", subject_id);
        params.put("question", question);
        params.put("score", score);
        params.put("explanation", explanation);
        String response = HttpUtils.POST(ApiUtils.API_URL + "/question/saveQuestion", params);
        JSONObject jsonObject = JSONObject.parseObject(response);
        if (jsonObject.getIntValue("code") == 200) {
            AlertUtils.alert_msg("保存成功");
        } else {
            AlertUtils.alert_warning("保存失败");
        }
    }

}
