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

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.FirewallRule;
import com.bocloud.ims.interfaces.resource.FirewallRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

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.*;

@RestController
@RequestMapping("/v1/firewalls/rules")
@Api(value = "防火墙规则", tags = "防火墙规则")
public class FirewallRuleController {

    @Autowired
    private FirewallRuleService firewallRuleService;

    /**
     * 查询防火墙规则列表
     *
     * @param pager
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询防火墙规则列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "防火墙规则", operation = "查询防火墙规则列表")
    @GetMapping
    public BocloudResult<GridBean<FirewallRule>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return firewallRuleService.list(pager, context);
    }

    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询防火墙规则列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "防火墙规则", operation = "查询防火墙规则列表")
    @GetMapping("/condition")
    public BocloudResult condition(@RequestParam(value = Common.CONDITION, required = false) String condition,
                                   @Value(Common.REQ_CONTEXT) RequestContext context) {
        return firewallRuleService.condition(condition, context);
    }

    /**
     * 创建防火墙规则
     *
     * @param rule
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "创建防火墙规则",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "防火墙规则", operation = "创建防火墙规则")
    @PostMapping
    public BocloudResult<FirewallRule> create(@RequestBody FirewallRule rule,
                                              @Value(Common.REQ_CONTEXT) RequestContext context) {
        return firewallRuleService.create(rule, context);
    }

    /**
     * 修改防火墙规则
     *
     * @param id
     * @param bean
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "修改防火墙规则",  httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "防火墙规则", operation = "修改防火墙规则")
    @PutMapping("/{id}")
    public BocloudResult<FirewallRule> modify(@PathVariable(value = Common.ID) Long id,
                                              @RequestBody FirewallRule bean, @Value(Common.REQ_CONTEXT) RequestContext context) {
        bean.setId(id);
        return firewallRuleService.modify(bean, context.getTarget());
    }

    /**
     * 查询防火墙规则详情
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询防火墙规则详情",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "防火墙规则", operation = "查询防火墙规则详情")
    @GetMapping("/{id}")
    public BocloudResult<FirewallRule> detail(@PathVariable(value = Common.ID) Long id) {
        return firewallRuleService.detail(id);
    }

    /**
     * 删除防火墙规则
     *
     * @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,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return firewallRuleService.remove(id, context);
    }

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

    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询ip组",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "防火墙规则", operation = "查询ip组")
    @GetMapping("/ipgps")
    public BocloudResult listIpgps(@RequestParam Long vendorId ,@RequestParam(required = false) String query) {
        return firewallRuleService.getIpgps(vendorId, query);
    }

    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询ip组",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "防火墙规则", operation = "查询ip组")
    @GetMapping("/service/default")
    public BocloudResult listServiceDefault(@RequestParam Long vendorId ,@RequestParam(required = false) String querySrv) {
        return firewallRuleService.getServiceDefaults(vendorId, querySrv);
    }

    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询ip组",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "防火墙规则", operation = "查询ip组")
    @GetMapping("/service/custom")
    public BocloudResult listServiceCustom(@RequestParam Long vendorId ) {
        return firewallRuleService.getServiceCustom(vendorId);
    }
}
