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

import com.alibaba.fastjson.JSONObject;
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.Keypair;
import com.bocloud.ims.interfaces.resource.KeypairService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/v1/keypairs")
@Api(value = "密钥", tags = "密钥")
public class KeypairController {

	@Autowired
	private KeypairService keypairService;

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

	/**
	 * 查询密钥列表
	 * 
	 * @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 keypairService.condition(condition, context);
	}

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

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

	/**
	 * 创建密钥
	 *
	 * @param keypair
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "创建密钥",  httpMethod = "POST")
	@Log(module = BoCloudService.IMS, target = "密钥", operation = "创建密钥")
	@PostMapping
	public BocloudResult<Keypair> create(@RequestBody Keypair keypair,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return keypairService.create(keypair, context);
	}

	/**
	 * v
	 *
	 * @param id
	 * @param keypair
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "修改密钥",  httpMethod = "PUT")
	@Log(module = BoCloudService.IMS, target = "密钥", operation = "创建密钥")
	@PutMapping("/{id}")
	public BocloudResult<Keypair> modify(@PathVariable(value = Common.ID) Long id, @RequestBody Keypair keypair,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		keypair.setId(id);
		return keypairService.modify(keypair, 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> idArray,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return keypairService.batchRemove(idArray, context);
	}

	/**
	 * 秘钥批量绑定虚拟机
	 *
	 * @param params
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "密钥批量删除",  httpMethod = "PATCH")
	@Log(module = BoCloudService.IMS, target = "密钥", operation = "秘钥批量绑定虚拟机")
	@PatchMapping("/{id}")
	public BocloudResult batchBindServer(@PathVariable(value = Common.ID) Long id,
			@RequestBody Keypair keypair, @Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null == keypair) {
			return ResultTools.formatErrResult();
		}
		keypair.setId(id);
		return keypairService.batchBindServer(keypair, context);
	}

	/**
	 * 下载私钥
	 *
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "下载私钥",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "密钥", operation = "下载私钥")
	@GetMapping("/download")
	public void download(@RequestParam(value = Common.ID) Long id, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		BocloudResult result = keypairService.getPrivateKey(id);
		Object object = result.getData();
		JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(object), JSONObject.class);
		response.setContentType("application/x-x509-ca-cert");
		response.setHeader("Content-disposition",
				"attachment; filename=" + URLEncoder.encode(jsonObject.getString("name"), "utf-8") + ".pem");
		OutputStream writer = response.getOutputStream();
		if (!StringUtils.isEmpty(jsonObject.getString("privateKeyContent"))) {
			writer.write(jsonObject.getString("privateKeyContent").getBytes());
		}
		if (null != writer) {
			writer.close();
		}
	}

}
