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

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.*;

/**
 * pool服务器组
 *
 * @author lyy
 * @version 1.0
 * @date 2021/1/5 10:28
 */
@Api(value = "pool服务器组", tags = "pool服务器组")
@RestController
@RequestMapping("/v1/lb/pools")
public class LbPoolController {
	@Autowired
	private LbPoolService lbPoolService;

	/**
	 * 查看pool服务器组列表
	 *
	 * @param pager
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "查看pool服务器组列表", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@Log(module = BoCloudService.IMS, target = "pool服务器组", operation = "查看pool服务器组列表")
	@GetMapping
	public BocloudResult list(Pager pager) throws Exception {
		return lbPoolService.list(pager.getPage(), pager.getRows(), pager.getParams(), pager.getSorter(),
				pager.getSimple());
	}

	/**
	 * 创建pool服务器组
	 *
	 * @param params
	 * @param context
	 * @return
	 */
	@ApiOperation(value = "创建pool服务器组", response = BocloudResult.class, httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	@Log(module = BoCloudService.IMS, target = "pool服务器组", operation = "创建pool服务器组")
	@PostMapping
	public BocloudResult create(@RequestBody LbPool lbPool, @Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null != lbPool) {
			return lbPoolService.create(lbPool, context, lbPool.getVendorId());
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 修改
	 *
	 * @param params
	 * @param context
	 * @return
	 */
	@ApiOperation(value = "修改pool服务器组", response = BocloudResult.class, httpMethod = "PUT", produces = MediaType.APPLICATION_JSON_VALUE)
	@Log(module = BoCloudService.IMS, target = "pool服务器组", operation = "修改pool服务器组")
	@PutMapping("/{id}")
	public BocloudResult modify(@PathVariable(value = Common.ID) Long id, @RequestBody LbPool pool,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null != pool) {
			pool.setId(id);
			return lbPoolService.modify(pool, context, pool.getVendorId());
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 详情
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "查看pool服务器组详情", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@Log(module = BoCloudService.IMS, target = "pool服务器组", operation = "查看pool服务器组详情")
	@GetMapping("/{id}")
	public BocloudResult detail(@PathVariable(value = Common.ID) Long id) {
		return lbPoolService.detail(id);
	}

	/**
	 * 删除
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "删除pool服务器组", response = BocloudResult.class, httpMethod = "DELETE", produces = MediaType.APPLICATION_JSON_VALUE)
	@Log(module = BoCloudService.IMS, target = "pool服务器组", operation = "删除pool服务器组")
	@DeleteMapping("/{id}")
	public BocloudResult remove(@PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return lbPoolService.remove(id, context.getTarget());
	}

	@ApiOperation(value = "获取可转发服务器组列表", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@Log(module = BoCloudService.IMS, target = "pool服务器组", operation = "获取可转发服务器组列表")
	@GetMapping("/listforPolicy")
	public BocloudResult listforPolicy(@RequestParam(value = "lbId") Long lbId
			, @RequestParam(value = "listenerId") Long listenerId) {
		return lbPoolService.listforPolicy(lbId, listenerId);
	}

	@ApiOperation(value = "获取可配置监听器默认服务器组列表", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@Log(module = BoCloudService.IMS, target = "pool服务器组", operation = "获取可配置监听器默认服务器组列表")
	@GetMapping("/listforListener")
	public BocloudResult listforListener(@RequestParam(value = "lbId") Long lbId
			, @RequestParam(value = "listenerId") Long listenerId) {
		return lbPoolService.listforListener(lbId, listenerId);
	}
}
