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

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.FirewallPolicy;
import com.bocloud.ims.entity.resource.FirewallRule;
import com.bocloud.ims.interfaces.resource.FirewallPolicyService;
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;

@RestController
@RequestMapping("/v1/firewalls/policies")
@Api(value = "防火墙策略", tags = "防火墙策略")
public class FirewallPolicyController {

	@Autowired
	private FirewallPolicyService firewallPolicyService;

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

	/**
	 * 查询防火墙策略列表
	 *
	 * @param condition
	 * @param context
	 * @return
	 */
	@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 firewallPolicyService.condition(condition, context);
	}

	/**
	 * 创建防火墙策略
	 *
	 * @param policy
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "创建防火墙策略", httpMethod = "POST")
	@Log(module = BoCloudService.IMS, target = "防火墙策略", operation = "创建防火墙策略")
	@PostMapping
	public BocloudResult<FirewallPolicy> create(@RequestBody FirewallPolicy policy,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return firewallPolicyService.create(policy, 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<FirewallPolicy> modify(@PathVariable(value = Common.ID) Long id,
			@RequestBody FirewallPolicy bean, @Value(Common.REQ_CONTEXT) RequestContext context) {
		bean.setId(id);
		return firewallPolicyService.modify(bean, context.getTarget());
	}

	/**
	 * 查询防火墙策略详情
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "查询防火墙策略详情", httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "防火墙策略", operation = "查询防火墙策略详情")
	@GetMapping("/{id}")
	public BocloudResult<FirewallPolicy> detail(@PathVariable(value = Common.ID) Long id) {
		return firewallPolicyService.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 firewallPolicyService.remove(id, context);
	}

	/**
	 * 插入规则
	 *
	 * @param id
	 * @param bean
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "插入规则", httpMethod = "POST")
	@Log(module = BoCloudService.IMS, target = "防火墙策略", operation = "插入规则")
	@PostMapping("/{id}/rules")
	public BocloudResult insertRule(@PathVariable(value = Common.ID) Long id, @RequestBody FirewallPolicy bean,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		bean.setId(id);
		return firewallPolicyService.insertRule(bean, context.getTarget());
	}

	/**
	 * 移除规则
	 *
	 * @param id
	 * @param bean
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "移除规则", httpMethod = "DELETE")
	@Log(module = BoCloudService.IMS, target = "防火墙策略", operation = "移除规则")
	@DeleteMapping("/{id}/rules")
	public BocloudResult removeRule(@PathVariable(value = Common.ID) Long id, @RequestBody FirewallPolicy bean,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		bean.setId(id);
		return firewallPolicyService.removeRule(bean, context.getTarget());
	}

	/**
	 * 查询策略绑定的规则
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "查询策略绑定的规则", httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "防火墙策略", operation = "查询策略绑定的规则")
	@GetMapping("/{id}/rules")
	public BocloudResult<List<FirewallRule>> listPolicyRule(@PathVariable(Common.ID) Long id) {
		return firewallPolicyService.listRuleByPolicyId(id);
	}

	/**
	 * 批量删除防火墙策略
	 *
	 * @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 firewallPolicyService.batchRemove(idArray, context);
	}
}
