package io.wdd.server.func;

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.Optional;

@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

        projectDeployContext.setProjectId(projectServerId);


//        ServerQueryEntity serverQueryEntity = new ServerQueryEntity();
//        // exsi server
////        serverQueryEntity.setServerName("Chengdu-amd64-99");
//        // lappro
////        serverQueryEntity.setServerName("Chengdu-amd64-65");
//
//        // cqga
//        serverQueryEntity.setServerName();
//        ServerInfoPO serverInfoPO = serverService
//                .serverGetByPage(serverQueryEntity)
//                .getRecords()
//                .get(0);

        String serverName = "Chongqing-amd64-01"; // cgga
//        String serverName = "Chengdu-amd64-99"; // lap pro

        ProjectServerVO projectServerVO = coreProjectServerService.projectServerOne(projectServerId);

        Optional<ServerInfoPO> serverInfoPOOptional = projectServerVO.getBindingServerList().stream().filter(
                serverInfoPO -> StringUtils.contains(serverInfoPO.getServerName(), serverName)
        ).findFirst();

        if (serverInfoPOOptional.isEmpty()) {
            System.out.printf("project of %s server of %s is empty", projectServerVO, serverName);
            return;
        }

        ServerInfoPO serverInfoPO = serverInfoPOOptional.get();
        System.out.println("serverInfoPO = " + serverInfoPO);

        projectDeployContext.setMasterNode(serverInfoPO);

//        baseFuncScheduler.runProcedure(projectDeployContext);

//        projectServerVO.getBindingServerList().forEach(
//                serverInfoPO1 -> {
//                    if (!StringUtils.contains(serverInfoPO1.getServerName(), "01")) {
//                        projectDeployContext.setMasterNode(serverInfoPO1);
//
//                        baseFuncScheduler.runProcedure(projectDeployContext);
//                    }
//                }
//        );

//        harborFuncScheduler.runProcedure(projectDeployContext);

        appFuncScheduler.runProcedure(projectDeployContext);
    }

}
