package io.wdd.rpc.scheduler.service.script;


import io.wdd.rpc.execute.service.ExecutionService;
import io.wdd.rpc.scheduler.beans.ScriptSchedulerDTO;
import io.wdd.rpc.scheduler.config.QuartzSchedulerUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

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

/**
 * 1.
 */
//@Service
@Slf4j
@Deprecated
public class AgentApplyScheduledScript {

    @Resource
    ExecutionService executionService;

    @Resource
    QuartzSchedulerUtils quartzSchedulerUtils;

    /**
     *  实际执行定时脚本任务的核心方法
     *
     * @param scriptSchedulerDTO
     */
    public void apply(ScriptSchedulerDTO scriptSchedulerDTO) {

        List<List<String>> completeCommandList = scriptSchedulerDTO.getCompleteCommandList();
        List<String> targetMachineList = scriptSchedulerDTO.getTargetMachineList();

        // 拿到执行任务的 resultKey
        HashMap<String, String> futureResultKeyMap = scriptSchedulerDTO.getAgentTopicNameToFutureResultKeyMap();

        // 新增-定时调用Nacos中预编辑脚本的内容
        String scriptType = scriptSchedulerDTO.getScriptType();
        if (StringUtils.isEmpty(scriptType)) {
            scriptType = "Scheduled Script";
        }

        // 发送命令到Agent中
        String finalScriptType = scriptType;
        List<String> resultKeyList = targetMachineList
                .stream()
                .map(
                        targetMachine -> {
//                            OctopusMessage octopusMessage = executionService
//                                    .SendCommandToAgent(
//                                            targetMachine,
//                                            finalScriptType,
//                                            null,
//                                            null,
//                                            completeCommandList,
//                                            false,
//                                            false
//                                    );


                            // 构建Map
                            futureResultKeyMap.put(
                                    targetMachine,
                                    "todo 2023年8月14日"
                            );

                            return "todo 2023年8月14日";
                        }
                )
                .collect(Collectors.toList());

        // 将 resultKeyList 放入这个DTO中
        scriptSchedulerDTO.setResultKeyList(resultKeyList);

        // 更新DTO中的下次执行时间 否则会出错
        HashMap<String, String> futureExecutionResultKey = quartzSchedulerUtils.generateAndSetFutureExecutionResultKey(scriptSchedulerDTO);

        log.debug(
                "futureExecutionResultKey in scheduled work is => {}",
                futureExecutionResultKey
        );

        // 需要更新数据库
        // 关联性数据库

        log.info("[ 定时脚本任务 ] - [ 业务类 ] - 实际执行完成!");
    }


}
