package com.youlai.boot.system.model.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.checkerframework.checker.units.qual.C;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;



public class DirectoryTree {
    private static int currentId = 1;
//    private static Map<File, DirectoryNode> buildMap = new HashMap<>();

    public static Map<String, DirectoryNode> directoryMap = new HashMap<>();


    private static DirectoryNode rootNode;
 
    public static void main(String[] args) {
        File rootDir = new File("D:\\temp\\front\\pac");
        rootNode = buildDirectoryTree(rootDir, -1);
        String jsonString = JSON.toJSONString(rootNode);
        DirectoryNode directoryNode = JSON.parseObject(jsonString, DirectoryNode.class);
        DirectoryNode targetNode = findNodeById(rootNode, 4);
        if (targetNode != null) {
            List<String> path = getPathToRoot(rootNode, directoryNode);
            System.out.println("Path to node with ID 4: " + path);
        } else {
            System.out.println("Node with ID 4 not found.");
        }
    }

    public static DirectoryNode getDirectoryNode(File rootDir){
        DirectoryNode directoryNode = buildDirectoryTree(rootDir, -1);
        currentId = 1;
        return directoryNode;
    }

    public static void traverseFolder(File folder, List<File> leafFolders) {
        if (folder.isDirectory()) {
            File[] subFiles = folder.listFiles();
            if (subFiles != null && subFiles.length > 0) {
                boolean hasSubFolder = false;
                for (File file : subFiles) {
                    if (file.isDirectory()) {
                        hasSubFolder = true;
                        traverseFolder(file, leafFolders);
                    }
                }
                // 如果没有子文件夹，则当前文件夹是叶子节点
                if (!hasSubFolder) {
                    leafFolders.add(folder);
                }
            } else {
                // 如果文件夹为空，则也是叶子节点
                leafFolders.add(folder);
            }
        }
    }

    public static List<File> getLeafFolders(File rootDir) {
        List<File> leafFolders = new ArrayList<>();
        traverseFolder(rootDir, leafFolders);
        return leafFolders;
    }

 
    private static DirectoryNode buildDirectoryTree(File dir, int parentId) {

        DirectoryNode currentNode = new DirectoryNode(dir.getName(), dir.getAbsolutePath(),currentId, parentId);

        File[] files = dir.listFiles();
        int pId = currentId;
        for (File file : files) {
            if (file.isDirectory()) {
                currentId++;
                currentNode.addChild(buildDirectoryTree(file, pId));
            }
        }
        return currentNode;
    }
 
    public static DirectoryNode findNodeById(DirectoryNode node, int id) {
        if (node.id == id) {
            return node;
        }
        for (DirectoryNode child : node.children) {
            DirectoryNode result = findNodeById(child, id);
            if (result != null) {
                return result;
            }
        }
        return null;
    }

    public static List<String> getPathToRoot(DirectoryNode rootNode, DirectoryNode targetNode) {
        List<String> path = new ArrayList<>();
        while (targetNode != null) {
            path.add(0, targetNode.getLabel()); // 在列表开头插入，以保持路径顺序
            targetNode = findNodeById(rootNode,targetNode.parentId);
        }
        return path;
    }
}