package cn.reghao.bnt.web.devops.builder.tool;

import cn.reghao.bnt.web.util.text.FileProcessor;
import cn.reghao.bnt.web.util.text.ReplaceCharacter;
import cn.reghao.bnt.web.devops.builder.model.AppDto;
import cn.reghao.bnt.web.devops.builder.model.LocalBuildDir;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Objects;

/**
 * @author reghao
 * @date 2019-11-16 21:55:31
 */
public class BuilderUtil {
    /**
     * 复制源码到编译目录
     *
     * @param appLocalRepo 应用代码的本地仓库位置
     * @date 2019-12-03 上午9:55
     */
    public static void copyToCompileDir(String appLocalRepo, AppDto appDto) throws IOException {
        String appCompileDir = LocalBuildDir.compileDir + File.separator + appDto.getAppId();
        File compileDir = new File(appCompileDir);
        if (!compileDir.exists() && !compileDir.mkdirs()) {
            throw new IOException(compileDir + "不存在");
        } else {
            FileUtils.cleanDirectory(compileDir);
        }

        copyDirContentToDir(appLocalRepo, appCompileDir);
        // TODO Dotnet 应用需要额外的处理
        if (appDto.getAppType().equals("dotnetCore")) {
            // Dotnet 应用需要替换一些字符
            replaceCharacter(appCompileDir);
        }
    }

    static void copyDirContentToDir(String src, String dst) throws IOException {
        File srcDir = new File(src);
        File dstDir = new File(dst);
        for (File file : Objects.requireNonNull(srcDir.listFiles())) {
            if (file.isDirectory()) {
                String dirname = file.getName();
                File dstdir = new File(dst + File.separator + dirname);
                FileUtils.copyDirectory(file, dstdir);
            } else {
                FileUtils.copyFileToDirectory(file, dstDir);
            }
        }
    }

    /**
     * TODO 判断文件编码
     *
     * @param
     * @return
     * @date 2020-08-18 上午11:00
     */
    public static void replaceCharacter(String appCompileDir) {
        traversal(appCompileDir, new ReplaceCharacter());
    }

    static void traversal(String dirPath, FileProcessor processor) {
        Path path = Paths.get(dirPath);
        try {
            Files.walkFileTree(path, new FileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    return FileVisitResult.CONTINUE;
                }

                // 处理目录中的文件
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    processor.process(file.toFile());

                    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;
                }
            });
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}
