package cn.reghao.autodop.dmaster.app.controller;

import cn.reghao.jdkutil.result.WebBody;
import cn.reghao.autodop.dmaster.app.model.constant.StatusOps;
import cn.reghao.autodop.dmaster.app.service.AppStatusService;
import cn.reghao.autodop.common.dagent.app.log.LogFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2020-02-28 11:33:23
 */
@Slf4j
@Api(tags = "应用状态管理接口")
@RestController
@RequestMapping("/api/app/status")
public class AppStatusController {
    private AppStatusService statusService;
    
    public AppStatusController(AppStatusService statusService) {
        this.statusService = statusService;
    }

    @ApiOperation(value = "重启应用")
    @PostMapping(value = "/restart/{appId}/{machineId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> restart(@PathVariable("appId") String appId,
                                          @PathVariable("machineId") String machineId) throws MqttException {
        statusService.changeStatus(appId, machineId, StatusOps.restart);
        return ResponseEntity.ok().body(WebBody.success(appId + " 已重启"));
    }

    @ApiOperation(value = "停止应用")
    @PostMapping(value = "/stop/{appId}/{machineId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> stop(@PathVariable("appId") String appId,
                                       @PathVariable("machineId") String machineId) throws MqttException {
        statusService.changeStatus(appId, machineId, StatusOps.stop);
        return ResponseEntity.ok().body(WebBody.success(appId + " 已停止"));
    }

    @ApiOperation(value = "启动应用")
    @PostMapping(value = "/start/{appId}/{machineId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> start(@PathVariable("appId") String appId,
                                        @PathVariable("machineId") String machineId) throws MqttException {
        statusService.changeStatus(appId, machineId, StatusOps.start);
        return ResponseEntity.ok().body(WebBody.success(appId + " 已启动"));
    }

    @ApiOperation(value = "刷新某个已部署应用状态")
    @PostMapping(value = "/refresh/{appId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> refreshDeployedApp(@PathVariable("appId") String appId) {
        statusService.refreshAppStatus(appId);
        return ResponseEntity.ok().body(WebBody.success());
    }

    @ApiOperation(value = "刷新已部署应用状态")
    @PostMapping(value = "/refresh", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> refreshDeployedApps() {
        statusService.refreshAppStatus();
        return ResponseEntity.ok().body(WebBody.success());
    }

    @ApiOperation(value = "获取指定主机上应用的所有日志文件")
    @GetMapping("/log/files")
    public ResponseEntity<String> appLogFiles(@RequestParam("machineId") String machineId,
                                              @RequestParam("appId") String appId) {
        List<LogFile> list = statusService.logFiles(appId, machineId);
        return ResponseEntity.ok().body(WebBody.success(list));
    }

    @ApiOperation(value = "获取应用日志内容")
    @GetMapping("/log/content")
    public ResponseEntity<String> appLogContent(@RequestParam("machineId") String machineId,
                                                @RequestParam("appId") String appId,
                                                @RequestParam("logType") String logType,
                                                @RequestParam("logLevel") String logLevel,
                                                @RequestParam("logFile") String logFile) {
        List<String> logs = statusService.logContent(appId, logType, logLevel, machineId, logFile);
        return ResponseEntity.ok().body(WebBody.success(logs));
    }
}
