package cn.reghao.autodop.dmaster.app.service.config.impl;

import cn.reghao.autodop.dmaster.app.db.crud.config.CompilerConfigCrud;
import cn.reghao.autodop.dmaster.app.db.query.config.AppConfigQuery;
import cn.reghao.autodop.dmaster.app.model.constant.build.CompileType;
import cn.reghao.autodop.dmaster.app.model.po.config.AppConfig;
import cn.reghao.autodop.dmaster.app.model.po.config.build.CompilerConfig;
import cn.reghao.autodop.dmaster.app.service.config.CompilerConfigService;
import cn.reghao.jdkutil.result.Result;
import cn.reghao.jdkutil.result.ResultStatus;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2021-09-17 15:54:07
 */
@Service
public class CompilerConfigServiceImpl implements CompilerConfigService {
    private final CompilerConfigCrud compilerConfigCrud;
    private final AppConfigQuery appConfigQuery;

    public CompilerConfigServiceImpl(CompilerConfigCrud compilerConfigCrud, AppConfigQuery appConfigQuery) {
        this.compilerConfigCrud = compilerConfigCrud;
        this.appConfigQuery = appConfigQuery;
    }

    @Override
    public Result add(CompilerConfig compilerConfig) {
        Result result = valid(compilerConfig);
        if (result.getCode() != ResultStatus.SUCCESS.getCode()) {
            return result;
        }
        compilerConfigCrud.save(compilerConfig);
        return result;
    }

    @Override
    public Result modify(CompilerConfig compilerConfig) {
        Result result = valid(compilerConfig);
        if (result.getCode() != ResultStatus.SUCCESS.getCode()) {
            return result;
        }
        compilerConfigCrud.save(compilerConfig);
        return result;
    }

    private Result valid(CompilerConfig compilerConfig) {
        String type = compilerConfig.getType();
        switch (CompileType.valueOf(type)) {
            case shell:
                if (compilerConfig.getCompileCmd().isEmpty() || compilerConfig.getCompileCmd().isBlank()) {
                    // TODO 检测编译命令是否有效
                    return Result.result(ResultStatus.FAIL, "必须指定编译命令");
                }
                break;
            case maven:
                String homePath = compilerConfig.getHomePath();
                if (homePath.isEmpty() || homePath.isBlank()) {
                    return Result.result(ResultStatus.FAIL, "必须指定 maven 的主目录");
                }
                break;
            case docker:
                if (compilerConfig.getCompileCmd().isEmpty() || compilerConfig.getCompileCmd().isBlank()) {
                    // TODO 检测 dockerfile 是否有效
                    return Result.result(ResultStatus.FAIL, "编译命令应该是 dockerfile");
                }
                break;
            case no_need_compile:
                break;
            default:
                return Result.result(ResultStatus.FAIL, "暂不支持 " + type);
        }
        return Result.result(ResultStatus.SUCCESS);
    }

    @Override
    public Result delete(CompilerConfig compilerConfig) {
        List<AppConfig> apps = appConfigQuery.findAllByCompilerConfig(compilerConfig);
        if (apps.isEmpty()) {
            compilerConfigCrud.delete(compilerConfig);
            return Result.result(ResultStatus.SUCCESS);
        } else {
            List<String> ids = apps.stream().map(AppConfig::getAppId).collect(Collectors.toList());
            String msg = compilerConfig.getName() + " 删除失败, " + ids.toString() + " 等应用正在使用";
            return Result.result(ResultStatus.FAIL, msg);
        }
    }
}
