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

import com.bocloud.cmp.model.IpInfoModel;
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.IpPool;
import com.bocloud.ims.interfaces.resource.IpPoolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/ip/pools")
@Api(value = "IP地址池", tags = "IP地址池")
public class IpPoolController {

	@Autowired
	private IpPoolService ipPoolService;

	/**
	 * 查询IP地址池列表
	 * 
	 * @param pager
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "查询IP地址池列表",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "IP地址池", operation = "查询IP地址池列表")
	@GetMapping
	public BocloudResult<GridBean<IpPool>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
		return ipPoolService.list(pager, context);
	}

	/**
	 * 通过condition方式查询IP地址池列表
	 * 
	 * @param condition
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "通过condition方式查询IP地址池列表",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "IP地址池", operation = "查询IP地址池列表")
	@GetMapping("/condition")
	public BocloudResult condition(@RequestParam(value = Common.CONDITION, required = false) String condition,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return ipPoolService.condition(condition, context);
	}

	/**
	 * 创建
	 *
	 * @param ipPool
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "创建IP地址池",  httpMethod = "POST")
	@Log(module = BoCloudService.IMS, target = "IP地址池", operation = "创建IP地址池")
	@PostMapping
	public BocloudResult<IpPool> create(@RequestBody IpPool ipPool, @Value(Common.REQ_CONTEXT) RequestContext context) {
		return ipPoolService.create(ipPool, context);
	}

	/**
	 * 修改
	 *
	 * @param id
	 * @param ipPool
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "修改IP地址池",  httpMethod = "PUT")
	@Log(module = BoCloudService.IMS, target = "IP地址池", operation = "修改IP地址池")
	@PutMapping("/{id}")
	public BocloudResult<IpPool> modify(@PathVariable(value = Common.ID) Long id, @RequestBody IpPool ipPool,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null == ipPool) {
			return ResultTools.formatErrResult();
		}
		ipPool.setId(id);
		return ipPoolService.modify(ipPool, context);
	}

	/**
	 * 删除IP地址池
	 *
	 * @param id
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "删除IP列表",  httpMethod = "DELETE")
	@Log(module = BoCloudService.IMS, target = "IP地址池", operation = "删除IP地址池")
	@DeleteMapping("/{id}")
	public BocloudResult remove(@PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return ipPoolService.remove(id, context);
	}

	/**
	 * 详情
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "查询IP地址池详情",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "IP地址池", operation = "查看IP地址池详情")
	@GetMapping("/{id}")
	public BocloudResult<IpPool> detail(@PathVariable(value = Common.ID) Long id) {
		return ipPoolService.detail(id);
	}

	/**
	 * 获取ip列表详情
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "获取IP地址池IP内列表详情",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "IP地址池", operation = "获取IP地址池IP内列表详情")
	@GetMapping("/{id}/ips")
	public BocloudResult<List<IpInfoModel>> ips(@PathVariable(value = Common.ID) Long id, @RequestParam(value = "cidr",required = false) String cidr) {
		return ipPoolService.ips(id,cidr);
	}
}
