package cn.reghao.tnb.content.app.disk.service;

import cn.reghao.oss.sdk.model.constant.ObjectType;
import cn.reghao.tnb.content.app.disk.model.vo.FileTree;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

@RunWith(MockitoJUnitRunner.class)
public class DiskUnitTest {
    void walkDir(Path path) throws IOException {
        Files.walkFileTree(path, new FileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                File dir1 = dir.toFile();
                process(dir1);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                File file1 = file.toFile();
                process(file1);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }
        });
    }

    List<FileTree> fileTreeList = new ArrayList<>();
    private void process(File file) {
        String basePath = dir.getAbsolutePath();
        String absolutePath = file.getAbsolutePath();
        String path = absolutePath.replace(basePath, "");
        String pid = file.getParent().replace(basePath, "");
        if (path.equals("")) {
            pid = "0";
        } else if (pid.equals("")) {
            pid = "/";
        }

        int fileType = ObjectType.Image.getCode();
        if (file.isDirectory()) {
            fileType = ObjectType.Dir.getCode();
        }

        String fileId = absolutePath.replace(basePath, "");
        FileTree fileTree = new FileTree(pid, fileId, fileType);
        fileTreeList.add(fileTree);
    }

    String baseDir = "/home/reghao/Downloads/";
    File dir = new File(baseDir);

    Map<String, List<FileTree>> getTreeMap() throws IOException {
        Path path = Paths.get(dir.getAbsolutePath());
        walkDir(path);

        Map<String, List<FileTree>> pidGroup = fileTreeList.stream()
                .collect(Collectors.groupingBy(FileTree::getPid));
        return new TreeMap<>(pidGroup);

    }

    @Test
    public void treeTest() throws Exception {
        /*Map<String, List<FileTree>> pidTreeMap = getTreeMap();
        pidTreeMap.forEach((key, list) -> {
            String pid = key;
            for (FileTree fileTree : list) {
                String fileId = fileTree.getFileId();
            }
        });
        System.out.println("main-thread goto sleep...");*/
    }
}
