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

import com.bocloud.common.annotations.Log;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.RequestContext;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.ResultTools;
import com.bocloud.ims.entity.resource.Region;
import com.bocloud.ims.entity.resource.Zone;
import com.bocloud.ims.interfaces.resource.RegionService;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 区域控制器(子区域为zone)
 *
 * @author dongkai
 */
@RestController
@RequestMapping("/v1/regions")
@Api(value = "区域管理", tags = "区域管理")
public class RegionController {

	@Autowired
	private RegionService regionService;

	/**
	 * 查询
	 *
	 * @param vendorId
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "查看区域列表", httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "区域", operation = "查询区域列表")
	@GetMapping
	public BocloudResult<List<Region>> list(@RequestParam(value = Common.VENDORID, required = true) Long vendorId) {
		return regionService.list(vendorId);
	}

	/**
	 * 查询
	 *
	 * @param condition
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "查看区域列表", httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "区域", operation = "查询区域列表")
	@GetMapping("/condition")
	public BocloudResult<List<Region>> condition(@RequestParam(Common.CONDITION) String condition,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return regionService.condition(condition, context);
	}

	/**
	 * 查询区域下的可用区
	 * 
	 * @param region
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "查询区域下子区域", httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "区域", operation = "查询区域下的可用区")
	@GetMapping("/zones")
	public BocloudResult<List<Zone>> listZone(@RequestParam(value = Common.VENDORID, required = false) Long vendorId,
			@RequestParam(value = Common.REGIONID, required = false) String regionId,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return regionService.listZone(vendorId, regionId);
	}

	/**
	 * 查询当前区域下可用区及备可用区
	 *
	 * @param params
	 * @param context
	 * @return
	 */
	@ApiOperation(value = "查询当前区域下可用区及备可用区", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@Log(module = BoCloudService.IMS, target = "区域", operation = "查询当前区域下可用区及备可用区")
	@GetMapping("/{id}/zones")
	public BocloudResult zones(@PathVariable(value = Common.ID) Long id,
			@RequestParam(value = Common.TYPE, required = false) String type,
			@RequestParam(value = Common.VENDORID, required = false) Long vendorId,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return regionService.zones(id, vendorId, type);
	}
}
