package cn.reghao.devops.web.mgr.build.chain;

import cn.reghao.devops.common.docker.po.DockerAuth;
import cn.reghao.devops.web.mgr.build.model.AppDto;
import cn.reghao.devops.web.mgr.build.model.CompilerDto;
import cn.reghao.devops.web.mgr.build.model.PackerDto;
import cn.reghao.devops.web.mgr.build.model.RepoAuth;
import cn.reghao.devops.web.mgr.build.model.constant.CompileType;
import cn.reghao.devops.web.mgr.build.model.constant.RepoType;
import cn.reghao.devops.web.mgr.build.tool.compiler.EmptyCompiler;
import cn.reghao.devops.web.mgr.build.tool.compiler.MavenCompiler;
import cn.reghao.devops.web.mgr.build.tool.compiler.ShellCompiler;
import cn.reghao.devops.common.msg.constant.PackType;
import cn.reghao.devops.web.mgr.build.tool.compiler.CodeCompiler;
import cn.reghao.devops.web.mgr.build.tool.packer.CodePacker;
import cn.reghao.devops.web.mgr.build.tool.packer.DockerPack;
import cn.reghao.devops.web.mgr.build.tool.packer.ZipPack;
import cn.reghao.devops.web.mgr.build.tool.repo.CodeUpdater;
import cn.reghao.devops.web.mgr.build.tool.repo.GitImpl;

import java.util.HashMap;
import java.util.Map;

/**
 * @author reghao
 * @date 2022-05-07 14:35:11
 */
public class BuildTools {
    // appId -> {codeUpdate,codeCompiler,codePacker} -> name
    private static final Map<String, Map<String, String>> buildConfig = new HashMap<>();
    private static final Map<String, CodeUpdater> codeUpdaterMap = new HashMap<>();
    private static final Map<String, CodeCompiler> codeCompilerMap = new HashMap<>();
    private static final Map<String, CodePacker> codePackerMap = new HashMap<>();

    public static CodeUpdater getCodeUpdater(String appId) {
        String codeUpdater = buildConfig.get(appId).get("codeUpdater");
        return codeUpdaterMap.get(codeUpdater);
    }

    public static CodeCompiler getCodeCompiler(String appId) {
        String codeCompiler = buildConfig.get(appId).get("codeCompiler");
        return codeCompilerMap.get(codeCompiler);
    }

    public static CodePacker getCodePacker(String appId) {
        String codePacker = buildConfig.get(appId).get("codePacker");
        return codePackerMap.get(codePacker);
    }

    public static void removeApp(String appId) {
        buildConfig.remove(appId);
    }

    public static void removeCodeUpdater(String repoAuthName) {
        codeUpdaterMap.remove(repoAuthName);
    }

    public static void removeCodeCompiler(String compilerName) {
        codeCompilerMap.remove(compilerName);
    }

    public static void removeCodePacker(String packerName) {
        codePackerMap.remove(packerName);
    }

    public static void init(AppDto appDto) throws Exception {
        String appId = appDto.getAppId();
        RepoAuth repoAuth = appDto.getBuildConfigDto().getRepoAuth();
        DockerAuth dockerAuth = appDto.getBuildConfigDto().getDockerAuth();

        String repoAuthName = repoAuth.getName();
        CodeUpdater codeUpdater = codeUpdaterMap.get(repoAuthName);
        if (codeUpdater == null) {
            switch (RepoType.valueOf(repoAuth.getType())) {
                case git:
                    codeUpdater = new GitImpl(repoAuth);
                    break;
                default:
                    throw new Exception(repoAuth.getType() + " 仓库类型未实现");
            }
            codeUpdaterMap.put(repoAuthName, codeUpdater);
        }
        buildConfig.computeIfAbsent(appId, k -> new HashMap<>()).put("codeUpdater", repoAuthName);

        CompilerDto compilerDto = appDto.getBuildConfigDto().getCompilerDto();
        String compilerName = compilerDto.getName();
        CodeCompiler codeCompiler = codeCompilerMap.get(compilerName);
        if (codeCompiler == null) {
            switch (CompileType.valueOf(compilerDto.getType())) {
                case shell:
                    codeCompiler = new ShellCompiler(compilerDto);
                    break;
                case maven:
                    codeCompiler = new MavenCompiler(compilerDto.getHomePath());
                    break;
                case docker:
                case none:
                    codeCompiler = new EmptyCompiler();
                    break;
                default:
                    throw new Exception(compilerDto.getType() + " 编译类型未实现");
            }
            codeCompilerMap.put(compilerName, codeCompiler);
        }
        buildConfig.computeIfAbsent(appId, k -> new HashMap<>()).put("codeCompiler", compilerName);

        PackerDto packerDto = appDto.getBuildConfigDto().getPackerDto();
        String packerName = packerDto.getName();
        CodePacker codePacker = codePackerMap.get(packerName);
        if (codePacker == null) {
            switch (PackType.valueOf(packerDto.getType())) {
                case docker:
                    if (dockerAuth != null) {
                        codePacker = new DockerPack(packerDto.getTargetPath(), dockerAuth);
                    } else {
                        codePacker = new DockerPack(packerDto.getTargetPath());
                    }

                    break;
                default:
                    codePacker = new ZipPack(packerDto.getBinDirname());
                    break;
            }
            codePackerMap.put(packerName, codePacker);
        }
        buildConfig.computeIfAbsent(appId, k -> new HashMap<>()).put("codePacker", packerName);
    }
}
