package io.wdd.server.func;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.wdd.func.auto.beans.AppFunctionEnum;
import io.wdd.func.auto.beans.BaseFunctionEnum;
import io.wdd.func.auto.beans.ProjectDeployContext;
import io.wdd.func.auto.service.AppFuncScheduler;
import io.wdd.func.auto.service.BaseFuncScheduler;
import io.wdd.func.auto.service.HarborFuncScheduler;
import io.wdd.server.beans.po.ServerInfoPO;
import io.wdd.server.beans.vo.ProjectServerVO;
import io.wdd.server.coreService.CoreProjectServerService;
import io.wdd.server.coreService.CoreServerService;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@SpringBootTest
public class TestBaseFuncScheduler {

    @Resource
    BaseFuncScheduler baseFuncScheduler;

    @Resource
    HarborFuncScheduler harborFuncScheduler;

    @Resource
    AppFuncScheduler appFuncScheduler;

    @Resource
    CoreServerService serverService;

    @Resource
    CoreProjectServerService coreProjectServerService;

    @Test
    public void testRunProcedure() {

        ProjectDeployContext projectDeployContext = new ProjectDeployContext();

//        Long projectServerId = 1751084188582440961L;// cqga
//        Long projectServerId = 1722453318596550657L;// lappro
//        Long projectServerId = 1752602668144975873L;// cqlyj
//        Long projectServerId = 1764575895186030593L;// 上海雷视联动
//        Long projectServerId = 1765290465066692610L;// 吉林移动
        Long projectServerId = 1765654823726669826L;// 资阳公安

        projectDeployContext.setProjectId(projectServerId);


//        String masterNodeServerName = "Shanghai-amd64-09"; // cgga
//        String masterNodeServerName = "Chengdu-amd64-51"; // lap pro
        String masterNodeServerName = "Ziyang-amd64-61"; // 资阳公安

        ProjectServerVO projectServerVO = coreProjectServerService.projectServerOne(projectServerId);
        Map<Boolean, List<ServerInfoPO>> collect = projectServerVO.getBindingServerList().stream().collect(
                Collectors.groupingBy(
                        serverInfoPO -> StringUtils.contains(serverInfoPO.getServerName(), masterNodeServerName)
                )
        );


        if (collect.get(Boolean.TRUE) == null) {
            System.out.printf("project of %s master server of %s is empty\n", projectServerVO, masterNodeServerName);
            return;
        }
        projectDeployContext.setMasterNode(collect.get(Boolean.TRUE).get(0));

        projectDeployContext.setAgentNodeList(collect.get(Boolean.FALSE));


        List<BaseFunctionEnum> masterNodeProcedure = List.of(
//                BaseFunctionEnum.DISABLE_SWAP,
//                BaseFunctionEnum.DISABLE_SELINUX,
//                BaseFunctionEnum.SHUTDOWN_FIREWALL,
//                BaseFunctionEnum.INSTALL_DOCKER,
//                BaseFunctionEnum.INSTALL_DEFAULT_SSH_KEY,
//                BaseFunctionEnum.INSTALL_DOCKER_COMPOSE,
//                BaseFunctionEnum.MODIFY_DOCKER_CONFIG
//                BaseFunctionEnum.INSTALL_HARBOR
        );

        List<BaseFunctionEnum> agentNodeProcedure = List.of(
                BaseFunctionEnum.DISABLE_SWAP,
                BaseFunctionEnum.INSTALL_DEFAULT_SSH_KEY,
                BaseFunctionEnum.DISABLE_SELINUX,
                BaseFunctionEnum.SHUTDOWN_FIREWALL,
                BaseFunctionEnum.INSTALL_DOCKER,
                BaseFunctionEnum.INSTALL_DOCKER_COMPOSE,
                BaseFunctionEnum.MODIFY_DOCKER_CONFIG
        );

        projectDeployContext.setMasterNodeBaseProcedure(masterNodeProcedure);
        projectDeployContext.setAgentNodeBaseProcedure(agentNodeProcedure);


        // log
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String s = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(projectDeployContext);
            System.out.println();
            System.out.println(s);
            System.out.println();
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

//        baseFuncScheduler.runProcedure(projectDeployContext);


//        harborFuncScheduler.runProcedure(projectDeployContext);


        List<AppFunctionEnum> appFunctionEnumList = List.of(
//                AppFunctionEnum.DEPLOY_CHRONY_SERVER,
//                AppFunctionEnum.DEPLOY_RKE
//                AppFunctionEnum.DEPLOY_K8S_DASHBOARD,
//                AppFunctionEnum.DEPLOY_NFS,
//                AppFunctionEnum.DEPLOY_TEST_NFS
//                AppFunctionEnum.DEPLOY_K8S_NAMESPACE,
//                AppFunctionEnum.DEPLOY_K8S_PVC,
//                AppFunctionEnum.DEPLOY_K8S_MYSQL,
//                AppFunctionEnum.DEPLOY_K8S_REDIS,
//                AppFunctionEnum.DEPLOY_K8S_MIDDLEWARES
//                AppFunctionEnum.DEPLOY_INGRESS,
//                AppFunctionEnum.DEPLOY_FRONTEND
//                AppFunctionEnum.DEPLOY_BACKEND
                AppFunctionEnum.DEPLOY_K8S_SRS

        );
        projectDeployContext.setMasterAppProcedure(appFunctionEnumList);

        appFuncScheduler.runProcedure(projectDeployContext);


        masterNodeProcedure = List.of(BaseFunctionEnum.CHRONY_TO_MASTER);
        agentNodeProcedure = List.of(BaseFunctionEnum.CHRONY_TO_MASTER);

        projectDeployContext.setMasterNodeBaseProcedure(masterNodeProcedure);
        projectDeployContext.setAgentNodeBaseProcedure(agentNodeProcedure);

//        baseFuncScheduler.runProcedure(projectDeployContext);
    }

}
