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

import cn.reghao.autodop.common.util.thread.ThreadPoolWrapper;
import cn.reghao.autodop.dmaster.app.db.query.config.AppConfigQuery;
import cn.reghao.autodop.dmaster.app.db.repository.AppBuildingRepository;
import cn.reghao.autodop.dmaster.app.db.repository.AppDeployingRepository;
import cn.reghao.autodop.dmaster.app.db.repository.log.BuildLogRepository;
import cn.reghao.autodop.dmaster.app.service.BuildDeployService;
import cn.reghao.autodop.dmaster.app.service.bd.AppDeployer;
import cn.reghao.autodop.dmaster.app.service.bd.AppIntegrate;
import cn.reghao.autodop.dmaster.notification.service.NotifyService;
import cn.reghao.autodop.dmaster.spring.interceptor.AppIntegrateReinitInterceptor;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.ExecutorService;

/**
 * @author reghao
 * @date 2021-09-17 11:30:16
 */
@Service
public class BuildDeployServiceImpl implements BuildDeployService {
    private AppBuildingRepository appBuildingRepository;
    private AppDeployingRepository appDeployingRepository;
    private BuildLogRepository buildLogRepository;
    private AppIntegrateReinitInterceptor reinitInterceptor;
    // TODO 多线程访问的集合
    private Set<String> onBuilding = new ConcurrentSkipListSet<>();
    private Set<String> onDeploying = new ConcurrentSkipListSet<>();
    private Map<String, AppIntegrate> integrateMap = new ConcurrentHashMap<>();
    private AppConfigQuery appConfigQuery;
    private ExecutorService threadPool;
    private AppDeployer appDeployer;
    private NotifyService notifyService;

    public BuildDeployServiceImpl(AppBuildingRepository appBuildingRepository,
                                 AppDeployingRepository appDeployingRepository,
                                 AppIntegrateReinitInterceptor reinitInterceptor,
                                 AppConfigQuery appConfigQuery,
                                 BuildLogRepository buildLogRepository,
                                 NotifyService notify,
                                 AppDeployer appDeployer) {
        this.appBuildingRepository = appBuildingRepository;
        this.appDeployingRepository = appDeployingRepository;
        this.reinitInterceptor = reinitInterceptor;
        this.appConfigQuery = appConfigQuery;
        this.buildLogRepository = buildLogRepository;
        this.notifyService = notify;
        this.appDeployer = appDeployer;
        this.threadPool = ThreadPoolWrapper.threadPool("build-deploy");
    }

    @Override
    public String buildAndDeploy(String appId) {
        return "";
    }

    @Override
    public String build(String appId) {
        return "";
    }

    @Override
    public String build(String appId, String commitId) {
        return "";
    }

    @Override
    public boolean deploy(String appId, String commitId) {
        return false;
    }
}
