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

import cn.reghao.autodop.dmaster.app.model.dto.DeployConfigs;
import cn.reghao.autodop.dmaster.app.service.config.AppConfigService;
import cn.reghao.autodop.dmaster.machine.db.query.MachineHostQuery;
import cn.reghao.jdkutil.result.Result;
import cn.reghao.jdkutil.result.ResultStatus;
import cn.reghao.autodop.dmaster.app.model.dto.NewApp;
import cn.reghao.autodop.dmaster.app.model.po.config.AppConfig;
import cn.reghao.autodop.dmaster.notification.model.po.NotifyGroup;
import cn.reghao.jdkutil.result.WebBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2019-11-27 11:29:43
 */
@Slf4j
@Api(tags = "应用配置 CRUD 接口")
@RestController
@RequestMapping("/api/app/config/app")
public class AppConfigController {
    private final AppConfigService appConfigService;
    private final MachineHostQuery hostQuery;

    public AppConfigController(AppConfigService appConfigService, MachineHostQuery hostQuery) {
        this.appConfigService = appConfigService;
        this.hostQuery = hostQuery;
    }

    /* 应用编排 */
    @ApiOperation(value = "添加/修改应用编排")
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public String addAppOrchestration(@Validated AppConfig app) {
        Integer id = app.getId();
        Result result;
        if (id == null) {
            result = appConfigService.add(app);
        } else {
            result = appConfigService.modify(app);
        }
        return WebBody.result(result);
    }

    // TODO 使用 @PathVariable 注解时会自动填充实体
    @ApiOperation(value = "复制应用编排")
    @PostMapping(value = "/copy/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String copyAppOrchestration(@PathVariable("id") AppConfig app, @Validated NewApp newApp) {
        Result result;
        try {
            result = appConfigService.copy(app, newApp);
        } catch (Exception e) {
            String msg = e.getMessage();
            result = Result.result(ResultStatus.ERROR, msg);
        }
        return WebBody.result(result);
    }

    @ApiOperation(value = "设置应用通知")
    @PostMapping(value = "/notify", produces = MediaType.APPLICATION_JSON_VALUE)
    public String appNotify(@RequestParam("id") int id,
                            @RequestParam(value = "groupId", required = false) List<NotifyGroup> notifyGroups) {
        appConfigService.setNotify(id, notifyGroups);
        return WebBody.success();
    }

    @ApiOperation(value = "设置应用部署配置")
    @PostMapping(value = "/deploy", produces = MediaType.APPLICATION_JSON_VALUE)
    public String appDeploy(@RequestParam("id") int id, @Validated DeployConfigs deployConfigs) {
        if (deployConfigs.getDeployConfigs().isEmpty()) {
            return WebBody.success();
        }

        Result result = appConfigService.setDeployConfig(id, deployConfigs.getDeployConfigs());
        return WebBody.result(result);
    }

    @ApiOperation(value = "删除应用编排")
    @DeleteMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteAppOrchestration(@PathVariable("id") AppConfig app) {
        Result result = appConfigService.delete(app);
        return WebBody.result(result);
    }
}
