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.form.SubjectForm;
import com.zhangmeng.online.exam.ui.controller.SubjectEditController;
import com.zhangmeng.online.exam.ui.module.Subject;
import com.zhangmeng.online.exam.ui.service.SubjectService;
import com.zhangmeng.online.exam.ui.utils.ApiUtils;
import com.zhangmeng.online.exam.ui.api.DataLoad;
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.scene.Parent;
import javafx.scene.control.MenuItem;
import javafx.stage.Stage;

import java.util.*;

/**
 * @author zm
 * @date 2025/3/17 16:39
 * @version: 1.0
 */
public class SubjectDataLoad 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(ApiUtils.API_URL + "/subject/list", params);
        JSONObject jsonObject = JSON.parseObject(userListData);
        JSONArray data = jsonObject.getJSONArray("data");
        int total = jsonObject.getIntValue("total");
        List<Map<String, SimpleStringProperty>> userMapList = new ArrayList<>();
        for (Object datum : data) {
            JSONObject user = (JSONObject) datum;
            Map<String, SimpleStringProperty> userMap = new HashMap<>();
            userMap.put("序号", new SimpleStringProperty(user.getString("id")));
            userMap.put("学科名称", new SimpleStringProperty(user.getString("name")));
            userMap.put("学科名称_name", new SimpleStringProperty(user.getString("name")));
            userMap.put("学科代码", new SimpleStringProperty(user.getString("code")));
            userMap.put("学科代码_code", new SimpleStringProperty(user.getString("code")));
            userMap.put("学科描述", new SimpleStringProperty(user.getString("description")));
            userMap.put("学科描述_description", new SimpleStringProperty(user.getString("description")));
            userMap.put("id", new SimpleStringProperty(user.getString("id")));
            userMapList.add(userMap);
        }

        ApiUtils.DataView dataView = new ApiUtils.DataView(Arrays.asList("序号", "学科名称", "学科代码", "学科描述"), userMapList).setDataLoad(this).setPageNum(pageNum).setPageSize(pageSize).setTotal(total);
        dataView.setDataLoad(this);
        return dataView;
    }

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

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

    @Override
    public Map<String, Object> saveForm(Map<String, Object> map) {
        String result = HttpUtils.POST(ApiUtils.API_URL + "/subject/save", map);
        JSONObject jsonObject = JSON.parseObject(result);
        if (jsonObject.getIntValue("code") == 200) {
            AlertUtils.alert_msg("保存成功！");
        } else {
            AlertUtils.alert_warning("保存失败！");
        }
        return null;
    }

    @Override
    public void deleteData(String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        String result = HttpUtils.POST(ApiUtils.API_URL + "/subject/delete", map);
        JSONObject jsonObject = JSON.parseObject(result);
        if (jsonObject.getIntValue("code") == 200) {
            AlertUtils.alert_msg("删除成功！");
        } else {
            AlertUtils.alert_warning("删除失败！");
        }
    }

    @Override
    public void editData(String id, Stage stage) {
        SubjectForm subjectForm = new SubjectForm();
        SubjectEditController controller = subjectForm.getController();
        Subject subject = SubjectService.getSubjectById(id);
        controller.setSubject(subject);

        FxUtils.alert("编辑学科信息", (Parent) subjectForm, 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 == null ? "" : oldValue);
        map.put("newValue", newValue);
        String result = HttpUtils.POST(ApiUtils.API_URL + "/subject/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;
    }
}
