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

import com.bocloud.cmp.model.CloudStackParam;
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.CloudStack;
import com.bocloud.ims.entity.resource.CloudStackEvent;
import com.bocloud.ims.entity.resource.StackTemplateVersion;
import com.bocloud.ims.interfaces.resource.CloudStackService;
import com.bocloud.ims.interfaces.resource.StackTemplateVersionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

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.*;

@RestController
@RequestMapping("/v1/heat/stacks")
@Api(value = "Heat管理", tags = "Heat管理")
public class CloudStackController {

	@Autowired
	private CloudStackService cloudStackService;
	@Autowired
	private StackTemplateVersionService stackTemplateVersionService;

	/**
	 * 查询Heat列表
	 *
	 * @param pager
	 * @param condition
	 * @param context
	 * @return
	 */
	@Log(module = BoCloudService.IMS, target = "Heat管理", operation = "查询Heat列表")
	@GetMapping
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "Heat列表查询",  httpMethod = "GET")
	public BocloudResult<GridBean<CloudStack>> list(Pager pager) {
		return cloudStackService.list(pager);
	}

	/**
	 * 查询stack event 列表
	 *
	 * @param pager
	 * @param context
	 * @return
	 */
	@Log(module = BoCloudService.IMS, target = "Heat管理", operation = "查询Heat列表")
	@GetMapping("/event")
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "Heat事件列表查询",  httpMethod = "GET")
	public BocloudResult<GridBean<CloudStackEvent>> stackEventList(Pager pager) {
		return cloudStackService.stackEventList(pager);
	}

	/**
	 * 查询Heat列表
	 *
	 * @param pager
	 * @param context
	 * @return
	 */
	@Log(module = BoCloudService.IMS, target = "Heat管理", operation = "查询Heat Version列表")
	@GetMapping("/versions")
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "Heat版本列表查询",  httpMethod = "GET")
	public BocloudResult<GridBean<StackTemplateVersion>> listVersion(Pager pager,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return stackTemplateVersionService.list(pager, context);
	}

	/**
	 * 栈操作类
	 *
	 * @param params
	 * @return 添加结果
	 */
	@Log(module = BoCloudService.IMS, target = "虚拟机", operation = "克隆虚拟机")
	@PatchMapping("/{id}")
	@ApiOperation(tags = { "CMC", "CSC" }, value = "Heat栈操作", httpMethod = "PATCH")
	public BocloudResult operation(@PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context, @RequestBody CloudStackParam cloudStackParam) {
		cloudStackParam.setId(id);
		return cloudStackService.operation(id, context, cloudStackParam);
	}

	/**
	 * 保存Heat
	 *
	 * @param cloudStack
	 * @param context
	 * @return
	 */
	@Log(module = BoCloudService.IMS, target = "Heat管理", operation = "保存Heat Policy")
	@PostMapping
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "Heat创建",  httpMethod = "POST")
	public BocloudResult create(@RequestBody CloudStack cloudStack, @Value(Common.REQ_CONTEXT) RequestContext context) {
		return cloudStackService.create(cloudStack, context);
	}

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

	/**
	 * 批量删除堆栈
	 *
	 * @return 操作结果
	 */
	@Log(module = BoCloudService.IMS, target = "堆栈", operation = "删除堆栈")
	@DeleteMapping
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "Heat批量删除",  httpMethod = "DELETE")
	public BocloudResult batchRemove(@RequestBody List<Long> ids, @Value(Common.REQ_CONTEXT) RequestContext context) {
		return cloudStackService.batchRemove(ids, context);
	}

	/**
	 * 更新HeatPolicy
	 *
	 * @param id
	 * @param cloudStack
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "Heat更新",  httpMethod = "PUT")
	@Log(module = BoCloudService.IMS, target = "Heat管理", operation = "更新Heat")
	@PutMapping(value = "/{id}")
	public BocloudResult modify(@PathVariable(value = Common.ID) Long id, @RequestBody CloudStack cloudStack,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		cloudStack.setId(id);
		return cloudStackService.modify(cloudStack, context.getTarget());
	}

	/**
	 * 查询HeatPolicy详情
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "查询Heat详情",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "Heat管理", operation = "查询Heat详情")
	@GetMapping(value = "/{id}")
	public BocloudResult<CloudStack> detail(@PathVariable(value = Common.ID) Long id) {
		return cloudStackService.detail(id);
	}

}
