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

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.common.utils.JSONTools;
import com.bocloud.common.utils.ResultTools;
import com.bocloud.ims.entity.resource.ElasticIp;
import com.bocloud.ims.entity.resource.NatGatewayDnatRule;
import com.bocloud.ims.entity.resource.Port;
import com.bocloud.ims.interfaces.resource.NatGatewayDnatRuleService;

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/natgateway/dnat/rule")
@Api(value = "nat网关dnat规则", tags = "nat网关dnat规则")
public class NatGatewayDnatRuleController {

	@Autowired
	private NatGatewayDnatRuleService service;

	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "查询nat网关dnat规则列表",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "nat网关dnat规则", operation = "查询nat网关dnat规则列表")
	@GetMapping
	public BocloudResult<GridBean<NatGatewayDnatRule>> list(Pager pager,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return service.list(pager, context);
	}

	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "创建nat网关dnat规则",  httpMethod = "POST")
	@Log(module = BoCloudService.IMS, target = "nat网关dnat规则", operation = "创建nat网关dnat规则")
	@PostMapping
	public BocloudResult create(@RequestBody NatGatewayDnatRule dnatRule,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return service.create(dnatRule, context);

	}

	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "nat网关dnat规则",  httpMethod = "DELETE")
	@Log(module = BoCloudService.IMS, target = "nat网关dnat规则", operation = "删除nat网关dnat规则")
	@DeleteMapping("/{id}")
	public BocloudResult remove(@PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return service.remove(id, context);
	}

	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "nat网关dnat规则",  httpMethod = "PUT")
	@Log(module = BoCloudService.IMS, target = "nat网关dnat规则", operation = "更新nat网关dnat规则")
	@PutMapping(value = "/{id}")
	public BocloudResult modify(@PathVariable(value = Common.ID) Long id, @RequestBody NatGatewayDnatRule dnatRule,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null != dnatRule) {
			if (dnatRule.getId() == null) {
				dnatRule.setId(id);
			}
			return service.modify(dnatRule, context);
		} else {
			return ResultTools.formatErrResult();
		}
	}

	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "查看nat网关dnat规则",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "nat网关dnat规则", operation = "查看nat网关dnat规则")
	@GetMapping("/{id}")
	public BocloudResult<NatGatewayDnatRule> detail(@PathVariable(value = Common.ID) Long id) {
		return service.detail(id);
	}

	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "查看nat网关dnat规则全部端口可使用的eip",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "nat网关dnat规则", operation = "查看nat网关dnat规则全部端口可使用的eip")
	@GetMapping("/eip")
	public BocloudResult<List<ElasticIp>> listAvailableEipsForDnat(@RequestParam(value = Common.PARAMS, required = false) String params,
																   @Value(Common.REQ_CONTEXT) RequestContext context) {

		JSONObject object = JSONTools.isJSONObj(params);
		if (null != object) {

			return service.listAvailableEipsForDnat(object);
		}
		return ResultTools.formatErrResult();
	}

	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "查看nat网关dnat规则可用的虚拟机端口",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "nat网关dnat规则", operation = "查看nat网关dnat规则可用的虚拟机端口")
	@GetMapping("/{vmId}/port")
	public BocloudResult<List<Port>> listAvailablePortByVmId(@PathVariable(value = "vmId") Long id,
															 @Value(Common.REQ_CONTEXT) RequestContext context) {
		return service.listAvailablePortByVmId(id);
	}

	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "查询绑定的私网ip",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "nat网关dnat规则", operation = "查询绑定的私网ip")
	@GetMapping("/listBindPrivateIp")
	public BocloudResult<List<String>> listBindPrivateIp(@RequestParam(value = Common.PARAMS, required = true) String params,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		JSONObject object = JSONTools.isJSONObj(params);
		if (null != object) {
			return service.listBindPrivateIps(object.getLong("natGatewayId"));
		}
		return ResultTools.formatErrResult();
	}

}
