package com.cmvr.system.util;

import cn.hutool.core.util.ObjUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cmvr.system.domain.vo.CarIcon;
import com.cmvr.system.domain.vo.CarUi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;


/**
 * 路径查找相关工具类
 */
public class CarPathFinderUtil {

    /**
     * 从 JSONArray 构建 UI 图结构（Map<String, CarUi>）
     *
     * @param jsonData JSON 数组，格式包含界面和图标数据
     * @return Map<界面名称, CarUi对象>
     */
    public static Map<String, CarUi> buildUiMapFromJson(JSONArray jsonData) {
        Map<String, CarUi> uiMap = new HashMap<>();

        for (int i = 0; i < jsonData.size(); i++) {
            JSONObject node = jsonData.getJSONObject(i);
            CarUi ui = new CarUi(
                    node.getString("ui_name"),
                    node.getString("ui_image"),
                    new ArrayList<>()
            );

            JSONArray iconArray = node.getJSONArray("icons");
            if (iconArray != null) {
                for (int j = 0; j < iconArray.size(); j++) {
                    JSONObject iconNode = iconArray.getJSONObject(j);
                    CarIcon icon = new CarIcon(
                            iconNode.getString("icon_name"),
                            iconNode.getString("icon_image"),
                            iconNode.getString("icon_desc"),
                            iconNode.getString("icon_feature"),
                            iconNode.getString("jump")
                    );
                    ui.getIcons().add(icon);
                }
            }

            uiMap.put(ui.getUiImage(), ui);
        }

        return uiMap;
    }

    /**
     * 使用 BFS 算法查找从起始 UI 到目标功能的路径
     *
     * @param uiMap         UI 图结构
     * @param startUi       起始界面名称
     * @param targetFeature 目标功能名称（对应 iconName）
     * @return 路径（图标序列），找不到则返回 null
     */
    public static List<CarIcon> findPath(Map<String, CarUi> uiMap, String startUi, String targetFeature) {
        Set<String> visited = new HashSet<>();
        Queue<List<CarIcon>> queue = new LinkedList<>();
        queue.add(new ArrayList<>());

        while (!queue.isEmpty()) {
            List<CarIcon> currentPath = queue.poll();
            String currentUiName = currentPath.isEmpty()
                    ? startUi
                    : currentPath.get(currentPath.size() - 1).getJump();

            CarUi currentUi = uiMap.get(currentUiName);
            if (currentUi == null) {
                continue;
            }

            for (CarIcon icon : currentUi.getIcons()) {
                if (ObjUtil.isEmpty(icon.getIconImage())) {
                    continue;
                }
                if (icon.getIconName().equals(targetFeature)) {
                    currentPath.add(icon);
                    return currentPath;
                }

                String jump = icon.getJump();
                if (jump != null && !visited.contains(jump)) {
                    visited.add(jump);
                    List<CarIcon> newPath = new ArrayList<>(currentPath);
                    newPath.add(icon);
                    queue.add(newPath);
                }
            }
        }

        return null;
    }
}
