package com.lanyuanxiaoyao.service.forest.service.launcher;

import cn.hutool.core.util.StrUtil;
import com.eshore.odcp.hudi.connector.Constants;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.map.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @author lanyuanxiaoyao
 * @date 2023-06-06
 */
@Service
public class LaunchersService {
    private static final Logger logger = LoggerFactory.getLogger(LaunchersService.class);

    private final ImmutableMap<String, LauncherService> serviceMap;

    public LaunchersService(
            A4LauncherService a4LauncherService,
            B1LauncherService b1LauncherService,
            B5LauncherService b5LauncherService,
            B5SyncLauncherService b5SyncLauncherService,
            B12LauncherService b12LauncherService
    ) {
        Map<String, LauncherService> map = new HashMap<>(5);
        map.put(Constants.CLUSTER_A4, a4LauncherService);
        map.put(Constants.CLUSTER_B1, b1LauncherService);
        map.put(Constants.CLUSTER_B5, b5LauncherService);
        map.put(Constants.CLUSTER_B5_SYNC, b5SyncLauncherService);
        map.put(Constants.CLUSTER_B12, b12LauncherService);
        serviceMap = Maps.immutable.ofAll(map);
    }

    private LauncherService getService(String cluster) {
        if (serviceMap.containsKey(cluster)) {
            return serviceMap.get(cluster);
        }
        throw new RuntimeException(StrUtil.format("Cluster {} not found", cluster));
    }

    private ImmutableList<LauncherService> getServices() {
        return serviceMap.valuesView().toList().toImmutable();
    }

    public void compactionStop(Long flinkJobId, String alias) {
        for (LauncherService service : getServices()) {
            service.compactionStop(flinkJobId, alias);
        }
    }

    public void compactionStop(Long flinkJobId, String alias, Boolean disableMeta) {
        for (LauncherService service : getServices()) {
            service.compactionStop(flinkJobId, alias, disableMeta);
        }
    }

    public void compactionStop(String cluster, Long flinkJobId, String alias) {
        LauncherService service = getService(cluster);
        service.compactionStop(flinkJobId, alias);
    }

    public void compactionStopApp(String applicationId) {
        for (LauncherService service : getServices()) {
            service.compactionStopApp(applicationId);
        }
    }

    public void compactionStopApp(String cluster, String applicationId) {
        LauncherService service = getService(cluster);
        service.compactionStopApp(applicationId);
    }
}
