package cn.stylefeng.team.generator.api.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.system.SystemUtil;
import cn.stylefeng.roses.kernel.file.api.expander.FileConfigExpander;
import cn.stylefeng.team.generator.api.constants.GenerationConstants;
import cn.stylefeng.team.generator.api.exception.GenerationException;
import cn.stylefeng.team.generator.api.exception.enums.ProjectGeneratorExceptionEnum;

import java.io.File;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.zip.ZipInputStream;

public class CommonGenerateUtil {

    public static String makeWorkDir(String projectGenerationId) {

        String workdir;
        if (SystemUtil.getOsInfo().isWindows()) {
            workdir = FileConfigExpander.getLocalFileSavePathWindows() + File.separator + GenerationConstants.PROJECT_TEMP_DIR + File.separator + projectGenerationId;
        } else {
            workdir = FileConfigExpander.getLocalFileSavePathLinux() + File.separator + GenerationConstants.PROJECT_TEMP_DIR + File.separator + projectGenerationId;
        }

        return workdir;
    }

    public static String makeWorkDir(Long projectGenerationId) {
        return makeWorkDir(String.valueOf(projectGenerationId));
    }

    public static void unzipResourceZipToWorkdir(String projectResourceName, String workdir) {
        InputStream inputStream = CommonGenerateUtil.class.getResourceAsStream(projectResourceName);
        if (inputStream == null) {
            throw new GenerationException(ProjectGeneratorExceptionEnum.PROJECT_TEMPLATE_CANT_FIND);
        }

        ZipInputStream zipInputStream = new ZipInputStream(inputStream);
        ZipUtil.unzip(zipInputStream, new File(workdir));
    }

    public static void settleJavaCode(String projectPackageName, String workdir) {

        String javaPath = workdir + File.separator + "src" + File.separator + "main" + File.separator + "java";
        File[] javaPathFiles = new File(javaPath).listFiles();
        if (javaPathFiles == null || javaPathFiles.length == 0) {
            return;
        }

        String packagePath = projectPackageName.replaceAll("\\.", Matcher.quoteReplacement(File.separator));
        String packageAbsolutePath = workdir + File.separator + "src" + File.separator + "main" + File.separator + "java" + File.separator + packagePath;
        File packageAbsolutePathDir = FileUtil.mkdir(packageAbsolutePath);

        for (File file : javaPathFiles) {
            FileUtil.move(file, packageAbsolutePathDir, true);
        }
    }

    public static String packageWorkdir(String workdir) {

        File zip = ZipUtil.zip(workdir);
        return zip.getPath();
    }

    public static void moveFiles(String fromDir, String toDir) {
        File[] fromDirFiles = new File(fromDir).listFiles();
        if (fromDirFiles == null || fromDirFiles.length == 0) {
            return;
        }

        FileUtil.mkdir(toDir);

        for (File file : fromDirFiles) {
            FileUtil.move(file, new File(toDir), true);
        }
    }

}
