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.MenuData;
import com.zhangmeng.online.exam.ui.module.User;
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.scene.control.Label;
import javafx.scene.control.TreeItem;
import com.zhangmeng.online.exam.ui.module.Role;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zhangmeng.online.exam.ui.utils.FxUtils.User_DynamicTableComponent;

/**
 * 模块
 * @author zm
 * @date 2025/3/31 15:29
 * @version: 1.0
 */
public class UserService {

    private static User currentUser;

    public static User getCurrentUser() {

        if (currentUser == null){
            return new User();
        }

        return currentUser;
    }

    public static void setCurrentUser(User user) {
        currentUser = user;
    }

    public static void user_type() {
        Map<String, Object> params = new HashMap<>();
        params.put("token", currentUser.getToken());
        String response = HttpUtils.GET(ApiUtils.API_URL + "/user/getUserInfo", params);
        JSONObject jsonObject = JSONObject.parseObject(response);
        if (jsonObject.getIntValue("code") == 200) {
            Map<String, Object> data = (Map<String, Object>) jsonObject.get("data");
            String type = data.get("type").toString();
            currentUser.setRoleType(type);
        }
    }

    public static void logout() {
        currentUser = null;
    }

    public static List<TreeItem<MenuData>> menu_tree_item() {

        String sideData = HttpUtils.GET(ApiUtils.API_URL + "/user/menu");
        JSONObject jsonObject = JSON.parseObject(sideData);
        JSONArray data = jsonObject.getJSONArray("data");

        List<TreeItem<MenuData>> menuList = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            JSONObject menu = data.getJSONObject(i);
            String name = menu.getString("name");
            String url = menu.getString("url");
            TreeItem<MenuData> management = new TreeItem<>(new MenuData(name, null, url));
            management.setGraphic(new Label(management.getValue().getTitle()));

            JSONArray children = (JSONArray) menu.get("children");
            children.forEach(child -> {
                JSONObject childMenu = (JSONObject) child;
                String childName = childMenu.getString("name");
                String childUrl = childMenu.getString("url");
                TreeItem<MenuData> menuDataTreeItem = new TreeItem<>(new MenuData(childName, null, childUrl));
                menuDataTreeItem.setGraphic(new Label(menuDataTreeItem.getValue().getTitle()));
                management.getChildren().add(menuDataTreeItem);
            });
            menuList.add(management);
        }

        return menuList;

    }


    public static void saveUser(User user) {

        Map<String, Object> map = new HashMap<>();
        map.put("id", user.getId());
        map.put("username", user.getUsername());
        map.put("name", user.getName());
        map.put("email", user.getEmail());
        map.put("status", user.getStatus());
        map.put("password", user.getPassword());
        map.put("salt", user.getSalt());
        map.put("phone", user.getPhone());
        map.put("avatarUrl", user.getAvatarUrl());
        map.put("token", user.getToken());
        map.put("role_ids", user.getRoles().stream().map(Role::getId).toArray());

        String result = HttpUtils.POST(ApiUtils.API_URL + "/user/save", map);
        JSONObject jsonObject = JSON.parseObject(result);
        if (jsonObject.getIntValue("code") == 200){
            if (user.getId() == 0){
                AlertUtils.alert_msg("保存成功！");
            }else {
                AlertUtils.alert_msg("更新成功！");
            }

            FxUtils.FX_BEANS.get(User_DynamicTableComponent).flushData();
        }else {
            AlertUtils.alert_warning("保存失败！");
        }
    }
}
