package io.wdd.func.auto.service;

import io.wdd.func.auto.beans.AppFunctionEnum;
import io.wdd.func.auto.beans.ProjectDeployContext;
import io.wdd.server.beans.po.ProjectInfoPO;
import io.wdd.server.coreService.CoreProjectService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

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

@Service
@Slf4j
public class AppFuncScheduler {

    @Resource
    FuncService funcService;

    @Resource
    CoreProjectService coreProjectService;

    /**
     * 参数固定顺序为 A1C2IP SUPREME N1C2IP A1C1IP A1C1JS M2D2IP KIMMY JACLOVE
     * 1       2      3      4       5     6     7       8
     *
     * @param projectDeployContext
     * @param projectInfoPO
     * @return
     */
    private static ArrayList<String> buildAppFuncArgs(ProjectDeployContext projectDeployContext, ProjectInfoPO projectInfoPO) {

        ArrayList<String> appFuncArgs = new ArrayList<>();
        appFuncArgs.add(projectDeployContext
                .getMasterNode()
                .getServerIpInV4());
        appFuncArgs.add(projectInfoPO.getProjectNamespace());
        appFuncArgs.add(projectDeployContext
                .getMasterNode()
                .getServerIpInV4());
        appFuncArgs.add(projectDeployContext
                .getMasterNode()
                .getServerIpPbV4());
        appFuncArgs.add(projectInfoPO.getProjectWebServicePort());
        // M2D2IP
        appFuncArgs.add(projectDeployContext
                .getMasterNode()
                .getServerIpInV4());
        appFuncArgs.add(projectInfoPO.getProjectVersion());
        // jackeyLoveFile
        appFuncArgs.add("init_5.1.0.tar");

        return appFuncArgs;
    }

    public boolean runProcedure(ProjectDeployContext projectDeployContext) {

        // before run
        // check base requirement
        beforeRunProcedure(projectDeployContext);


        // during run
        doRunProcedure(projectDeployContext);


        // after run
        afterRunProcedure(projectDeployContext);


        return true;
    }

    private void afterRunProcedure(ProjectDeployContext projectDeployContext) {

        // 检查是否安装完成, 对安装环境进行判定
        log.debug("afterRunProcedure complete!");
    }

    private void doRunProcedure(ProjectDeployContext projectDeployContext) {

        // 执行 基础功能施工的内容
        String masterTopicName = projectDeployContext
                .getMasterNode()
                .getTopicName();

        ProjectInfoPO projectInfoPO = coreProjectService.projectGetOne(projectDeployContext.getProjectId());
        if (ObjectUtils.isEmpty(projectInfoPO)) {
            log.error(
                    "project id of [{}] wrong ! cant find project !",
                    projectDeployContext.getProjectId()
            );
        }

        ArrayList<String> appFuncArgs = buildAppFuncArgs(
                projectDeployContext,
                projectInfoPO
        );

        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

        );


        for (AppFunctionEnum appFunctionEnum : appFunctionEnumList) {

            // rebuild
            appFuncArgs.add(
                    0,
                    appFunctionEnum.getAppOpname()
            );

            boolean appFuncJudge = funcService.callAppFuncAndJudge(
                    masterTopicName,
                    appFunctionEnum,
                    appFuncArgs
            );

            if (!appFuncJudge) {
                log.error(
                        "app func of {} execute error !",
                        appFunctionEnum.getAppOpname()
                );
                return;
            }


            // must remove this one
            appFuncArgs.remove(0);

        }

    }

    private void beforeRunProcedure(ProjectDeployContext projectDeployContext) {

        // 检查是否符合规定

        // 检查每一个Agent是否能够连通,调用命令返回结果

        // 打印施工信息

        log.debug("beforeRunProcedure complete!");

    }

}
