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

import com.alibaba.fastjson.JSONArray;
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.JSONTools;
import com.bocloud.common.utils.ResultTools;
import com.bocloud.ims.entity.resource.LBAddBean;
import com.bocloud.ims.entity.resource.LoadBalancer;
import com.bocloud.ims.interfaces.resource.LoadBalancerService;
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/lbs")
@Api(value = "负载均衡器 ", tags = "负载均衡器")
public class LoadBalancerController {

	@Autowired
	private LoadBalancerService loadBalancerService;

	/**
	 * 查看负载均衡器列表
	 *
	 * @param pager
	 * @param context
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "查看负载均衡器列表", httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "负载均衡器", operation = "查看负载均衡器列表")
	@GetMapping
	public BocloudResult<GridBean<LoadBalancer>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context)
			throws Exception {
		return loadBalancerService.list(pager, context);
	}

	/**
	 * 创建负载均衡器
	 *
	 * @param bean
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "创建负载均衡器", httpMethod = "POST")
	@Log(module = BoCloudService.IMS, target = "负载均衡器", operation = "创建负载均衡器")
	@PostMapping
	public BocloudResult create(@RequestBody LBAddBean bean, @Value(Common.REQ_CONTEXT) RequestContext context) {
		return loadBalancerService.create(bean, context);
	}

	/**
	 * 修改负载均衡器
	 *
	 * @param id
	 * @param params
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "修改负载均衡器", httpMethod = "PUT")
	@Log(module = BoCloudService.IMS, target = "负载均衡器", operation = "修改负载均衡器")
	@PutMapping("/{id}")
	public BocloudResult<LoadBalancer> modify(@PathVariable(value = Common.ID) Long id, @RequestBody LBAddBean bean,
			@Value(Common.REQ_CONTEXT) RequestContext context) {

		if (null != bean) {
			return loadBalancerService.modify(id, bean, context.getTarget());
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 查看负载均衡器详情
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "查看负载均衡器详情", httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "负载均衡器", operation = "查看负载均衡器详情")
	@GetMapping("/{id}")
	public BocloudResult<LoadBalancer> detail(@PathVariable(value = Common.ID) Long id) {
		return loadBalancerService.detail(id);
	}

	/**
	 * 删除负载均衡器
	 *
	 * @param id
	 * @param context
	 * @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 loadBalancerService.remove(id, context);
	}

	/**
	 * 批量删除负载均衡器
	 *
	 * @param params
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "负载均衡器批量删除", httpMethod = "DELETE")
	@Log(module = BoCloudService.IMS, target = "负载均衡器", operation = "批量删除负载均衡器")
	@DeleteMapping
	public BocloudResult batchRemove(@RequestBody List<Long> idArray,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return loadBalancerService.batchRemove(idArray, context);
	}

	@GetMapping("/getElbByParams")
	@ApiOperation(value = "查询ELB信息", response = JSONObject.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@Log(module = BoCloudService.IMS, target = "负载均衡器", operation = "查询ELB信息")
	public JSONArray getElbByParams(@RequestParam(value = "envCode") String envCode,
			@RequestParam("appSystemCode") String appSystemCode) {
		return loadBalancerService.getElbByParams(envCode, appSystemCode);
	}

	@ApiOperation(value = "负载均衡器标签", response = BocloudResult.class, httpMethod = "PATCH", produces = MediaType.APPLICATION_JSON_VALUE)
	@Log(module = BoCloudService.IMS, target = "负载均衡器", operation = "负载均衡器标签")
	@PatchMapping(value = "/{id}")
	public BocloudResult labels(@PathVariable(value = Common.ID) Long id,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		JSONObject object = JSONTools.isJSONObj(params);
		if (null != object && object.containsKey("labels")) {
			String labels = object.getString("labels");
			return loadBalancerService.labels(id, labels, context);
		}
		return ResultTools.formatErrResult();

	}

	@ApiOperation(value = "查询可绑定负载", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@Log(module = BoCloudService.IMS, target = "负载均衡器", operation = "查询可绑定负载")
	@GetMapping("/availableLbs")
	public BocloudResult listAvailableLbs(@RequestParam(value = Common.VENDORID, required = true) Long vendorId,
			@RequestParam(value = Common.REGIONID, required = true) String regionId,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return loadBalancerService.listAvailableLbs(vendorId, regionId, context);
	}
}
