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

import com.bocloud.cmp.model.FirewallModel;
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.Firewall;
import com.bocloud.ims.interfaces.resource.FirewallService;
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;
import java.util.Map;

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

	@Autowired
	private FirewallService firewallService;

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

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

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

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

	/**
	 * 防火墙关联路由
	 *
	 * @param id
	 * @param firewallModel
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "关联路由", httpMethod = "POST")
	@Log(module = BoCloudService.IMS, target = "防火墙", operation = "防火墙关联路由")
	@PostMapping("/{id}/routers")
	public BocloudResult relevanceRouter(@PathVariable(value = Common.ID) Long id,
			@RequestBody FirewallModel firewallModel, @Value(Common.REQ_CONTEXT) RequestContext context) {
		return firewallService.relevanceRouter(id, firewallModel, context);
	}

	/**
	 * 查询路由
	 *
	 * @param id
	 * @param vendorId
	 * @param tenantUuid
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "查询路由信息", httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "防火墙", operation = "查询路由")
	@GetMapping("/{id}/routers")
	public BocloudResult<Map<String, Object>> listRouter(@PathVariable(value = Common.ID) Long id,
			@RequestParam(value = Common.VENDORID) Long vendorId,
			@RequestParam(value = "tenantUuid", required = false) String tenantUuid,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return firewallService.listRouter(id, vendorId, tenantUuid, context.getTenant());
	}

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