package com.elab.my.github;

import org.junit.Test;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

/**
 * 描述: 用于递归遍历目录并打印MD日志到控制台
 *
 * @author liukx
 * @date 2021/8/31 14:01
 */
public class FileCase {
    Set<String> fileList = new HashSet<>();

    private String fileDir = "D:\\github\\MyHome\\";

    @Test
    public void testFile() {
        String filePath = fileDir + "文章";
        File[] files = new File(filePath).listFiles();
        deepFile(files, 0);
    }

    private void deepFile(File[] files, int index) {
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            if (file.isDirectory()) {
                if (fileList.add(file.getName()) && !isEndWith(file.getName(), "assets")) {
                    System.out.println("##" + appendIndex("#", index) + " " + file.getName());
                }
                deepFile(file.listFiles(), index + 1);
            } else {
                if (!isEndWith(file.getName(), "png", "gif", "jpg")) {
                    System.out.println(appendIndex("-", 1) + "\t" + "[" + file.getName() + "](" + mdPath(file.getPath(),
                        "D:\\github\\MyHome", ".") + ")");
                }
            }
        }
    }

    public String mdPath(String path, String replaceStr, String replace) {
        return path.replace(replaceStr, replace);
    }

    public String appendIndex(String str, int index) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < index; i++) {
            stringBuilder.append(str);
        }
        return stringBuilder.toString();
    }

    private boolean isEndWith(String fileName, String... fileEndsWith) {
        for (int i = 0; i < fileEndsWith.length; i++) {
            String s = fileEndsWith[i];
            if (fileName.endsWith(s)) {
                return true;
            }
        }
        return false;
    }
}
