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.ResultTools;
import com.bocloud.ims.entity.resource.NetworkCard;
import com.bocloud.ims.interfaces.resource.NetworkCardService;
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;

@RestController
@RequestMapping("/v1/nics")
@Api(value = "网卡", tags = "网卡")
public class NetworkCardController {

	@Autowired
	private NetworkCardService networkCardService;

	/**
	 * 查看网卡列表
	 * 
	 * @param pager
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "查询网卡列表",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "网卡", operation = "查看网卡列表")
	@GetMapping
	public BocloudResult<GridBean<NetworkCard>> list(Pager pager) {
		return networkCardService.list(pager);
	}

	/**
	 * 查询未绑定浮动ip的网卡列表
	 *
	 * @param pager
	 * @param context
	 * @return
	 */
	@ApiOperation(value = "查询未绑定浮动ip的网卡列表", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@Log(module = BoCloudService.IMS, target = "网卡", operation = "查询未绑定浮动ip的网卡列表")
	@GetMapping("/unBandList/{vendorId}")
	public BocloudResult unBandList(@PathVariable(value = Common.VENDORID) Long vendorId) {
		return networkCardService.unBandList(vendorId);
	}

	/**
	 * 查看网卡详情
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "查看网卡详情",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "网卡", operation = "查看网卡详情")
	@GetMapping("/{id}")
	public BocloudResult<NetworkCard> detail(@PathVariable(value = Common.ID) Long id) {
		return networkCardService.detail(id);
	}

	/**
	 * 创建网卡
	 *
	 * @param params
	 * @param user
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "创建网卡",  httpMethod = "POST")
	@Log(module = BoCloudService.IMS, target = "网卡", operation = "创建网卡")
	@PostMapping
	public BocloudResult create(@RequestBody NetworkCard networkCard,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return networkCardService.create(networkCard, context);
	}

	/**
	 * 删除网卡
	 *
	 * @param id
	 * @param user
	 * @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 networkCardService.remove(id, context);
	}

	/**
	 * 批量操作
	 *
	 * @param params
	 * @param user
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "批量删除操作",  httpMethod = "PATCH")
	@Log(module = BoCloudService.IMS, target = "vpc管理", operation = "批量操作")
	@DeleteMapping
	public BocloudResult batchRemove(@RequestBody List<Long> idArray,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return networkCardService.batchRemove(idArray, context);
	}

	/**
	 * 绑定主机
	 *
	 * @param id
	 * @param params
	 * @param user
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "网卡挂载",  httpMethod = "PATCH")
	@Log(module = BoCloudService.IMS, target = "网卡", operation = "绑定主机")
	@PatchMapping("/{id}/vms")
	public BocloudResult attachNetworkCard(@PathVariable(value = Common.ID) Long id,
			@RequestBody NetworkCard networkCardParam, @Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null == networkCardParam) {
			return ResultTools.formatErrResult();
		}
		networkCardParam.setId(id);
		return networkCardService.attachNetworkCard(networkCardParam, context);
	}

	/**
	 * 解绑主机
	 *
	 * @param params
	 * @param user
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "网卡卸载",  httpMethod = "DELETE")
	@Log(module = BoCloudService.IMS, target = "网卡", operation = "解绑主机")
	@DeleteMapping("/{id}/vms")
	public BocloudResult detachNetworkCard(@PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return networkCardService.detachNetworkCard(id, context);
	}

}
