package io.wdd.rpc.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.wdd.common.response.R;
import io.wdd.rpc.beans.request.MetricQueryEntity;
import io.wdd.rpc.scheduler.service.status.AgentAliveStatusMonitorService;
import io.wdd.rpc.status.beans.AgentMetric;
import io.wdd.rpc.status.service.SyncStatusService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

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

import static io.wdd.rpc.status.AllAgentStatusCache.*;


//@RestController
@Api(value = "Agent运行状态Controller", tags = "Status")
//@RequestMapping("/octopus/server/status")
public class StatusController {

    @Resource
    SyncStatusService syncStatusService;

    @Resource
    AgentAliveStatusMonitorService agentAliveStatusMonitorService;

    @ApiOperation("[ Agent-状态 ] Map")
    @GetMapping("/agent/status")
    public R<Map<String, Boolean>> GetAllAgentHealthyStatus() {

        return R.ok(ALL_AGENT_STATUS_MAP);
    }

    @ApiOperation("[ 状态-Agent ] Map")
    @GetMapping("/status/agent")
    public R<Map<String, List<String>>> GetHealthyStatusAgentList() {

        return R.ok(STATUS_AGENT_LIST_MAP);
    }

    @ApiOperation("[ Agent ] - 所有的Agent主机")
    @PostMapping("/agent/all")
    public R<List<String>> GetAllAgentTopicNameList() {

        return R.ok(ALL_AGENT_TOPIC_NAME_LIST);
    }


    @ApiOperation("[ Agent ] - 所有主机String")
    @PostMapping("/agent/all/string")
    public R<String> GetAllAgentString() {

        String data = ALL_AGENT_TOPIC_NAME_LIST.toString();
        data.replace("[",
                     "");
        data.replace("]",
                     "");
        return R.ok(
                data
        );

    }


    @ApiOperation("[ Agent ] - 健康的Agent主机")
    @PostMapping("/agent/healthy")
    public R<List<String>> GetAllHealthyAgent() {

        return R.ok(ALL_HEALTHY_AGENT_TOPIC_NAME_LIST);
    }

    @ApiOperation("手动更新Agent的状态")
    @PostMapping("/agent/status/update")
    public R<Map<String, List<String>>> ManualUpdateAgentStatus() {

        // 暴露此部分 Status的核心代码
        agentAliveStatusMonitorService.collectAllAgentAliveStatus();

        return R.ok(STATUS_AGENT_LIST_MAP);
    }


    @ApiOperation("[ Agent-Metric ] 获取Agent的Metric信息")
    @PostMapping("/agent/metric")
    public R<ArrayList<AgentMetric>> QueryMetricStatus(
            @RequestBody MetricQueryEntity metricQueryEntity
    ) {

        return R.ok(syncStatusService.QueryMetricStatus(metricQueryEntity));
    }


}
