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.utils.HttpUtils;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.Parent;
import javafx.scene.control.MenuItem;
import javafx.stage.Stage;

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 {
    @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("type", new SimpleStringProperty(permission.getString("type")));
            questionMapList.add(userMap);
        }
        return new ApiUtils.DataView(Arrays.asList("序号", "题目", "题目类型", "所属学科", "分值"), questionMapList).setDataLoad(this).setPageNum(pageNum).setPageSize(pageSize).setTotal(total);
    }


    @Override
    public void setForm(Parent view) {

    }

    @Override
    public Parent getForm() {
        return null;
    }

    @Override
    public void deleteData(String id) {

    }

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

    @Override
    public void editData(String s, Stage stage) {

    }

    @Override
    public void updateData(String id, String name, String oldValue, String newValue) {

    }


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

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