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

import com.alibaba.fastjson.JSON;
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.ims.entity.resource.VolumePool;
import com.bocloud.ims.interfaces.resource.VolumePoolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
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;

/**
 * @author luyanchao
 * @since 2019/3/4 14:14
 */

@RestController
@Api(value = "存储池", tags = "存储池")
public class VolumePoolController {

	@Autowired
	private VolumePoolService volumePoolService;

	/**
	 * 存储池列表
	 * 
	 * @param pager
	 * @return
	 */
	@Log(module = BoCloudService.IMS, target = "存储池", operation = "查询")
	@RequestMapping(value = "/v1/storages/pools", method = { RequestMethod.GET })
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "查询",  httpMethod = "GET")
	public BocloudResult<GridBean<VolumePool>> list(Pager pager) {
		return volumePoolService.list(pager);
	}

	/**
	 * 根据存储查询池
	 *
	 * @param storageId
	 * @return
	 */
	@Log(module = BoCloudService.IMS, target = "存储池", operation = "根据存储设备查询")
	@RequestMapping(value = "/v1/storages/{id}/pools", method = { RequestMethod.GET })
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "根据存储设备查询",  httpMethod = "GET")
	public BocloudResult<List<VolumePool>> listByStorage(
			@PathVariable(value = Common.ID, required = true) long storageId) {
		return volumePoolService.listByStorage(storageId);
	}

	/**
	 * 创建
	 */
	@Log(module = BoCloudService.IMS, target = "存储池", operation = "创建")
	@RequestMapping(value = "/v1/storages/pools", method = { RequestMethod.POST })
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "创建",  httpMethod = "POST")
	public BocloudResult<String> create(@RequestBody VolumePool pool,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return volumePoolService.create(pool, context);
	}

	/**
	 * 修改
	 */
	@Log(module = BoCloudService.IMS, target = "存储池", operation = "修改")
	@RequestMapping(value = "/v1/storages/pools/{id}", method = { RequestMethod.PUT })
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "修改",  httpMethod = "PUT")
	public BocloudResult<String> modify(@PathVariable(value = Common.ID) long poolId, @RequestBody VolumePool pool,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		pool.setId(poolId);
		return volumePoolService.modify(pool, context);
	}

	/**
	 * 删除
	 */
	@Log(module = BoCloudService.IMS, target = "存储池", operation = "删除")
	@RequestMapping(value = "/v1/storages/pools/{id}", method = { RequestMethod.DELETE })
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "删除",  httpMethod = "DELETE")
	public BocloudResult<String> remove(@PathVariable(value = Common.ID, required = true) long poolId,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		boolean force = false;
		if (StringUtils.isNotEmpty(params)) {
			JSONObject paramObject = JSON.parseObject(params);
			if (paramObject.containsKey("force")) {
				force = paramObject.getBooleanValue("force");
			}
		}
		return volumePoolService.remove(poolId, force, context);
	}

	/**
	 * 详情
	 */
	@Log(module = BoCloudService.IMS, target = "存储池", operation = "详情")
	@RequestMapping(value = "/v1/storages/pools/{id}", method = { RequestMethod.GET })
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "详情",  httpMethod = "GET")
	public BocloudResult<VolumePool> get(@PathVariable(value = Common.ID, required = true) long poolId,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return volumePoolService.detail(poolId);
	}

	/**
	 * 添加成员
	 */
	@Log(module = BoCloudService.IMS, target = "存储池", operation = "添加磁盘")
	@RequestMapping(value = "/v1/storages/pools/{id}/members", method = { RequestMethod.POST })
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "添加磁盘",  httpMethod = "POST")
	public BocloudResult<String> addMember(@PathVariable(value = Common.ID, required = true) long poolId,
			@RequestParam(value = Common.PARAMS, required = true) String params,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		List<Long> memberIds = JSON.parseArray(params, Long.class);
		return volumePoolService.addMember(poolId, memberIds, context);
	}

	/**
	 * 删除成员
	 */
	@Log(module = BoCloudService.IMS, target = "存储池", operation = "删除磁盘")
	@RequestMapping(value = "/v1/storages/pools/{id}/members", method = { RequestMethod.DELETE })
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "删除磁盘",  httpMethod = "DELETE")
	public BocloudResult<List<Long>> removeMember(@PathVariable(value = Common.ID, required = true) long poolId,
			@RequestParam(value = Common.PARAMS, required = true) String params,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		List<Long> memberIds = JSON.parseArray(params, Long.class);
		return volumePoolService.removeMember(poolId, memberIds, context);
	}

}
