import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.sql.SQLOutput;
import java.util.*;
import java.util.stream.Collectors;

public class GitDiff {
    public static void main(String[] args) throws Exception {
        packFolder("/Users/mac/Desktop", "A8V5_8.2_橙阳科技U8C集成开发_20240428","d2cd64d3","9630006d");
    }

    private static File packFolder(String path, String name,String startV,String endV) throws Exception {
        String destPath = path + File.separator + name;
        String javaCode = "/src/main/java";
        String webCode = "/src/main/webapp";
        String resCode = "src/main/resources";
        String targetClass = "/target/classes";
        File file = new File(GitDiff.class.getResource("").getPath());
        String[] split = file.getPath().split("v5");
        File rootFile = new File(URLDecoder.decode(split[0], "utf-8"));
        File[] files = rootFile.listFiles();
        List<String> filePaths = new ArrayList<>();
        for (File v5 : files) {
            if ("cap_code".equals(v5.getName())) {
                File capCore = new File(v5.getAbsoluteFile() + File.separator + "core" + File.separator + "target" + File.separator + "classes");
                if (capCore.exists()) {
                    traverseFile(capCore, filePaths);
                }
            }
            if ("v5".equals(v5.getName())) {
                File[] v5Files = v5.listFiles();
                for (File v5File : v5Files) {
                    File v5Classes = new File(v5File.getAbsoluteFile() + File.separator + "target" + File.separator + "classes");
                    File v5Webapps = new File(v5File.getAbsoluteFile() + File.separator + "src" + File.separator + "main" + File.separator + "webapp");
                    if (v5Classes.exists()) {
                        traverseFile(v5Classes, filePaths);
                    }
                    if (v5Webapps.exists()) {
                        traverseFile(v5Webapps, filePaths);
                    }
                }
            }
        }

        System.out.println("\r\n产品编译后编译后文件列表");
        filePaths.forEach(System.out::println);

        System.out.println("\r\n异常源码列表");
        Process process = Runtime.getRuntime().exec("git diff "+startV+" "+endV+" --name-only");
        //process.waitFor();
        List<String> gitFiles = IOUtils.readLines(process.getInputStream(), "utf-8").stream().filter(p -> p.contains(javaCode) || p.contains(webCode) || p.contains(resCode)).map(p -> {
            if (p.contains(javaCode)) {
                return p.split(javaCode)[1];
            } else if (p.contains(resCode)) {
                return p.split(resCode)[1];
            } else {
                return p.split(webCode)[1];
            }
        }).collect(Collectors.toList());

        System.out.println("\r\n有效GIT变动文件列表");
        gitFiles.forEach(System.out::println);

        //有效的文件列表
        List<String> paths = filePaths.stream().filter(p -> p.contains(targetClass) || p.contains(webCode)).collect(Collectors.toList());
        System.out.println("\r\n有效的编译文件列表");
        paths.forEach(System.out::println);

        System.out.println("\r\n有效的GIT版本文件");
        for (String filePath : paths) {
            if (filePath.contains(targetClass)) {
                String cp = filePath.split(targetClass)[1];
                for (String gitFile : gitFiles) {
                    if (cp.contains(gitFile.replaceAll(".java", ""))) {
                        File destFile = new File(destPath + "/seeyon/WEB-INF/classes/" + cp);
                        FileUtils.copyFile(new File(filePath), destFile);
                        System.out.println(filePath);
                    }
                }
            }
            if (filePath.contains(webCode)) {
                String wp = filePath.split(webCode)[1];
                for (String gitFile : gitFiles) {
                    if (wp.contains(gitFile)) {
                        File destFile = new File(destPath + "/seeyon/" + wp);
                        FileUtils.copyFile(new File(filePath), destFile);
                        System.out.println(filePath);
                    }
                }
            }
        }
        return new File(destPath);
    }

    private static List<String> traverseFile(File file, List<String> resultFileName) {
        File[] files = file.listFiles();
        if (files == null) return resultFileName;// 判断目录下是不是空的
        for (File f : files) {
            if (f.isDirectory()) {// 判断是否文件夹
                traverseFile(f, resultFileName);// 调用自身,查找子目录
            } else
                resultFileName.add(f.getPath().replaceAll("\\\\", "/"));
        }
        return resultFileName;
    }
}
