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

import com.alibaba.fastjson.JSON;
import com.bocloud.common.annotations.Log;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.*;
import com.bocloud.common.utils.Common;
import com.bocloud.ims.entity.resource.Mdisk;
import com.bocloud.ims.interfaces.resource.MdiskService;
import com.bocloud.ims.interfaces.resource.MdiskVolumeService;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author luyanchao
 * @since 2019/6/12
 */
@RestController
@RequestMapping("/v1/storages/mdisks")
@Api(value = "磁盘阵列", tags = "磁盘阵列")
public class MdiskController {

	@Autowired
	private MdiskService mdiskService;
	@Autowired
	private MdiskVolumeService mdiskVolumeService;

	/**
	 * 查询光纤网信息列表
	 */
	@ApiOperation(tags={"CMC","CSC"},value = "查询光纤网信息列表",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "磁盘阵列", operation = "查询磁盘阵列列表")
	@RequestMapping(method = { RequestMethod.GET })
	public BocloudResult<GridBean<Mdisk>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {

		// 分页查询mdisk
		return mdiskService.list(pager, context);
	}

	/**
	 * 根据action查询光纤网信息列表
	 */
	@ApiOperation(tags={"CMC","CSC"},value = "根据action查询光纤网信息列表",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "磁盘阵列", operation = "查询磁盘阵列列表")
	@GetMapping("/condition")
	public BocloudResult<GridBean<Mdisk>> condition(Pager pager, @RequestParam(value = Common.ACTION, required = false) String action,
							  @Value(Common.REQ_CONTEXT) RequestContext context) {
		if (StringUtils.isEmpty(action) || "common".equals(action)) {
			// 分页查询mdisk
			return mdiskService.list(pager, context);
		} else if ("listByVolume".equals(action)) {
			// 按卷查询mdisk
			Map<String, Object> paramMap = new HashMap<>(8);
			if (pager.getParams() != null && pager.getParams().size() > 0) {
				for (Param param : pager.getParams()) {
					paramMap.putAll(param.getParam());
				}
			}
			if (!paramMap.containsKey("volumeId")) {
				return new BocloudResult(false, "缺少参volumeId");
			}
			long volumeId = Long.parseLong(paramMap.get("volumeId").toString());
			return mdiskVolumeService.listMdiskByVolume(pager.getPage(), pager.getRows(), volumeId, paramMap,
					pager.getSorter());
		} else {
			return new BocloudResult(false, "查询类型不支持");
		}
	}

	/**
	 * 创建
	 */
	@ApiOperation(tags={"CMC","CSC"},value = "创建MDisk",  httpMethod = "POST")
	@Log(module = BoCloudService.IMS, target = "磁盘阵列", operation = "创建MDisk")
	@RequestMapping(method = { RequestMethod.POST })
	public BocloudResult create(@RequestParam(value = Common.PARAMS, required = true) String params,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		Mdisk mdisk = JSON.parseObject(params, Mdisk.class);
		return mdiskService.create(mdisk, context);
	}

	/**
	 * 详情
	 */
	@ApiOperation(tags={"CMC","CSC"},value = "查看磁盘阵列详情",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "磁盘阵列", operation = "查看磁盘阵列详情")
	@RequestMapping(value = "/{id}", method = { RequestMethod.GET })
	public BocloudResult<Mdisk> detail(@PathVariable(value = Common.ID, required = true) Long mdiskId) {
		return mdiskService.detail(mdiskId);
	}

	/**
	 * 华为存储查看raid策略
	 */
	@ApiOperation(tags={"CMC","CSC"},value = "查看raid策略",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "磁盘阵列", operation = "查看raid策略")
	@RequestMapping(value = "/{id}/raid", method = { RequestMethod.GET })
	public BocloudResult<List<SimpleBean>> getRaid(@PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return mdiskService.getRaid(id, context);
	}

	/**
	 * 华为存储根据raid名称查看raid策略
	 */
	@ApiOperation(tags={"CMC","CSC"},value = "根据raid名称查看raid策略",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "磁盘阵列", operation = "根据raid名称查看raid策略")
	@RequestMapping(value = "/{id}/raid/tactics", method = { RequestMethod.GET })
	public BocloudResult<List<SimpleBean>> getRaidByRaidName(@RequestParam(value = Common.PARAMS, required = true) String params,
			@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_CONTEXT) RequestContext context) {
		return mdiskService.getRaidByRaidName(id, params, context);
	}

	/**
	 * 根据raid策略获取创建池可用的容量
	 */
	@ApiOperation(tags={"CMC","CSC"},value = "根据raid策略获取创建池可用的容量",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "磁盘阵列", operation = "根据raid策略获取创建池可用的容量")
	@RequestMapping(value = "/{id}/pool/capacity", method = { RequestMethod.GET })
	public BocloudResult<Mdisk> getCapacityForCreate(@PathVariable(value = Common.ID) Long id,
			@RequestParam(value = Common.PARAMS, required = true) String params,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return mdiskService.getCapacityForCreatePool(id, params);
	}

}
