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

import com.bocloud.cmp.model.SnapshotGenericParam;
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.Snapshot;
import com.bocloud.ims.interfaces.resource.SnapshotService;
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.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/snapshots")
@Api(value = "快照管理", tags = "快照管理")
public class SnapshotController {

	@Autowired
	private SnapshotService snapshotService;

	/**
	 * @param pager
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "查询快照列表", httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "快照", operation = "查看快照列表")
	@GetMapping
	public BocloudResult<GridBean<Snapshot>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
		return snapshotService.list(pager, context);
	}

	@ApiOperation(tags = { "CMC", "CSC" }, value = "查询树形快照列表", httpMethod = "GET")
	@GetMapping("/tree")
	public BocloudResult<List<Snapshot>> tree(@RequestParam(value = Common.PARENTID) Long parentId,
			@RequestParam(value = Common.VENDORID, required = false) Long vendorId,
			@RequestParam(value = "serverId", required = false) Long serverId,
			@Value(Common.REQ_CONTEXT) RequestContext requestContext) {
		return snapshotService.tree(parentId, vendorId, serverId, requestContext);
	}

	/**
	 * 根据condition查询快照列表
	 *
	 * @param condition
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "根据condition查询快照列表", httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "快照", operation = "查看快照列表")
	@GetMapping("/condition")
	public BocloudResult condition(@RequestParam(value = Common.CONDITION, required = false) String condition,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return snapshotService.snapshots(condition, context);
	}

	/**
	 * 创建
	 *
	 * @param snapshot
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "创建快照", httpMethod = "POST")
	@Log(module = BoCloudService.IMS, target = "快照", operation = "创建快照")
	@PostMapping
	public BocloudResult<String> create(@RequestBody Snapshot snapshot,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return snapshotService.create(snapshot, context);
	}

	/**
	 * 删除
	 *
	 * @param id
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "删除快照", httpMethod = "DELETE")
	@Log(module = BoCloudService.IMS, target = "快照", operation = "删除快照")
	@DeleteMapping(value = "/{id}")
	public BocloudResult remove(@PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return snapshotService.remove(id, context);
	}

	/**
	 * 详情
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "查看快照详情", httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "快照", operation = "查看快照详情")
	@GetMapping(value = "/{id}")
	public BocloudResult<Snapshot> detail(@PathVariable(value = Common.ID) Long id) {
		return snapshotService.detail(id);
	}

	/**
	 * 恢复 / 更新到期时间
	 *
	 * @param id
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "回复快照", httpMethod = "PATCH")
	@Log(module = BoCloudService.IMS, target = "快照", operation = "恢复快照")
	@PatchMapping("/{id}")
	public BocloudResult revert(@PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context, @RequestBody SnapshotGenericParam snapshotGenericParam) {
		return snapshotService.operation(id, context, snapshotGenericParam);
	}

	/**
	 * 修改快照
	 *
	 * @param id
	 * @param snapshot
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "修改快照", httpMethod = "PUT")
	@Log(module = BoCloudService.IMS, target = "快照", operation = "修改快照")
	@PutMapping(value = "/{id}")
	public BocloudResult modify(@PathVariable(value = Common.ID) Long id, @RequestBody Snapshot snapshot,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return snapshotService.modify(id, snapshot, 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> ids, @Value(Common.REQ_CONTEXT) RequestContext context) {
		return snapshotService.batchRemove(ids, context);
	}

}
