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.utils.ApiUtils;
import com.zhangmeng.online.exam.ui.api.DataLoad;
import com.zhangmeng.online.exam.ui.api.form.RoleForm;
import com.zhangmeng.online.exam.ui.components.PermissionChooseComponent;
import com.zhangmeng.online.exam.ui.controller.PermissionChooseController;
import com.zhangmeng.online.exam.ui.controller.RoleEditController;
import com.zhangmeng.online.exam.ui.module.Permission;
import com.zhangmeng.online.exam.ui.module.Role;
import com.zhangmeng.online.exam.ui.module.RoleType;
import com.zhangmeng.online.exam.ui.service.RoleService;
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.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.util.*;

import static com.zhangmeng.online.exam.ui.utils.ApiUtils.API_URL;
import static com.zhangmeng.online.exam.ui.utils.FxUtils.Role_DynamicTableComponent;

/**
 * @author zm
 * @date 2025/3/14 16:19
 * @version: 1.0
 */
public class RoleDataLoad 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 + "/role/list",params);
        JSONObject jsonObject = JSON.parseObject(userListData);
        JSONArray data = jsonObject.getJSONArray("data");
        Integer total = jsonObject.getInteger("total");
        List<Map<String, SimpleStringProperty>> roleMapList = new ArrayList<>();
        for (Object datum : data) {
            JSONObject role = (JSONObject) datum;
            Map<String, SimpleStringProperty> roleMap = new HashMap<>();
            roleMap.put("序号", new SimpleStringProperty(role.getString("id")));
            roleMap.put("id", new SimpleStringProperty(role.getString("id")));
            roleMap.put("角色名称", new SimpleStringProperty(role.getString("name")));
            roleMap.put("角色名称_name", new SimpleStringProperty(role.getString("name")));
            roleMap.put("描述", new SimpleStringProperty(role.getString("desc")));
            roleMap.put("描述_desc", new SimpleStringProperty(role.getString("desc")));
            roleMap.put("角色类型", new SimpleStringProperty(role.getString("type_name")));
            roleMap.put("角色类型_type_name", new SimpleStringProperty(role.getString("type_name")));
            roleMapList.add(roleMap);
        }
        return new ApiUtils.DataView(Arrays.asList("序号", "角色名称", "描述", "角色类型"), roleMapList).setDataLoad(this).setPageNum(pageNum).setPageSize(pageSize).setTotal(total);
    }

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

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

    @Override
    public Map<String, Object> saveForm(Map<String, Object> map) {
        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 + "/role/delete", map);
        JSONObject jsonObject = JSON.parseObject(result);
        if (jsonObject.getIntValue("code") == 200) {
            AlertUtils.alert_msg("删除成功！");
            FxUtils.FX_BEANS.get(Role_DynamicTableComponent).flushData();
        } else {
            AlertUtils.alert_warning("删除失败！");
        }
    }

    @Override
    public void editData(String id, Stage stage) {
        RoleForm userForm = new RoleForm();
        RoleEditController controller = userForm.getController();

        Role role = RoleService.getRoleById(id);
        List<RoleType> roleTypes = RoleService.getRoleTypes();
        for (RoleType roleType : roleTypes) {
            if (roleType.getType().equals(role.getTypeName())) {
                controller.roleComboBox.setValue(roleType);
                break;
            }
        }

        controller.setRole(role);
        AlertUtils.alert("编辑用户信息", (Parent) userForm, stage);
    }

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

    }

    @Override
    public List<MenuItem> getContextMenu() {

        MenuItem group_volumes = new MenuItem("设置权限");
        group_volumes.setOnAction(event -> {
            Map<String, SimpleStringProperty> map = ApiUtils.selectedItem(group_volumes);
            String id = map.get("id").getValue();
            ContextMenu parentPopup = group_volumes.getParentPopup();
            BorderPane indexPage = (BorderPane) parentPopup.getStyleableParent();
            Stage stage = (Stage) indexPage.getScene().getWindow();

            Map<String, Object> context = new HashMap<>();
            context.put("id", id);

            PermissionChooseComponent paperViewComponent = new PermissionChooseComponent(context);

            PermissionChooseController permissionChooseController = paperViewComponent.getPermissionChooseController();
            permissionChooseController.permissionChooseVBox.prefWidthProperty().bind(paperViewComponent.widthProperty());
            permissionChooseController.permissionChooseVBox.prefHeightProperty().bind(paperViewComponent.heightProperty());
            permissionChooseController.permissionChooseVBox.setStyle("-fx-border-width: 0.5px; -fx-border-color: transparent;");

            Role role = RoleService.getRoleById(id);

            //查询权限
            List<Permission> permissions = RoleService.getPermissionsByRole(role);
            role.setPermissions(permissions);

            permissionChooseController.setRole(role);
            AlertUtils.alert("设置权限", paperViewComponent, 649, 800, stage, true);

        });

        return Arrays.asList(group_volumes);
    }

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