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

import com.bocloud.cmp.model.VappParam;
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.entity.resource.VApp;
import com.bocloud.ims.interfaces.resource.VAppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/vapps")
@Api(value = "vApp", tags = "vApp")
public class VAppController {

    @Autowired
    private VAppService vAppService;

    /**
     * 查询vapp
     *
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询vapp列表", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "vapp", operation = "查询vapp列表")
    @GetMapping
    public BocloudResult<List<VApp>> list() {
        return vAppService.list(null, null, null);
    }

    /**
     * 根据condition查询vapp列表
     *
     * @param condition
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "根据condition查询vapp列表", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "vapp", operation = "查询vapp列表")
    @GetMapping("/condition")
    public BocloudResult condition(@RequestParam(value = Common.CONDITION, required = false) String condition) {
        return vAppService.vrps(condition);
    }

    /**
     * 创建vapp
     *
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "创建vapp", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "vapp", operation = "创建vapp")
    @PostMapping
    public BocloudResult<String> create(@RequestBody VApp vapp, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return vAppService.create(vapp, context);
    }

    /**
     * 修改vapp
     *
     * @param id
     * @param vapp
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "修改vapp", httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "vapp", operation = "修改vapp")
    @PutMapping(value = "/{id}")
    public BocloudResult<String> modify(@PathVariable(value = Common.ID) Long id, @RequestBody VApp vapp,
        @Value(Common.REQ_CONTEXT) RequestContext context) {
        vapp.setId(id);
        return vAppService.modify(vapp, context);
    }

    /**
     * 删除vapp
     *
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "删除vapp", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "vapp", operation = "删除vapp")
    @DeleteMapping(value = "/{id}")
    public BocloudResult<String> remove(@PathVariable(value = Common.ID) Long id,
        @Value(Common.REQ_CONTEXT) RequestContext context) {
        return vAppService.remove(id, context.getTarget());
    }

    /**
     * 查询vapp详情
     *
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询vapp详情", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "vapp", operation = "查询vapp详情")
    @GetMapping(value = "/{id}")
    public BocloudResult<VApp> detail(@PathVariable(value = Common.ID) Long id,
        @Value(Common.REQ_CONTEXT) RequestContext context) {
        return vAppService.detail(id);
    }

    /**
     * 批量删除vapp
     *
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "批量删除vapp", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "vapp", operation = "批量删除vapp")
    @DeleteMapping
    public BocloudResult batchRemove(@RequestBody List<Long> idArray,
        @Value(Common.REQ_CONTEXT) RequestContext context) {
        return vAppService.batchRemove(idArray, context);
    }

    @ApiOperation(value = "vapp操作")
    @Log(module = BoCloudService.IMS, target = "vapp", operation = "vapp操作")
    @PatchMapping(value = "/{id}")
    public BocloudResult action(@PathVariable(value = Common.ID) Long id, @RequestBody VappParam vappParam,
        @Value(Common.REQ_CONTEXT) RequestContext context) {
        if (null == vappParam) {
            return new BocloudResult<>(false, "参数格式错误");
        }
        if ("start".equalsIgnoreCase(vappParam.getAction())) {
            return vAppService.start(id, context);
        } else if ("stop".equalsIgnoreCase(vappParam.getAction())) {
            return vAppService.stop(id, context);
        }
        return vAppService.suspend(id, context);
    }
}
