package io.wdd.rpc.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.wdd.common.response.R;
import io.wdd.rpc.beans.request.ExecutionEntity;
import io.wdd.rpc.execute.service.ExecutionService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/octopus/server/executor")
@Api(value = "Agent执行命令的Controller", tags = "Execution")
public class ExecutionController {

    @Resource
    ExecutionService executionService;

    @PostMapping("/command/one")
    @ApiOperation("[命令] [异步]- 单台主机")
    public R<ArrayList<String>> patchCommandToAgent(
            @RequestParam(value = "topicName") @ApiParam(name = "topicName", value = "目标主机名称") String topicName,
            @RequestParam(value = "commandList", required = false) @Nullable List<String> commandList,
            @RequestParam(value = "completeCommandList", required = false)
            @ApiParam(name = "completeCommandList", value = "完整命令行，优先，可为空") @Nullable List<List<String>> completeCommandList,
            @RequestParam(value = "type", required = false) @Nullable String type,
            @ApiParam(name = "isDurationTask", value = "是否是持久化任务") @RequestParam(value = "isDurationTask", defaultValue = "false", required = false) boolean isDurationTask
    ) {

        return R.ok(null);
    }

    @PostMapping("/command/batch")
    @ApiOperation("[命令] [异步] - 批量主机")
    public R<List<ArrayList<String>>> patchCommandToAgentList(
            @RequestParam(value = "topicNameList")
            @ApiParam(name = "topicNameList", value = "目标机器列表") List<String> topicNameList,
            @RequestParam(value = "commandList", required = false)
            @ApiParam(name = "commandList", value = "命令行") @Nullable List<String> commandList,
            @RequestParam(value = "completeCommandList", required = false)
            @ApiParam(name = "completeCommandList", value = "完整命令行，优先，可为空") @Nullable List<List<String>> completeCommandList,
            @RequestParam(value = "type", required = false) @Nullable String type,
            @ApiParam(name = "isDurationTask", value = "是否是持久化任务") @RequestParam(value = "isDurationTask", defaultValue = "false", required = false) boolean isDurationTask
    ) {
        return R.ok(null);
    }


    @PostMapping("/command/all")
    @ApiOperation("[命令] [异步] - 所有的主机")
    public R<List<ArrayList<String>>> patchCommandToAllAgent(
            @RequestParam(value = "commandList", required = false)
            @ApiParam(name = "commandList", value = "命令行") @Nullable List<String> commandList,
            @RequestParam(value = "completeCommandList", required = false)
            @ApiParam(name = "completeCommandList", value = "完整命令行，优先，可为空") @Nullable List<List<String>> completeCommandList,
            @RequestParam(value = "type", required = false) @Nullable String type,
            @ApiParam(name = "isDurationTask", value = "是否是持久化任务") @RequestParam(value = "isDurationTask", defaultValue = "false", required = false) boolean isDurationTask
    ) {

        return R.ok(null);
    }

    @PostMapping("/command/healthy")
    @ApiOperation("[命令] [异步] - 健康的主机")
    public R<List<ArrayList<String>>> patchCommandToHealthyAgent(
            @RequestParam(value = "commandList", required = false)
            @ApiParam(name = "commandList", value = "命令行") @Nullable List<String> commandList,
            @RequestParam(value = "completeCommandList", required = false)
            @ApiParam(name = "completeCommandList", value = "完整命令行，优先，可为空") @Nullable List<List<String>> completeCommandList,
            @RequestParam(value = "type", required = false) @Nullable String type,
            @ApiParam(name = "isDurationTask", value = "是否是持久化任务") @RequestParam(value = "isDurationTask", defaultValue = "false", required = false) boolean isDurationTask
    ) {

//        List<ArrayList<String>> pathResult = syncExecutionService
//                .SyncSendCommandToAgentComplete(
//                        ALL_HEALTHY_AGENT_TOPIC_NAME_LIST,
//                        type,
//                        null,
//                        commandList,
//                        completeCommandList,
//                        false,
//                        null,
//                        isDurationTask
//                );
//
//
//        return R.ok(pathResult);
        return R.ok(null);


    }


    @PostMapping("/command/sync/one")
    @ApiOperation("[命令] [同步] - 单机-等待命令结果")
    public R<List<String>> SyncCommandToAgent(
            @RequestParam(value = "topicName") @ApiParam(name = "topicName", value = "目标主机名称") String topicName,
            @RequestParam(value = "commandList", required = false)
            @ApiParam(name = "commandList", value = "命令行") @Nullable List<String> commandList,
            @RequestParam(value = "completeCommandList", required = false)
            @ApiParam(name = "completeCommandList", value = "完整命令行，优先，可为空") @Nullable List<List<String>> completeCommandList,
            @RequestParam(value = "pipeLineCommand", required = false)
            @ApiParam(name = "pipeLineCommand", value = "完整命令行，优先，可为空") @Nullable List<List<String>> pipeLineCommand,
            @RequestParam(value = "executionType", required = false) @ApiParam(name = "executionType", value = "执行命令类型") @Nullable String executionType
    ) {

        ArrayList<String> resultLog = executionService
                .SendCommandToAgent(
                        topicName,
                        executionType,
                        null,
                        pipeLineCommand,
                        commandList,
                        completeCommandList,
                        true,
                        false
                );

        return R.ok(resultLog);

    }

    @PostMapping("/command/sync/one/body")
    @ApiOperation("[命令] [同步] - 单机-等待命令结果")
    public R<List<String>> SyncCommandToAgentBody(
            @RequestBody @Validated ExecutionEntity executionEntity
    ) {

        ArrayList<String> resultLog = executionService
                .SendCommandToAgent(
                        executionEntity.getAgentTopicName(),
                        executionEntity.getExecutionType(),
                        executionEntity.getFuncContent(),
                        executionEntity.getPipeLineCommand(),
                        executionEntity.getSingleLineCommand(),
                        executionEntity.getMultiLineCommand(),
                        executionEntity.isNeedResultReplay(),
                        executionEntity.isDurationTask()
                );

        return R.ok(resultLog);
    }

    @PostMapping("/command/sync/batch")
    @ApiOperation("[命令] [同步] - 批量-等待命令结果")
    public R<List<ArrayList<String>>> SyncPatchCommandToAgentBatch(
            @RequestParam(value = "topicNameList")
            @ApiParam(name = "topicNameList", value = "目标机器列表") List<String> topicNameList,
            @RequestParam(value = "commandList", required = false)
            @ApiParam(name = "commandList", value = "命令行") @Nullable List<String> commandList,
            @RequestParam(value = "completeCommandList", required = false)
            @ApiParam(name = "completeCommandList", value = "完整命令行，优先，可为空") @Nullable List<List<String>> completeCommandList,
            @RequestParam(value = "type", required = false) @ApiParam(name = "type", value = "执行命令类型") @Nullable String type,
            @ApiParam(name = "isDurationTask", value = "是否是持久化任务") @RequestParam(value = "isDurationTask", defaultValue = "false", required = false) boolean isDurationTask
    ) {

//        List<ArrayList<String>> pathResult = syncExecutionService
//                .SyncSendCommandToAgentComplete(
//                        topicNameList,
//                        type,
//                        null,
//                        commandList,
//                        completeCommandList,
//                        false,
//                        null,
//                        isDurationTask
//                );
//
//
//        return R.ok(pathResult);
        return R.ok(null);


    }

    @PostMapping("/command/sync/all")
    @ApiOperation("[命令] [同步] - 全部-同步等待命令结果")
    public R<List<ArrayList<String>>> SyncPatchCommandToAgentAll(
            @RequestParam(value = "commandList", required = false)
            @ApiParam(name = "commandList", value = "命令行") @Nullable List<String> commandList,
            @RequestParam(value = "completeCommandList", required = false)
            @ApiParam(name = "completeCommandList", value = "完整命令行，优先，可为空") @Nullable List<List<String>> completeCommandList,
            @RequestParam(value = "type", required = false) @ApiParam(name = "type", value = "执行命令类型") @Nullable String type,
            @ApiParam(name = "isDurationTask", value = "是否是持久化任务") @RequestParam(value = "isDurationTask", defaultValue = "false", required = false) boolean isDurationTask
    ) {

//        List<ArrayList<String>> pathResult = syncExecutionService
//                .SyncSendCommandToAgentComplete(
//                        ALL_AGENT_TOPIC_NAME_LIST,
//                        type,
//                        null,
//                        commandList,
//                        completeCommandList,
//                        false,
//                        null,
//                        isDurationTask
//                );
//
//
//        return R.ok(pathResult);
        return R.ok(null);


    }

    @PostMapping("/command/sync/healthy")
    @ApiOperation("[命令] [同步] - 健康的主机")
    public R<List<ArrayList<String>>> SyncPatchCommandToHealthyAgent(
            @RequestParam(value = "commandList", required = false)
            @ApiParam(name = "commandList", value = "命令行") @Nullable List<String> commandList,
            @RequestParam(value = "completeCommandList", required = false)
            @ApiParam(name = "completeCommandList", value = "完整命令行，优先，可为空") @Nullable List<List<String>> completeCommandList,
            @RequestParam(value = "type", required = false) @Nullable String type,
            @ApiParam(name = "isDurationTask", value = "是否是持久化任务") @RequestParam(value = "isDurationTask", defaultValue = "false", required = false) boolean isDurationTask
    ) {

//        List<ArrayList<String>> pathResult = syncExecutionService
//                .SyncSendCommandToAgentComplete(
//                        ALL_HEALTHY_AGENT_TOPIC_NAME_LIST,
//                        type,
//                        null,
//                        commandList,
//                        completeCommandList,
//                        false,
//                        null,
//                        isDurationTask
//                );
//
//
//        return R.ok(pathResult);
        return R.ok(null);


    }


    @PostMapping("/agentStatusStream")
    @ApiOperation("切换Console查看Agent状态日志")
    @Deprecated
    public R<String> getAgentStatusStrem(
            @RequestParam(value = "streamKey") @ApiParam(value = "status的Stream Key") String streamKey
    ) {

        return R.ok("请到控制台查看,已经切换至 =>  " + streamKey);

    }


}
