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

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.NatGateway;
import com.bocloud.ims.interfaces.resource.NatGatewayService;
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;

@RestController
@RequestMapping("/v1/natgateway")
@Api(value = "nat网关", tags = "nat网关")
public class NatGatewayController {

	@Autowired
	private NatGatewayService service;

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

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

	}

	@ApiOperation(tags = { "CMC", "CSC" }, value = "nat网关", httpMethod = "DELETE")
	@Log(module = BoCloudService.IMS, target = "nat网关", operation = "删除nat网关")
	@DeleteMapping("/{id}")
	public BocloudResult remove(@PathVariable(value = Common.ID) Long id,
			@RequestParam(value = "force", required = false) Boolean force,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null != force) {
			return service.remove(id, force, context);
		} else {
			return service.remove(id, context);
		}

	}

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

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

	@ApiOperation(tags = { "CMC", "CSC" }, value = "查询可绑定网关", httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "NAT网关", operation = "查询可绑定网关")
	@GetMapping("/availableGateway")
	public BocloudResult<List<NatGateway>> listAvailableGateway(
			@RequestParam(value = Common.VENDORID, required = true) Long vendorId,
			@RequestParam(value = Common.REGIONID, required = true) String regionId,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return service.listAvailableGateway(vendorId, regionId);
	}

	@ApiOperation(tags = { "CMC", "CSC" }, value = "查询已绑定的公网ip", httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "NAT网关", operation = "查询已绑定的公网ip")
	@GetMapping("/listBingElasticIp")
	public BocloudResult<List<String>> listBingElasticIp(
			@RequestParam(value = "natGatewayId", required = true) Long natGatewayId,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return service.listBindElasticIp(natGatewayId);
	}

	@ApiOperation(tags = { "CMC", "CSC" }, value = "查询可用的私网ip", httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "NAT网关", operation = "查询可用的私网ip")
	@GetMapping("/listAvailablePrivateIp")
	public BocloudResult<List<Map<String, Object>>> listAvailablePrivateIp(
			@RequestParam(value = "natGatewayId", required = true) Long natGatewayId,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return service.listAvailablePrivateIp(natGatewayId);
	}

	@ApiOperation(tags = { "CMC", "CSC" }, value = "查询网关列表", httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "NAT网关", operation = "查询网关列表")
	@GetMapping("/list")
	public BocloudResult<List<NatGateway>> list(@RequestParam(value = Common.VENDORID, required = true) Long vendorId,
			@RequestParam(value = Common.REGIONID, required = true) String regionId,
			@RequestParam(value = "vpcId", required = true) Long vpcId,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return service.list(vendorId, regionId, vpcId);
	}

	@ApiOperation(tags = { "CMC", "CSC" }, value = "查询路由可用的网关", httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "NAT网关", operation = "查询可绑定网关")
	@GetMapping("/listAvailableForRoute")
	public BocloudResult<List<NatGateway>> listAvailableForRoute(
			@RequestParam(value = Common.VENDORID, required = true) Long vendorId,
			@RequestParam(value = "routeTableId", required = true) Long routeTableId,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return service.listAvailableGateway(vendorId, routeTableId);
	}

}
