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

import cn.reghao.autodop.dmaster.app.service.BuildDeployService;
import cn.reghao.jdkutil.result.WebBody;
import cn.reghao.autodop.dmaster.app.service.impl.BuildDeployConfigServiceImpl;
import cn.reghao.autodop.dmaster.app.service.BuildDeployDispatcher;
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 BuildDeployService buildDeployService;
    private final BuildDeployDispatcher buildDeployDispatcher;
    private final BuildDeployConfigServiceImpl buildService;

    public BuildDeployController(BuildDeployService buildDeployService,
                                 BuildDeployDispatcher buildDeployDispatcher, BuildDeployConfigServiceImpl buildService) {
        this.buildDeployService = buildDeployService;
        this.buildDeployDispatcher = buildDeployDispatcher;
        this.buildService = buildService;
    }

    @ApiOperation(value = "构建部署应用")
    @ApiImplicitParams(@ApiImplicitParam(name="appId", value="应用 ID", paramType="query", dataType = "String"))
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public String buildAndDeploy(@RequestParam("appId") String appId) throws Exception {
        //buildDeployService.buildAndDeploy(appId);
        String ret = buildDeployDispatcher.buildAndDeploy(appId, true);
        return WebBody.successWithMsg(ret);
    }

    @ApiOperation(value = "构建应用")
    @ApiImplicitParams(@ApiImplicitParam(name="appId", value="应用 ID", paramType="query", dataType = "String"))
    @PostMapping(value = "/build", produces = MediaType.APPLICATION_JSON_VALUE)
    public String build(@RequestParam("appId") String appId) throws Exception {
        //buildDeployService.build(appId);
        String ret = buildDeployDispatcher.buildAndDeploy(appId, false);
        return WebBody.successWithMsg(ret);
    }

    @ApiOperation(value = "部署应用")
    @ApiImplicitParams({
            @ApiImplicitParam(name="appId", value="应用 ID", paramType="query", dataType = "String"),
            @ApiImplicitParam(name="commitId", value="代码版本", paramType="query", dataType = "String")
    })
    @PostMapping(value = "/deploy", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deploy(@RequestParam("appId") String appId, @RequestParam("commitId") String commitId) {
        //buildDeployService.deploy(appId, commitId);
        buildDeployDispatcher.deploy(appId, commitId);
        return WebBody.success();
    }

    @ApiOperation(value = "部署应用")
    @ApiImplicitParams({
            @ApiImplicitParam(name="appId", value="应用 ID", paramType="query", dataType = "String"),
            @ApiImplicitParam(name="commitId", value="代码版本", paramType="query", dataType = "String")
    })
    @PostMapping(value = "/deploy/{appId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deployApp(@PathVariable("appId") String appId) {
        log.info("部署 {}", appId);
        return WebBody.success();
    }

    @Deprecated
    @ApiOperation(value = "刷新应用构建, 部署和运行列表")
    @PostMapping(value = "/refresh", produces = MediaType.APPLICATION_JSON_VALUE)
    public String refreshAppBuilding() {
        return WebBody.success();
    }

    @ApiOperation(value = "当前正在构建和部署的应用")
    @GetMapping(value = "/current", produces = MediaType.APPLICATION_JSON_VALUE)
    public String currentBuildingOrDeploy() {
        return WebBody.success(buildDeployDispatcher.current());
    }

    @ApiOperation(value = "清除当前正在构建和部署的应用")
    @PostMapping(value = "/clear", produces = MediaType.APPLICATION_JSON_VALUE)
    public String clearCurrentBuildingOrDeploy() {
        buildDeployDispatcher.clear();
        return WebBody.success();
    }
}
