package com.bocloud.ims.booter.controller.resource.compute;

import com.bocloud.cmp.model.ServerTemplateModel;
import com.bocloud.common.annotations.Log;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.RequestContext;
import com.bocloud.common.utils.Common;
import com.bocloud.ims.interfaces.resource.ServerTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@Api(value = "云主机模板", tags = "云主机模板")
@RestController
@RequestMapping("/v1/vms/templates")
public class ServerTemplateController {

    @Autowired
    private ServerTemplateService serverTemplateService;

    @ApiOperation(tags = {"CMC", "CSC"}, value = "添加云主机模板", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "云主机模板", operation = "添加云主机模板")
    @PostMapping
    public BocloudResult create(@RequestBody ServerTemplateModel params,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return serverTemplateService.create(params, context);
    }

    @ApiOperation(tags = {"CMC", "CSC"}, value = "修改云主机模板", httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "云主机模板", operation = "修改云主机模板")
    @PutMapping("/{id}")
    public BocloudResult modify(@PathVariable(Common.ID) Long id,
                                @RequestBody ServerTemplateModel params,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        params.setId(id);
        return serverTemplateService.modify(params, context);
    }

    @ApiOperation(tags = {"CMC", "CSC"}, value = "删除云主机模板", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "云主机模板", operation = "删除云主机模板")
    @DeleteMapping("/{id}")
    public BocloudResult remove(@PathVariable(Common.ID) Long id,
                                @RequestParam(value = Common.VENDORID, required = false) Long vendorId,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return serverTemplateService.remove(id, vendorId, context);
    }

    @ApiOperation(tags = {"CMC", "CSC"}, value = "操作云主机模板", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "云主机模板", operation = "操作云主机模板")
    @PatchMapping("/{id}")
    public BocloudResult operate(@PathVariable(Common.ID) Long id,
                                 @RequestParam(value = Common.VENDORID, required = false) Long vendorId,
                                 @RequestParam(value = Common.ACTION, required = false) String action,
                                 @RequestBody ServerTemplateModel param,
                                 @Value(Common.REQ_CONTEXT) RequestContext context) {
        if (StringUtils.isEmpty(action)) {
            action = param.getAction();
        }
        if (vendorId == null) {
            vendorId = param.getVendorId();
        }
        return serverTemplateService.operate(id, vendorId, action, param, context);
    }

    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询云主机模板详情", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "云主机模板", operation = "查询云主机模板详情")
    @GetMapping("/{id}")
    public BocloudResult detail(@PathVariable(Common.ID) Long id,
                                @RequestParam(value = Common.VENDORID, required = false) Long vendorId,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return serverTemplateService.detail(id, vendorId, context);
    }
}
