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

import cn.reghao.bnt.common.msg.constant.AppStatOps;
import cn.reghao.bnt.web.devops.app.model.dto.AppNode;
import cn.reghao.bnt.web.devops.app.db.query.AppDeployQuery;
import cn.reghao.bnt.web.devops.app.model.vo.AppRunning;
import cn.reghao.bnt.web.devops.app.model.vo.AppRunningNode;
import cn.reghao.bnt.web.devops.app.service.AppRunService;
import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.jutil.jdk.web.result.ResultStatus;
import cn.reghao.jutil.web.WebResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2019-08-30 18:49:15
 */
@Slf4j
@Tag(name = "应用状态页面")
@RestController
@RequestMapping("/api/devops/app/stat")
public class AppStatController {
    private final AppDeployQuery appDeployQuery;
    private final AppRunService appRunService;
    private int pageSize = 10;

    public AppStatController(AppDeployQuery appDeployQuery, AppRunService appRunService) {
        this.appDeployQuery = appDeployQuery;
        this.appRunService = appRunService;
    }

    @Operation(summary = "应用运行状态列表页面", description = "N")
    @GetMapping(value = "")
    public String statusPage1(@RequestParam(value = "pn") int pageNumber,
                              @RequestParam(value = "env") String env,
                              @RequestParam(value = "appType") String type) {
        PageRequest pageRequest = PageRequest.of(pageNumber-1, pageSize);
        Page<AppRunning> page = appDeployQuery.getAppRunningByPage(env, type, pageRequest);
        PageList<AppRunning> pageList = getPageList(page);
        return WebResult.success(pageList);
    }

    private PageList<AppRunning> getPageList(Page<AppRunning> page) {
        int pageNumber = page.getNumber() + 1;
        int pageSize = page.getSize();
        long total = page.getTotalElements();
        List<AppRunning> list = page.getContent();
        return PageList.pageList(pageNumber, pageSize, (int) total, list);
    }

    @Operation(summary = "应用运行状态详情页面", description = "N")
    @GetMapping(value = "/detail")
    public String statusPage2(@RequestParam(value = "appId") String appId) {
        List<AppRunningNode> list = appDeployQuery.getAppRunning(appId);
        return WebResult.success(list);
    }

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

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

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

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