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

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.module.Permission;
import com.zhangmeng.online.exam.ui.module.Role;
import com.zhangmeng.online.exam.ui.module.RoleType;
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
import javafx.collections.ObservableList;

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

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

/**
 * @author zm
 * @date 2025/3/31 17:04
 * @version: 1.0
 */
public class RoleService {
    public static List<Role> getAllRoles() {

        Map<String, Object> params = new HashMap<>();
        params.put("pageNum", ApiUtils.PAGE_NUM);
        params.put("pageSize", ApiUtils.PAGE_SIZE);

        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<Role> roles = new ArrayList<>();
        for (Object datum : data) {
            JSONObject rolemap = (JSONObject) datum;

            Role role = new Role();
            role.setId(rolemap.getInteger("id"));
            role.setName(rolemap.getString("name"));
            role.setDescription(rolemap.getString("desc")); // 角色描述
            role.setStatus(rolemap.getString("status"));
            role.setPermissions(new ArrayList<>()); // 角色权限

            roles.add(role);
        }

        return roles;
    }

    public static Role getRoleById(String id) {

        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        String result = HttpUtils.GET(ApiUtils.API_URL + "/role/getRoleById", map);
        JSONObject jsonObject = JSON.parseObject(result);
        JSONObject data = jsonObject.getJSONObject("data");

        String name = data.getString("name");
        String desc  = data.getString("desc");
        String type_name = data.getString("type_name");

        Role role = new Role();
        role.setId(Integer.parseInt(id));
        role.setName(name);
        role.setDescription(desc);
        role.setTypeName(type_name);
        role.setPermissions(new ArrayList<>());
        return role;
    }

    public static List<RoleType> getRoleTypes() {

        String result = HttpUtils.GET(ApiUtils.API_URL + "/role/getRoleTypes");
        JSONObject jsonObject = JSON.parseObject(result);
        JSONArray roleTypes = jsonObject.getJSONArray("data");
        int index = 0;
        List<RoleType> roleTypeList = new ArrayList<>();
        for (Object obj : roleTypes) {
            JSONObject roleTypeMap = (JSONObject) obj;
            String type = roleTypeMap.getString("type_name");
            String desc = roleTypeMap.getString("type_desc");
//            int index = roleTypeMap.getInteger("index");
            RoleType roleType = new RoleType(type, desc, index);
            index++;
            roleTypeList.add(roleType);
        }
        return roleTypeList;
    }

    public static boolean saveRole(String id, String name, String desc, String type) {

        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", name);
        map.put("desc", desc);
        map.put("type", type);
        String result = HttpUtils.POST(ApiUtils.API_URL + "/role/saveRole", map);
        JSONObject jsonObject = JSON.parseObject(result);
        return jsonObject.getIntValue("code") == 200;
    }

    public static boolean updatePermission(int id, ObservableList<Permission> permissions) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        StringBuilder ids = new StringBuilder();
        for (Permission permission : permissions) {
            ids.append(permission.getId()).append(",");
        }
        map.put("permissionIds", ids.toString());
        String result = HttpUtils.POST(ApiUtils.API_URL + "/role/updatePermission", map);
        JSONObject jsonObject = JSON.parseObject(result);
        return jsonObject.getIntValue("code") == 200;
    }

    public static List<Permission> getPermissionsByRole(Role role) {

        Map<String, Object> map = new HashMap<>();
        map.put("roleId", role.getId());
        String result = HttpUtils.GET(ApiUtils.API_URL + "/role/getPermissionsByRole", map);
        JSONObject jsonObject = JSON.parseObject(result);
        List<Permission> permissionList = new ArrayList<>();

        JSONArray data = jsonObject.getJSONArray("data");
        Integer total = jsonObject.getInteger("total");

        for (Object datum : data) {
            JSONObject permissionmap = (JSONObject) datum;

            String id = permissionmap.getString("id");
            String name = permissionmap.getString("name");
            String desc = permissionmap.getString("desc");
            String url = permissionmap.getString("url");
            String parentId = permissionmap.getString("parentId");
            String sort = permissionmap.getString("sort");

            JSONArray children = permissionmap.getJSONArray("children");
            List<Permission> childrenList = new ArrayList<>();
            for (Object child : children) {
                JSONObject childmap = (JSONObject) child;
                Permission permission = new Permission();
                permission.setId(Integer.parseInt(childmap.getString("id")));
                permission.setName(childmap.getString("name"));
                permission.setUrl(childmap.getString("url"));
                String parentId1 = childmap.getString("parentId");
                if (sort != null) {
                    permission.setSort(Integer.parseInt(sort));
                }
                permission.setDescription(childmap.getString("desc"));
                if (parentId1 != null) {
                    permission.setParentId(Integer.parseInt(parentId1));
                }
                childrenList.add(permission);
            }

            Permission permission = new Permission();
            permission.setId(Integer.parseInt(id));
            permission.setName(name);
            permission.setUrl(url);
            if (sort != null) {
                permission.setSort(Integer.parseInt(sort));
            }
            if (parentId != null) {
                permission.setParentId(Integer.parseInt(parentId));
            }

            permission.setChildren(childrenList);
            permission.setDescription(desc);
            permissionList.add(permission);
        }

        return permissionList;
    }

}
