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

import com.bocloud.cmp.model.ElasticIpGenericParam;
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.ElasticIp;
import com.bocloud.ims.interfaces.resource.ElasticIpService;
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.web.bind.annotation.*;

@RestController
@RequestMapping("/v1/elasticips")
@Api(value = "弹性IP", tags = "弹性IP")
public class ElasticIpController {

	@Autowired
	private ElasticIpService elasticIpService;

	@ApiOperation(tags = { "CMC", "CSC" }, value = "查询弹性ip列表", httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "弹性IP", operation = "查询弹性ip列表")
	@GetMapping
	public BocloudResult<GridBean<ElasticIp>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
		return elasticIpService.list(pager, context);
	}

	/**
	 * 分配IP
	 *
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "分配IP", httpMethod = "POST")
	@Log(module = BoCloudService.IMS, target = "弹性IP", operation = "分配IP")
	@PostMapping
	public BocloudResult<ElasticIp> allocate(@RequestBody ElasticIp elasticIp,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return elasticIpService.allocate(elasticIp, context);
	}

	/**
	 * 释放IP
	 *
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "释放IP", httpMethod = "DELETE")
	@Log(module = BoCloudService.IMS, target = "弹性IP", operation = "释放IP")
	@DeleteMapping("/{id}")
	public BocloudResult release(@PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return elasticIpService.release(id, context);
	}

	/**
	 * 弹性IP操作
	 *
	 * @param id
	 * @param action
	 * @param params
	 * @param context
	 * @param request
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "弹性IP操作", httpMethod = "PATCH")
	@PatchMapping(value = "/{id}")
	@Log(module = BoCloudService.IMS, target = "弹性IP", operation = "弹性IP操作")
	public BocloudResult action(@PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context,
			@RequestBody ElasticIpGenericParam elasticIpGenericParam) {
		if (null == elasticIpGenericParam) {
			return new BocloudResult(false, "参数格式错误");
		}
		String action = elasticIpGenericParam.getAction();
		if ("association".equals(action)) {
			elasticIpGenericParam.setId(id);
			return elasticIpService.associate(elasticIpGenericParam, context);
		} else if ("disassociation".equals(action)) {
			return elasticIpService.disassociate(id, context);
		}
		return new BocloudResult(false, "暂不支持该类操作");
	}

	/**
	 * 更新EIP
	 *
	 * @param id
	 * @param elasticIp
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "更新EIP", httpMethod = "PUT")
	@Log(module = BoCloudService.IMS, target = "弹性IP", operation = "更新EIP")
	@PutMapping(value = "/{id}")
	public BocloudResult<ElasticIp> modify(@PathVariable(value = Common.ID) Long id, @RequestBody ElasticIp elasticIp,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		elasticIp.setId(id);
		return elasticIpService.modify(elasticIp, context);
	}

	/**
	 * 查看ip明细
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "查看ip明细", httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "查看ip明细", operation = "查看ip明细")
	@GetMapping("/{id}")
	public BocloudResult detail(@PathVariable(value = Common.ID) Long id) {
		return elasticIpService.detail(id);
	}

	/***
	 * 查询平台下未关联EIP
	 *
	 * @param params
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = {"CMC", "CSC"}, value = "查询未关联EIP", httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "弹性IP", operation = "查询未关联EIP")
	@GetMapping("/frees")
	public BocloudResult listFreeEips(@RequestParam(value = Common.VENDORID, required = false) Long vendorId,
									  @RequestParam(value = Common.REGIONID, required = false) String regionId, @Value(Common.REQ_CONTEXT) RequestContext context) {
		return elasticIpService.listFrees(vendorId, regionId, context);
	}

	/***
	 * 查询nat网关可使用的EIP
	 *
	 * @param params
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "查询nat网关可使用的EIP", httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "弹性IP", operation = "查询nat网关可使用的EIP")
	@GetMapping("/natgateway")
	public BocloudResult listAvailableEipsForNatGateway(
			@RequestParam(value = Common.PARAMS, required = false) String params) {

		return elasticIpService.listAvailableEipsForNatGateway(params);

	}

	/**
	 * 更新EIP
	 *
	 * @param id
	 * @param elasticIp
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "更新EIP带宽", httpMethod = "PUT")
	@Log(module = BoCloudService.IMS, target = "弹性IP", operation = "更新EIP带宽")
	@PutMapping(value = "/bandwidth/{id}")
	public BocloudResult<ElasticIp> modifyBandwidth(@PathVariable(value = Common.ID) Long id,
			@RequestBody ElasticIp elasticIp, @Value(Common.REQ_CONTEXT) RequestContext context) {
		elasticIp.setId(id);
		return elasticIpService.modifyBandwidth(elasticIp, context);
	}
}
