package io.wdd.func.auto.service;

import io.wdd.func.auto.beans.AppFunctionEnum;
import io.wdd.func.auto.beans.BaseFunctionEnum;
import io.wdd.func.auto.beans.HarborFunctionEnum;
import io.wdd.rpc.execute.ExecutionMessageType;
import io.wdd.rpc.execute.service.ExecutionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

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

@Service
@Slf4j
public class FuncServiceImpl implements FuncService {

    @Resource
    ExecutionService executionService;


    @Override
    public List<String> callBaseFuncService(String agentTopicName, String baseFunctionName, List<String> funcArgs) {

        return this.syncCallFunction(
                agentTopicName,
                ExecutionMessageType.BASE,
                funcArgs,
                false
        );
    }

    @Override
    public List<String> callAppFuncService(String agentTopicName, String appFunctionName, List<String> funcArgs) {

        return this.syncCallFunction(
                agentTopicName,
                ExecutionMessageType.APP,
                funcArgs,
                false
        );
    }

    @Override
    public boolean callBaseFuncAndJudge(String agentTopicName, BaseFunctionEnum baseFunctionEnum, List<String> funcArgs, boolean durationTask) {
        log.debug(
                "调用基本脚本 => {}",
                baseFunctionEnum.getDesc()
        );

        List<String> syncResultLog = syncCallFunction(
                agentTopicName,
                ExecutionMessageType.BASE,
                funcArgs,
                durationTask
        );

        return JudgeSyncBaseCommandResult(syncResultLog);
    }

    @Override
    public boolean callAppFuncAndJudge(String agentTopicName, AppFunctionEnum appFunctionEnum, List<String> funcArgs) {
        List<String> syncResultLog = syncCallFunction(
                agentTopicName,
                ExecutionMessageType.APP,
                funcArgs,
                true
        );

        boolean appFuncResult = JudgeSyncBaseCommandResult(syncResultLog);

        return appFuncResult;
    }

    @Override
    public boolean callHarborFuncAndJudge(String agentTopicName, HarborFunctionEnum harborFunctionEnum, ArrayList<String> funcArgs) {

        List<String> syncResultLog = syncCallFunction(
                agentTopicName,
                ExecutionMessageType.HARBOR,
                funcArgs,
                true
        );

        boolean harborResult = JudgeSyncBaseCommandResult(syncResultLog);

        return harborResult;
    }

    private boolean JudgeSyncBaseCommandResult(List<String> syncResultLog) {
        if (CollectionUtils.isEmpty(syncResultLog)) {
            log.error("基本脚本调用失败!");
            return false;
        }

        if (syncResultLog
                .get(syncResultLog.size() - 1)
                .endsWith("true")) {

            log.debug(
                    "基本脚本调用成功 => {}",
                    syncResultLog
            );

            return true;
        }

        log.error(
                "基本脚本调用失败 !! => {}",
                syncResultLog
        );

        return false;
    }


    // 归一化调用
    private List<String> syncCallFunction(String agentTopicName, ExecutionMessageType emType, List<String> funcArgs, boolean durationTask) {

        Assert.notNull(
                funcArgs,
                "syncCallFunction funArgs can not be null! "
        );

        // 调用
        ArrayList<String> resultLog = executionService.SendCommandToAgent(
                agentTopicName,
                emType.toString(),
                funcArgs,
                null,
                null,
                null,
                true,
                durationTask
        );

        return resultLog;
    }

}
