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.utils.AlertUtils;
import com.zhangmeng.online.exam.ui.utils.FxUtils;
import com.zhangmeng.online.exam.ui.utils.HttpUtils;

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;
import static com.zhangmeng.online.exam.ui.utils.FxUtils.Permission_DynamicTableComponent;

/**
 * @author zm
 * @date 2025/4/8 14:23
 * @version: 1.0
 */
public class PermissionService {


    public static List<Permission> getALLPermissions() {


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

        String userListData = HttpUtils.GET(API_URL + "/permission/list", params);
        JSONObject jsonObject = JSON.parseObject(userListData);
        JSONArray data = jsonObject.getJSONArray("data");
        Integer total = jsonObject.getInteger("total");
        List<Permission> roles = new ArrayList<>();
        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");

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

        return roles;


    }

    public static void savePermission(Map<String, Object> params, boolean isEdit) {
        String result = HttpUtils.POST(ApiUtils.API_URL + "/permission/save", params);
        JSONObject jsonObject = JSON.parseObject(result);
        if (jsonObject.getIntValue("code") == 200) {
            if (!isEdit) {
                AlertUtils.alert_msg("保存成功！");
                FxUtils.FX_BEANS.get(Permission_DynamicTableComponent).flushData();
            } else {
                AlertUtils.alert_msg("更新成功！");
                FxUtils.FX_BEANS.get(Permission_DynamicTableComponent).flushData();
            }
        } else {
            AlertUtils.alert_warning("保存失败！");
        }
    }

    public static Permission getPermissionById(String id) {

        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        String result = HttpUtils.GET(ApiUtils.API_URL + "/permission/getPermissionById", map);
        JSONObject jsonObject = JSON.parseObject(result);
        JSONObject data = jsonObject.getJSONObject("data");
        String name = data.getString("name");
        String desc = data.getString("desc");
        String url = data.getString("url");
        String sort = data.getString("sort");
        String parentId = data.getString("parentId");

        Permission permission = new Permission();
        permission.setId(Integer.parseInt(id));
        permission.setName(name);
        permission.setUrl(url);
        permission.setSort(Integer.parseInt(sort == null ? "0" : sort));
        permission.setParentId(Integer.parseInt(parentId));
        permission.setDescription(desc);

        return permission;
    }

    public static List<Permission> getRootPermissions() {

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

        String userListData = HttpUtils.GET(API_URL + "/permission/getRootPermissions", params);
        JSONObject jsonObject = JSON.parseObject(userListData);
        JSONArray data = jsonObject.getJSONArray("data");
        Integer total = jsonObject.getInteger("total");
        List<Permission> roles = new ArrayList<>();
        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);
            roles.add(permission);
        }

        return roles;
    }
}
