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.*;
import com.bocloud.common.utils.Common;
import com.bocloud.ims.common.Constant;
import com.bocloud.ims.entity.resource.FlashCopy;
import com.bocloud.ims.interfaces.resource.FlashCopyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author luyanchao
 * @since 2019/6/12
 */
@RestController
@RequestMapping("/v1")
public class FlashCopyController {

	@Autowired
	private FlashCopyService flashCopyService;

	/**
	 * 列表
	 */
	@Log(module = BoCloudService.IMS, target = "FlashCopy", operation = "查询FlashCopy列表")
	@RequestMapping(value = "/storages/flashcopies", method = { RequestMethod.GET })
	public BocloudResult<GridBean<FlashCopy>> list(Pager pager) {
		return flashCopyService.list(pager, null);
	}

	/**
	 * 根据action查询FlashCopy列表
	 */
	@Log(module = BoCloudService.IMS, target = "FlashCopy", operation = "根据action查询FlashCopy列表")
	@RequestMapping(value = "/storages/flashcopies/action", method = { RequestMethod.GET })
	public BocloudResult<GridBean<FlashCopy>> action(Pager pager, @RequestParam(value = "action", required = false) String action) {
		if ("pageByVolumeId".equals(action)) {
			Map<String, Object> paramMap = new HashMap<>(8);
			pager.getParams().stream().map(Param::getParam).forEach(paramMap::putAll);
			Long volumeId = Optional.ofNullable(paramMap.get("volumeId")).map(Object::toString).map(Long::valueOf).orElse(null);
			pager.getParams().forEach(param -> {
				param.getParam().remove("volumeId");
			});
			return flashCopyService.list(pager, volumeId);
		}
		return new BocloudResult(false, "查询失败");
	}

	/**
	 * 创建
	 */
	@Log(module = BoCloudService.IMS, target = "FlashCopy", operation = "创建FlashCopy")
	@RequestMapping(value = "/storages/flashcopies", method = { RequestMethod.POST })
	public BocloudResult create(@RequestParam(value = Common.PARAMS) String params,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		FlashCopy flashCopy = JSON.parseObject(params, FlashCopy.class);
		return flashCopyService.create(flashCopy, context);
	}

	/**
	 * 操作，支持启动、停止
	 */
	@Log(module = BoCloudService.IMS, target = "FlashCopy", operation = "操作FlashCopy")
	@RequestMapping(value = "/storages/flashcopies/{id}", method = { RequestMethod.PATCH })
	public BocloudResult operate(@PathVariable(value = Common.ID) Long flashCopyId,
			@RequestParam(value = Constant.ACTION) String action,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		if ("start".equals(action)) {
			return flashCopyService.start(flashCopyId, context);
		} else if ("stop".equals(action)) {
			return flashCopyService.stop(flashCopyId, context);
		} else if ("move_in".equals(action)) {
			return flashCopyService.moveIn(flashCopyId, JSONObject.parseObject(params).getLongValue("groupId"),
					context);
		} else if ("move_out".equals(action)) {
			return flashCopyService.moveOut(flashCopyId, context);
		} else {
			return new BocloudResult(false, "不支持的操作类型");
		}
	}

	/**
	 * 删除
	 */
	@Log(module = BoCloudService.IMS, target = "FlashCopy", operation = "删除FlashCopy")
	@RequestMapping(value = "/storages/flashcopies/{id}", method = { RequestMethod.DELETE })
	public BocloudResult remove(@PathVariable(value = Common.ID) Long flashCopyId,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		Boolean force = Optional.ofNullable(params).map(JSON::parseObject).map(o -> o.getBoolean("force")).orElse(false);
		return flashCopyService.remove(flashCopyId, force, context);
	}

	/**
	 * 详情
	 */
	@Log(module = BoCloudService.IMS, target = "FlashCopy", operation = "查看FlashCopy详情")
	@RequestMapping(value = "/storages/flashcopies/{id}", method = { RequestMethod.GET })
	public BocloudResult<FlashCopy> detail(@PathVariable(value = Common.ID) Long flashCopyId) {
		return flashCopyService.detail(flashCopyId);
	}

	/**
	 * 根据原卷查询可以作为FlashCopy目标卷的卷
	 */
	@Log(module = BoCloudService.IMS, target = "FlashCopy", operation = "根据原卷查询可以作为FlashCopy目标卷的卷")
	@RequestMapping(value = "/volumes/{id}/targets", method = { RequestMethod.GET })
	public BocloudResult listTargetVolume(@PathVariable(value = Common.ID) long volumeId) {
		return flashCopyService.listTargetVolumeFromDriver(volumeId);
	}

}
