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.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
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.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.entity.resource.kubernetes.KubeNamespace;
import com.bocloud.ims.interfaces.resource.kubernetes.KubeNamespaceService;

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

@RestController
@RequestMapping("/v1/kubernetes/namespaces")
@Api(value = "Kubernetes命名空间管理", tags = "Kubernetes命名空间管理")
public class KubeNamespaceController {

	@Autowired
	private KubeNamespaceService kubeNamespaceService;

	/**
	 * 列表
	 * 
	 * @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 kubeNamespaceService.list(pager, context);
	}

	/**
	 * 添加
	 * 
	 * @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 create(@RequestBody KubeNamespace namespace,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null != namespace) {
			return kubeNamespaceService.create(namespace, context);
		}
		return ResultTools.formatErrResult();

	}

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

	}

	/**
	 * 查询 应用下的服务列表
	 * 
	 * @param params
	 * @param user
	 * @return 添加结果
	 */
	@GetMapping("/app/services")
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "服务列表", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@Log(module = BoCloudService.IMS, target = "Kubernetes", operation = "添加service")
	public BocloudResult listAllServices(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
		return kubeNamespaceService.listAllServices(pager.getPage(), pager.getRows(), pager.getParams(),
				pager.getSorter(), pager.getSimple(), context);
	}

	/**
	 * 查询 应用下的服务列表
	 * 
	 * @param params
	 * @param user
	 * @return 添加结果
	 */
	@GetMapping("/{id}/services")
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "服务列表", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@Log(module = BoCloudService.IMS, target = "Kubernetes", operation = "添加service")
	public BocloudResult listServices(@PathVariable(value = Common.ID) Long id,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return kubeNamespaceService.listServices(id, params, context);
	}

	/**
	 * 查询 应用下的服务列表
	 * 
	 * @param params
	 * @param user
	 * @return 添加结果
	 */
	@GetMapping("/app/services/{id}")
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "查询应用下服务列表详情", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@Log(module = BoCloudService.IMS, target = "Kubernetes", operation = "添加service")
	public BocloudResult serviceDetail(@PathVariable(value = Common.ID) Long id,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return kubeNamespaceService.serviceDetail(params, context);
	}

	/**
	 * 查询 应用下的服务列表
	 * 
	 * @param params
	 * @param user
	 * @return 添加结果
	 */
	@GetMapping("/{id}/tree")
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "查询应用下服务列表", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@Log(module = BoCloudService.IMS, target = "Kubernetes", operation = "添加service")
	public BocloudResult servicesTree(@PathVariable(value = Common.ID) Long id,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return kubeNamespaceService.servicesTree(id, params, context);
	}

	/**
	 * 删除 应用下的服务
	 * 
	 * @param params
	 * @param user
	 * @return 添加结果
	 */
	@DeleteMapping("/{id}/services")
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "删除应用下服务", response = BocloudResult.class, httpMethod = "DELETE", produces = MediaType.APPLICATION_JSON_VALUE)
	@Log(module = BoCloudService.IMS, target = "Kubernetes", operation = "删除service")
	public BocloudResult removeServices(@PathVariable(value = Common.ID) Long id,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return kubeNamespaceService.removeServices(id, params, context);
	}

	/**
	 * 修改
	 * 
	 * @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 KubeNamespace namespace, @PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null != namespace) {
			return kubeNamespaceService.modify(namespace, 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 kubeNamespaceService.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 kubeNamespaceService.detail(id);
	}

}
