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

import com.bocloud.cmp.model.InternetGatewayParam;
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.ResultTools;
import com.bocloud.ims.entity.resource.InternetGateway;
import com.bocloud.ims.interfaces.resource.InternetGatewayService;
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.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/v1/internetgateway")
@Api(value = "互联网网关", tags = "互联网网关")
public class InternetGatewayController {

	@Autowired
	private InternetGatewayService internetGatewayService;

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

	/**
	 * 创建互联网网关
	 *
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "创建互联网网关",  httpMethod = "POST")
	@Log(module = BoCloudService.IMS, target = "互联网网关", operation = "创建互联网网关")
	@PostMapping
	public BocloudResult<InternetGateway> create(@RequestBody InternetGateway internetGateway,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null != internetGateway) {
			return internetGatewayService.create(internetGateway, context);
		}
		return ResultTools.formatErrResult();
	}

	/**
	 * 删除互联网网关
	 *
	 * @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 internetGatewayService.remove(id, context);
	}

	/**
	 * 互联网网关操作
	 *
	 * @param id
	 * @param context
	 * @param request
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "互联网网关操作",  httpMethod = "PATCH")
	@PatchMapping(value = "/{id}")
	@Log(module = BoCloudService.IMS, target = "互联网网关", operation = "互联网网关操作")
	public BocloudResult action(@PathVariable(value = Common.ID) Long id,
								@RequestBody InternetGatewayParam internetGatewayParam,
			@Value(Common.REQ_CONTEXT) RequestContext context, HttpServletRequest request) {
		String action = internetGatewayParam.getAction();
		internetGatewayParam.setId(id);
		if ("attach".equals(action)) {
			return internetGatewayService.attach(internetGatewayParam, context);
		} else if ("detach".equals(action)) {
			return internetGatewayService.detach(internetGatewayParam, context);
		}
		return new BocloudResult(false, "暂不支持该类操作");
	}

	/**
	 * 更新网关
	 *
	 * @param id
	 * @param gateway
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "互联网网关",  httpMethod = "PUT")
	@Log(module = BoCloudService.IMS, target = "互联网网关", operation = "更新互联网网关操作")
	@PutMapping(value = "/{id}")
	public BocloudResult<InternetGateway> modify(@PathVariable(value = Common.ID) Long id,
			@RequestBody InternetGateway gateway, @Value(Common.REQ_CONTEXT) RequestContext context) {
		gateway.setId(id);
		return internetGatewayService.modify(gateway, context);
	}
}
