package io.wdd.server.func;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.wdd.func.auto.beans.ImageSyncContext;
import io.wdd.func.auto.service.ImageFuncScheduler;
import io.wdd.server.beans.po.ProjectInfoPO;
import io.wdd.server.beans.po.ServerInfoPO;
import io.wdd.server.beans.request.ProjectQueryEntity;
import io.wdd.server.beans.vo.ProjectServerVO;
import io.wdd.server.coreService.CoreProjectServerService;
import io.wdd.server.coreService.CoreProjectService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@SpringBootTest
@Slf4j
public class TestImageSyncScheduler {

    @Resource
    ImageFuncScheduler imageFuncScheduler;


    @Autowired
    CoreProjectService coreProjectService;

    @Autowired
    CoreProjectServerService coreProjectServerService;

    @Test
    public void runImageSync() {

        ArrayList<String> CmiiAppNameList = new ArrayList<>(List.of(
//                "cmii-uav-integration:5.4.0-070301"
        ));

        ArrayList<String> ImageFullNameList = new ArrayList<>(List.of(
                "harbor.cdcyy.com.cn/cmii/cmii-uav-platform:5.5.0-26810-071201"
        ));

        Boolean downloadAndCompressOnly = false;

//        String projectNamespace = "wdd"; // wdd
        String projectNamespace = "cqlyj"; // 重庆林业局
//        String projectNamespace = "jlyd"; // 吉林移动
//        String projectNamespace = "jxyd"; // 吉林移动
//        String projectNamespace = "xmyd"; // 厦门移动

//        String innerWorkerAgentName = "Chengdu-amd64-65-lapwdd"; //wdd
        String innerWorkerAgentName = "Chengdu-amd64-071-ed6e2b7f"; //prod

        // 之下不要修改 除非你知道自己在干什么！

        // start
        ImageSyncContext imageSyncContext = new ImageSyncContext();

        imageSyncContext.setDownloadAndCompressOnly(downloadAndCompressOnly);

        // inner
        imageSyncContext.setInnerWorkerAgentName(innerWorkerAgentName); // dev

        ProjectQueryEntity projectQueryEntity = ProjectQueryEntity.builder().projectNamespace(projectNamespace).build();

        Page<ProjectInfoPO> page = coreProjectService.projectQueryByEntity(projectQueryEntity);
        if (page.getSize() < 1) {
            log.error(" project name space error !");
            return;
        }
        ProjectInfoPO projectInfoPO = page.getRecords().get(0);
        imageSyncContext.setProjectInfoPO(projectInfoPO);

        log.info("project name => {}  namespace => {}", projectInfoPO.getProjectName(), projectInfoPO.getProjectNamespace());
        // master
        ProjectServerVO projectServerVO = coreProjectServerService.projectServerOne(projectInfoPO.getProjectId());
        Optional<ServerInfoPO> master = projectServerVO.getBindingServerList().stream().filter(server -> StringUtils.startsWith(server.getRole(), "master")).findFirst();
        if (master.isEmpty()) {
            log.error("project master node not set ！");
            return;
        }
        imageSyncContext.setProjectMasterNode(master.get());

        log.info("project master node inner ipv4 is => {}", master.get().getServerIpInV4());

        imageSyncContext.setImageFullNameList(ImageFullNameList);
        imageSyncContext.setCmiiAppNameTagList(CmiiAppNameList);

        imageFuncScheduler.runProcedure(imageSyncContext);
    }

    @Test
    public void downloadAndCompressOnly() {
        ArrayList<String> CmiiAppNameList = new ArrayList<>(List.of(
//                "harbor.cdcyy.com.cn/cmii/cmii-uav-mission:5.3.0-cqly-032802"
        ));

        ArrayList<String> ImageFullNameList = new ArrayList<>(List.of(
                "harbor.cdcyy.com.cn/cmii/cmii-uav-platform:5.3.0-cqly-042901"
        ));

        Boolean downloadAndCompressOnly = true;

        String projectNamespace = "wdd";

//        String innerWorkerAgentName = "Chengdu-amd64-65-lapwdd"; //wdd
        String innerWorkerAgentName = "Chengdu-amd64-71-3571gd"; //prod

        // 之下不要修改 除非你知道自己在干什么！

        // start
        ImageSyncContext imageSyncContext = new ImageSyncContext();

        imageSyncContext.setDownloadAndCompressOnly(downloadAndCompressOnly);

        // inner
        imageSyncContext.setInnerWorkerAgentName(innerWorkerAgentName); // dev

        ProjectQueryEntity projectQueryEntity = ProjectQueryEntity.builder().projectNamespace(projectNamespace).build();

        Page<ProjectInfoPO> page = coreProjectService.projectQueryByEntity(projectQueryEntity);
        if (page.getSize() < 1) {
            log.error(" project name space error !");
            return;
        }
        ProjectInfoPO projectInfoPO = page.getRecords().get(0);
        imageSyncContext.setProjectInfoPO(projectInfoPO);

        log.info("project name => {}  namespace => {}", projectInfoPO.getProjectName(), projectInfoPO.getProjectNamespace());
        // master
        ProjectServerVO projectServerVO = coreProjectServerService.projectServerOne(projectInfoPO.getProjectId());
        Optional<ServerInfoPO> master = projectServerVO.getBindingServerList().stream().filter(server -> StringUtils.startsWith(server.getRole(), "master")).findFirst();
        if (master.isEmpty()) {
            log.error("project master node not set ！");
            return;
        }
        imageSyncContext.setProjectMasterNode(master.get());

        log.info("project master node inner ipv4 is => {}", master.get().getServerIpInV4());

        imageSyncContext.setImageFullNameList(ImageFullNameList);
        imageSyncContext.setCmiiAppNameTagList(CmiiAppNameList);

        imageFuncScheduler.runProcedure(imageSyncContext);
    }

}
