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

import com.alibaba.fastjson.JSONObject;
import com.bocloud.common.annotations.Log;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.GridBean;
import com.bocloud.common.model.Pager;
import com.bocloud.common.model.RequestContext;
import com.bocloud.common.utils.Common;
import com.bocloud.ims.entity.resource.NameRule;
import com.bocloud.ims.interfaces.resource.NameRuleService;
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.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 命名规则控制器
 *
 * @author wangyu
 * @version 3.0.0-SNAPSHOT
 * @since 2018/7/2
 */
@RestController
@RequestMapping("/v1/namings")
@Api(value = "命名规则", tags = "命名规则")
public class NameRuleController {

    @Autowired
    private NameRuleService nameRuleService;

    /**
     * 获取命名规则列表
     *
     * @param pager
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "获取命名规则列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "命名规则", operation = "获取命名规则列表")
    @GetMapping
    public BocloudResult<GridBean<NameRule>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return nameRuleService.list(pager, context);
    }

    /**
     * 添加命名规则
     *
     * @param nameRule
     * @param context
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "添加命名规则",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "命名规则", operation = "添加命名规则")
    @PostMapping
    public BocloudResult<NameRule> create(@RequestBody NameRule nameRule,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return nameRuleService.create(nameRule, context);
    }

    /**
     * 修改命名规则
     *
     * @param id
     * @param nameRule
     * @param context
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "修改命名规则",  httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "命名规则", operation = "修改命名规则")
    @PutMapping("/{id}")
    public BocloudResult modify(@PathVariable(value = Common.ID) Long id,
                                @RequestBody NameRule nameRule, @Value(Common.REQ_CONTEXT) RequestContext context) {
        nameRule.setId(id);
        return nameRuleService.modify(nameRule, context);
    }

    /**
     * 根据命名规则生成云主机名称
     *
     * @param id
     * @param params
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "根据命名规则生成云主机名称",  httpMethod = "PATCH")
    @Log(module = BoCloudService.IMS, target = "命名规则", operation = "根据命名规则生成云主机名称")
    @PatchMapping("/{id}")
    public BocloudResult<String> patch(@PathVariable(value = Common.ID) Long id,
                               @RequestParam(value = Common.PARAMS) String params) {
        return nameRuleService.generate(id, JSONObject.parseObject(params));
    }

    /**
     * 删除命名规则
     *
     * @param id
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "删除命名规则",  httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "命名规则", operation = "删除命名规则")
    @DeleteMapping("/{id}")
    public BocloudResult remove(@PathVariable(value = Common.ID) Long id) {
        return nameRuleService.remove(id);
    }

    /**
     * 查看命名规则详情
     *
     * @param id
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "查看命名规则详情",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "命名规则", operation = "查看命名规则详情")
    @GetMapping("/{id}")
    public BocloudResult<NameRule> detail(@PathVariable(value = Common.ID) Long id) {
        return nameRuleService.detail(id);
    }

}
