package cn.reghao.devops.web.mgr.app.controller;

import cn.reghao.devops.web.mgr.app.service.AppRunService;
import cn.reghao.devops.web.mgr.log.model.vo.AppLogVO;
import cn.reghao.devops.web.mgr.log.service.LogService;
import cn.reghao.devops.common.msg.constant.AppStatOps;
import cn.reghao.jutil.jdk.result.Result;
import cn.reghao.jutil.jdk.result.ResultStatus;
import cn.reghao.jutil.jdk.result.WebResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
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 RunStatController {
    private final AppRunService appRunService;
    private final LogService logService;
    
    public RunStatController(AppRunService appRunService, LogService logService) {
        this.appRunService = appRunService;
        this.logService = logService;
    }

    @ApiOperation(value = "重启应用")
    @PostMapping(value = "/restart/{appId}/{machineId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String restart(@PathVariable("appId") String appId, @PathVariable("machineId") String machineId) throws Exception {
        appRunService.changeStatus(appId, machineId, AppStatOps.restart);
        return WebResult.result(Result.result(ResultStatus.SUCCESS, appId + " 正在重启，请 10s 后刷新页面查看最新状态"));
    }

    @ApiOperation(value = "停止应用")
    @PostMapping(value = "/stop/{appId}/{machineId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String stop(@PathVariable("appId") String appId, @PathVariable("machineId") String machineId) throws Exception {
        appRunService.changeStatus(appId, machineId, AppStatOps.stop);
        return WebResult.result(Result.result(ResultStatus.SUCCESS, appId + " 正在停止，请稍后刷新页面"));
    }

    @ApiOperation(value = "启动应用")
    @PostMapping(value = "/start/{appId}/{machineId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String start(@PathVariable("appId") String appId, @PathVariable("machineId") String machineId) throws Exception {
        appRunService.changeStatus(appId, machineId, AppStatOps.start);
        return WebResult.result(Result.result(ResultStatus.SUCCESS, appId + " 正在启动，请 10s 后刷新页面查看最新状态"));
    }

    @ApiOperation(value = "应用当前状态")
    @GetMapping(value = "/{appId}/{machineId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String status(@PathVariable("appId") String appId, @PathVariable("machineId") String machineId) {
        appRunService.getStatus(appId, machineId);
        String msg = String.format("正在获取 %s 的状态，请 10s 后刷新页面查看", appId);
        return WebResult.result(Result.result(ResultStatus.SUCCESS, msg));
    }

    @ApiOperation(value = "应用历史日志")
    @GetMapping(value = "/log", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getAppLog() {
        List<AppLogVO> list = logService.getAppLogs("admin-service", "172.16.90.200", "INFO");
        return WebResult.success(list);
    }
}
