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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.http.MediaType;
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 com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.model.kubernetes.KubeNodeParam;
import com.bocloud.common.annotations.Log;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BocloudResult;
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.interfaces.resource.kubernetes.KubeNodeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/v1/kubernetes/nodes")
@Scope("prototype")
@Api(value = "Kubernetes节点管理", tags = "Kubernetes节点管理")
public class KubeNodeController {

	@Autowired
	private KubeNodeService kubeNodeService;

	/**
	 * 列表
	 * 
	 * @param page   当前页码
	 * @param rows   页面数据大小
	 * @param params 查询参数，例如：[{"param":{"name":"aaa","password":"1245"},"sign":
	 *               "EQ|UEQ"},{"param":{"name":"aaa","password":"1245"},"sign":
	 *               "EQ|UEQ"}]
	 * @param sorter 排序参数，例如：{"name":0|1,"password":0|1},0表示增序，1表示降序
	 * @param simple 简单查询标记，只有true和false,为false时返回用户的详细信息，为true时只返回id和name值。
	 * @param user   操作者信息
	 * @return 数据结果集
	 */
	@GetMapping
	@Log(module = BoCloudService.IMS, target = "Kubernetes节点", operation = "查看节点列表")
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "节点列表", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	public BocloudResult list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
		return kubeNodeService.list(pager);
	}

	/**
	 * 添加
	 * 
	 * @param params
	 * @param user
	 * @return 添加结果
	 */
	@PostMapping
	@Log(module = BoCloudService.IMS, target = "Kubernetes节点", operation = "添加节点")
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "节点创建", response = BocloudResult.class, httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	public BocloudResult operation(@RequestParam(value = Common.PARAMS, required = true) String params,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		JSONObject object = JSONTools.isJSONObj(params);
		if (null != object) {
			return kubeNodeService.operation(params, context);
		}
		return ResultTools.formatErrResult();

	}

	/**
	 * 修改
	 * 
	 * @param params
	 * @param id
	 * @param user
	 * @return
	 */
	@PutMapping(value = "/{id}")
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "节点修改", response = BocloudResult.class, httpMethod = "PUT", produces = MediaType.APPLICATION_JSON_VALUE)
	@Log(module = BoCloudService.IMS, target = "Kubernetes节点", operation = "修改节点")
	public BocloudResult modify(@RequestBody KubeNodeParam kubeNodeParam, @PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null != kubeNodeParam) {
			return kubeNodeService.modify(kubeNodeParam, context);
		}
		return ResultTools.formatErrResult();
	}

	/**
	 * 移除
	 * 
	 * @param id
	 * @param user
	 * @return 操作结果
	 */
	@DeleteMapping(value = "/{id}")
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "节点删除", response = BocloudResult.class, httpMethod = "DELETE", produces = MediaType.APPLICATION_JSON_VALUE)
	@Log(module = BoCloudService.IMS, target = "Kubernetes节点", operation = "移除节点")
	public BocloudResult remove(@PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return kubeNodeService.remove(id, context);
	}

	/**
	 * 详细信息
	 * 
	 * @param id
	 * @return 操作结果
	 */
	@GetMapping(value = "/{id}")
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "节点详情", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@Log(module = BoCloudService.IMS, target = "Kubernetes节点", operation = "查看节点详情")
	public BocloudResult detail(@PathVariable(value = Common.ID) Long id) {
		return kubeNodeService.detail(id);
	}

	/**
	 * 操作节点
	 *
	 * @param id 任务ID
	 * @return 操作结果
	 */
	@PatchMapping(value = "/{id}")
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "节点操作", response = BocloudResult.class, httpMethod = "PATCH", produces = MediaType.APPLICATION_JSON_VALUE)
	@Log(module = BoCloudService.COP, target = "Kubernetes节点", operation = "节点操作")
	public BocloudResult action(@PathVariable(value = Common.ID) Long id, @RequestBody KubeNodeParam kubeNodeParam,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null == kubeNodeParam) {
			return new BocloudResult(false, "参数格式错误");
		}
		if ("schedule".equals(kubeNodeParam.getAction())) {
			return kubeNodeService.schedule(id, context);
		}
		return new BocloudResult(false, "暂不支持该类操作");
	}

}
