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

import cn.reghao.devops.common.msg.constant.AppStatOps;
import cn.reghao.devops.mgr.app.db.query.AppDeployQuery;
import cn.reghao.devops.mgr.app.model.vo.AppRunningNode;
import cn.reghao.devops.mgr.app.model.vo.AppRunning;
import cn.reghao.devops.mgr.app.service.AppRunService;
import cn.reghao.devops.mgr.util.DefaultSetting;
import cn.reghao.devops.mgr.util.PageSort;
import cn.reghao.jutil.jdk.result.Result;
import cn.reghao.jutil.jdk.result.ResultStatus;
import cn.reghao.jutil.web.WebResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author reghao
 * @date 2019-08-30 18:49:15
 */
@Slf4j
@Api(tags = "应用状态页面")
@Controller
@RequestMapping("/app/stat")
public class AppStatPageController {
    private final AppDeployQuery appDeployQuery;
    private final AppRunService appRunService;

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

    @ApiOperation(value = "应用运行状态列表页面", notes = "N")
    @GetMapping
    public String statusPage1(@RequestParam(value = "env", required = false) String env,
                              @RequestParam(value = "type", required = false) String appType,
                              Model model) {
        if (env == null) {
            env = DefaultSetting.getDefaultEnv();
        }

        if (appType == null) {
            appType = DefaultSetting.getDefaultAppType();
        }

        PageRequest pageRequest = PageSort.pageRequest();
        Page<AppRunning> page = appDeployQuery.getAppRunningByPage(env, appType, pageRequest);

        model.addAttribute("env", env);
        model.addAttribute("type", appType);
        model.addAttribute("page", page);
        model.addAttribute("list", page.getContent());
        return "/devops/app/stat/index";
    }

    @ApiOperation(value = "应用运行状态详情页面", notes = "N")
    @GetMapping(value = "/detail/{appId}")
    public String statusPage2(@PathVariable(value = "appId") String appId, Model model) {
        List<AppRunningNode> list = appDeployQuery.getAppRunning(appId);
        model.addAttribute("list", list);
        return "/devops/app/stat/detail";
    }

    @ApiOperation(value = "重启应用", notes = "N")
    @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 = "停止应用", notes = "N")
    @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 = "启动应用", notes = "N")
    @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 = "应用当前状态", notes = "N")
    @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));
    }
}
