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

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.entity.enums.ResourceCategory;
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.common.utils.ResultTools;
import com.bocloud.ims.entity.resource.OSTenant;
import com.bocloud.ims.entity.resource.PoolGroup;
import com.bocloud.ims.interfaces.resource.OSTenantService;
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.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * openstack租户控制器
 *
 * @author wangyu
 * @version 4.2.1-SNAPSHOT
 * @since 2019/11/14
 */
@RestController
@RequestMapping("/v1/ostenants")
@Api(value = "openstack租户管理", tags = "openstack租户管理")
public class OSTenantController {

	@Autowired
	private OSTenantService osTenantService;

	/**
	 * 获取openstack租户列表
	 *
	 * @param pager
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "查询openstack租户列表", httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "openstack租户", operation = "获取openstack租户列表")
	@GetMapping
	public BocloudResult<GridBean<OSTenant>> list(Pager pager) {
		return osTenantService.list(pager);
	}

	/**
	 * 通过condition方式查询op项目
	 * 
	 * @param condition
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "通过condition方式查询op项目", httpMethod = "GET")
	@GetMapping("/condition")
	public BocloudResult condition(
			@RequestParam(value = Common.CONDITION, required = false) String condition,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return osTenantService.condition(condition, context);
	}

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

	/**
	 * 保存vpc
	 * 
	 * @param params
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "保存OpenStack项目", httpMethod = "POST")
	@PostMapping
	public BocloudResult<String> create(@RequestBody OSTenant oSTenant,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null != oSTenant) {
			return osTenantService.create(oSTenant, context);
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 删除OpenStack项目
	 * 
	 * @param id
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "删除OpenStack项目", httpMethod = "DELETE")
	@DeleteMapping(value = "/{id}")
	public BocloudResult<String> remove(@PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return osTenantService.remove(id, context);
	}

	/**
	 * 更新OpenStack项目
	 * 
	 * @param id
	 * @param params
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "更新OpenStack项目", httpMethod = "PUT")
	@PutMapping(value = "/{id}")
	public BocloudResult<String> modify(@PathVariable(value = Common.ID) Long id, @RequestBody OSTenant oSTenant,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null != oSTenant) {
			oSTenant.setId(id);
			return osTenantService.modify(oSTenant, context);
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 项目分配
	 *
	 * @param id
	 * @param params
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "项目分配", httpMethod = "POST")
	@Log(module = BoCloudService.IMS, target = "云主机", operation = "云主机用户分配")
	@PostMapping("/{id}/tenant")
	public BocloudResult assign(@PathVariable(value = Common.ID) Long id, @RequestBody OSTenant osTenant,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return osTenantService.assign(osTenant, context);
	}

	/**
	 * 项目回收
	 *
	 * @param id
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "回收租户云主机", httpMethod = "DELETE")
	@Log(module = BoCloudService.IMS, target = "云主机", operation = "回收租户云主机")
	@DeleteMapping("/{id}/tenant")
	public BocloudResult reclaim(@PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return osTenantService.reclaim(id, context);
	}
	
	/**
	 * 项目启用禁用
	 * 
	 * @param group
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC" }, value = "修改资源池状态", httpMethod = "PATCH")
	@Log(module = BoCloudService.IMS, target = "企业项目", operation = "修改企业项目启用禁用")
	@PatchMapping("/enabled")
	public BocloudResult modifyRegionRelation(@RequestBody OSTenant osTenant,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return osTenantService.enabled(osTenant, context);
	}

}
