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

import cn.reghao.devops.web.mgr.app.service.AppBuildService;
import cn.reghao.devops.web.mgr.app.service.bd.BuildApp;
import cn.reghao.devops.web.mgr.app.service.bd.DeployApp;
import cn.reghao.devops.web.mgr.app.service.webhook.WebhookService;
import cn.reghao.jutil.jdk.result.Result;
import cn.reghao.jutil.jdk.result.WebResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * @author reghao
 * @date 2019-08-30 18:49:15
 */
@Slf4j
@Api(tags = "应用构建部署接口")
@RestController
@RequestMapping("/api/app/bd")
public class BuildDeployController {
    private final BuildApp buildApp;
    private final DeployApp deployApp;
    private final AppBuildService appBuildService;
    private final WebhookService webhookService;

    public BuildDeployController(BuildApp buildApp, DeployApp deployApp, AppBuildService appBuildService, WebhookService webhookService) {
        this.buildApp = buildApp;
        this.deployApp = deployApp;
        this.appBuildService = appBuildService;
        this.webhookService = webhookService;
    }

    @ApiOperation(value = "构建部署应用")
    @ApiImplicitParams(@ApiImplicitParam(name="appId", value="应用 ID", paramType="path", dataType = "String"))
    @PostMapping(value = "/update/{appId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String buildAndDeploy(@PathVariable("appId") String appId) throws Exception {
        Result result = buildApp.buildAndDeploy(appId, true);
        return WebResult.result(result);
    }

    @ApiOperation(value = "构建应用")
    @ApiImplicitParams(@ApiImplicitParam(name="appId", value="应用 ID", paramType="path", dataType = "String"))
    @PostMapping(value = "/build/{appId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String build(@PathVariable("appId") String appId) throws Exception {
        Result result = buildApp.buildAndDeploy(appId, false);
        return WebResult.result(result);
    }

    @ApiOperation(value = "部署应用")
    @ApiImplicitParams({
            @ApiImplicitParam(name="buildLogId", value="构建 ID", paramType="path", dataType = "String"),
            @ApiImplicitParam(name="machineId", value="机器 ID", paramType="path", dataType = "String")
    })
    @PostMapping(value = "/deploy/{buildLogId}/{machineId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deploy(@PathVariable("buildLogId") String buildLogId, @PathVariable("machineId") String machineId)
            throws Exception {
        if (buildLogId == null || buildLogId.equals("null")) {
            return WebResult.failWithMsg("应用尚未构建");
        }

        deployApp.deploy(buildLogId, machineId);
        return WebResult.successWithMsg("部署请求已提交");
    }

    @ApiOperation(value = "重置应用构建状态")
    @PostMapping(value = "/reset", produces = MediaType.APPLICATION_JSON_VALUE)
    public String resetState() {
        // TODO 暂不启用 web 接口, 只在应用启动时执行
        //appBuildService.resetState();
        return WebResult.failWithMsg("接口未实现");
    }

    @ApiOperation(value = "取消构建任务")
    @PostMapping(value = "/task/cancel/{appId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String cancelTask(@PathVariable("appId") String appId) {
        //buildApp.cancelTask(appId);
        return WebResult.failWithMsg("接口未实现");
    }

    @ApiOperation(value = "webhook 自动构建部署")
    @PostMapping("/hook")
    public String hook(@RequestBody String body) throws Exception {
        webhookService.handle(body);
        return WebResult.success();
    }

    @ApiOperation(value = "删除构建")
    @DeleteMapping(value = "/delete/{buildLogId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteBuild(@PathVariable("buildLogId") String buildLogId) throws Exception {
        appBuildService.deleteBuildLog(buildLogId);
        return WebResult.success();
    }
}
