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

import com.alibaba.fastjson.JSON;
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.JSONTools;
import com.bocloud.common.utils.ResultTools;
import com.bocloud.ims.entity.resource.Resource;
import com.bocloud.ims.interfaces.resource.ResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.http.HttpHeaders;
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.List;

@RestController
@RequestMapping("/v1/resources")
@Api(value = "平台资源", tags = "平台资源")
public class ResourceController {

	@Autowired
	private ResourceService resourceService;

	@ApiOperation(tags={"CMC","CSC"},value = "查询平台资源列表",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "云主机或物理机资源", operation = "查询资源信息列表")
	@GetMapping
	public BocloudResult<GridBean<Resource>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
		return resourceService.list(pager, context);
	}

	@ApiOperation(tags={"CMC","CSC"},value = "通过condition查询平台资源列表",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "云主机或物理机资源", operation = "查询资源信息列表")
	@GetMapping("/condition")
	public BocloudResult<Long> condition(@RequestParam(value = Common.CONDITION, required = false) String condition) {
		return resourceService.resources(condition);
	}

	@ApiOperation(tags={"CMC","CSC"},value = "获取堡垒机accessClient下载链接",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "云主机或物理机资源", operation = "获取堡垒机accessClient下载链接")
	@GetMapping(value = "/access/client")
	public BocloudResult accessClient(@RequestParam(value = Common.PARAMS, required = true) String params,
									  @Value(Common.REQ_CONTEXT) RequestContext context) {
		return resourceService.accessClient(params, context);
	}

	@ApiOperation(tags={"CMC","CSC"},value = "创建资源",  httpMethod = "POST")
	@Log(module = BoCloudService.IMS, target = "平台资源", operation = "创建资源")
	@PostMapping
	public BocloudResult create(@RequestParam(value = Common.PARAMS, required = true) String params,
								@Value(Common.REQ_CONTEXT) RequestContext context) {
		if (JSONTools.isJSON(params).isFailed()) {
			return ResultTools.formatErrResult();
		}
		return resourceService.create(JSON.parseObject(params, Resource.class), context);
	}

	/**
	 * 详细信息
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(tags={"CMC","CSC"},value = "查看资源详情信息",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "云主机或物理机资源", operation = "查看资源详情")
	@GetMapping(value = "/{id}")
	public BocloudResult detail(@PathVariable(value = Common.ID) Long id,
								@RequestParam(value = Common.PARAMS, required = false) String params,
								@RequestParam(value = Common.TYPE, required = false) String type) {
		if (null != type) {
			return resourceService.detail(id, params, type);
		}
		return resourceService.detail(id);
	}

	/**
	 * 修改账户，密码
	 *
	 * @param params
	 * @param type
	 * @param id
	 * @param context
	 * @return
	 */
	@ApiOperation(tags={"CMC","CSC"},value = "修改账户密码",  httpMethod = "PUT")
	@Log(module = BoCloudService.IMS, target = "云主机或物理机资源", operation = "修改账户密码")
	@PutMapping(value = "/{id}")
	public BocloudResult modify(@RequestParam(value = Common.PARAMS, required = true) String params,
								@RequestParam(value = Common.TYPE, required = false) String type,
								@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_CONTEXT) RequestContext context) {
		if ("common".equals(type)) {
			return resourceService.modify(params, context);
		}
		return resourceService.modifyAccount(params, context);
	}


	/**
	 * 修删除平台资源
	 *
	 * @param params
	 * @param id
	 * @param context
	 * @return
	 */
	@ApiOperation(tags={"CMC","CSC"},value = "删除资源",  httpMethod = "DELETE")
	@Log(module = BoCloudService.IMS, target = "平台资源", operation = "删除平台资源")
	@DeleteMapping(value = "/{id}")
	public BocloudResult delete(@RequestParam(value = Common.PARAMS, required = false) String params,
								@PathVariable(value = Common.ID) Long id,
								@Value(Common.REQ_CONTEXT) RequestContext context) {
		return resourceService.delete(id, params, context);
	}

	/**
	 * 操作
	 *
	 * @param params
	 * @param action
	 * @param context
	 * @return
	 */
	@ApiOperation(tags={"CMC","CSC"},value = "修改账户密码",  httpMethod = "PATCH")
	@Log(module = BoCloudService.IMS, target = "操作对象", operation = "操作资源")
	@PatchMapping
	public BocloudResult batchPatch(@RequestParam(value = Common.PARAMS, required = false) String params,
									@RequestParam(value = Common.ACTION, required = false) String action,
									@Value(Common.REQ_CONTEXT) RequestContext context) {
		return resourceService.patch(action, params, context);
	}

	/**
	 * 操作
	 *
	 * @param params
	 * @param action
	 * @param context
	 * @return
	 */
	@ApiOperation(tags={"CMC","CSC"},value = "修改账户密码",  httpMethod = "PATCH")
	@Log(module = BoCloudService.IMS, target = "操作对象", operation = "操作资源")
	@PatchMapping(value = "/{id}")
	public BocloudResult patch(@RequestParam(value = Common.PARAMS, required = false) String params,
							   @RequestParam(value = Common.ACTION, required = false) String action,
							   @PathVariable(value = Common.ID) Long id,
							   @RequestHeader(HttpHeaders.USER_AGENT) String agent,
							   @Value(Common.REQ_CONTEXT) RequestContext context) {
		return resourceService.patch(id, action, params, agent, context);
	}

}
