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.api.ApiUtils;
import com.zhangmeng.online.exam.ui.module.MenuData;
import com.zhangmeng.online.exam.ui.module.User;
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TreeItem;
import javafx.stage.Stage;

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

/**
 * 模块
 * @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 createUser(User user) {

    }

    public static void updateUser(User user) {

    }
}
