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

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.model.ClusterBean;
import com.bocloud.cmp.model.ClusterOperateModel;
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.params.ServerPerformanceParam;
import com.bocloud.ims.entity.resource.AlarmThreshold;
import com.bocloud.ims.entity.resource.Cluster;
import com.bocloud.ims.interfaces.resource.ClusterService;
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.*;

import java.util.List;
import java.util.Map;

@Api(value = "集群", tags = "集群")
@RestController
@RequestMapping("/v1/clusters")
public class ClusterController {

    @Autowired
    private ClusterService clusterService;

    /**
     * 查看集群列表
     *
     * @param pager
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询集群列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "集群", operation = "查看集群列表")
    @GetMapping
    public BocloudResult<GridBean<Cluster>> list(Pager pager) {
        return clusterService.list(pager);
    }

    /**
     * 添加集群
     *
     * @param model
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "创建集群",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "集群", operation = "添加集群")
    @PostMapping
    public BocloudResult<Cluster> create(@RequestBody ClusterOperateModel model,
                                         @Value(Common.REQ_CONTEXT) RequestContext context) {
        return clusterService.create(model, context);
    }

    /**
     * 修改集群
     *
     * @param id
     * @param bean
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "更新集群信息",  httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "集群", operation = "修改集群")
    @PutMapping("/{id}")
    public BocloudResult<Cluster> modify(@PathVariable(value = Common.ID) Long id,
                                         @RequestBody ClusterBean bean, @Value(Common.REQ_CONTEXT) RequestContext context) {
        bean.setId(id);
        return clusterService.modify(bean, context);
    }

    /**
     * 移除集群
     *
     * @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 clusterService.remove(id, context);
    }

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

    /**
     * 集群添加宿主机
     *
     * @param params
     * @param context
     * @return
     */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "集群移入主机", httpMethod = "POST")
	@Log(module = BoCloudService.IMS, target = "集群", operation = "集群添加宿主机")
	@PostMapping("/{id}/hosts")
	public BocloudResult addHost(@PathVariable(value = Common.ID) Long id, @RequestBody ClusterBean bean,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		JSONObject object = JSONTools.isJSONObj(JSONObject.toJSONString(bean));
		if (null != object) {
			object.put("clusterId", id);
			object.put(Common.ACTION, "addOsHost");
			return clusterService.moveHost(object, context);
		}
		return ResultTools.formatErrResult();
	}

    /**
     * 删除集群中的主机
     *
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "集群移出主机",  httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "集群", operation = "删除集群中的主机")
    @DeleteMapping("/{id}/hosts")
	public BocloudResult removeHost(@PathVariable(value = Common.ID) Long id, @RequestBody ClusterBean bean,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		JSONObject object = JSONTools.isJSONObj(JSONObject.toJSONString(bean));
		if (null != object) {
			object.put("clusterId", id);
			object.put(Common.ACTION, "removeOsHost");
			return clusterService.moveHost(object, context);
		}
		return ResultTools.formatErrResult();
	}

    /**
     * 设置集群告警阈值
     *
     * @param id
     * @param alarmThreshold
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "设置集群告警阈值",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "集群", operation = "设置集群告警阈值")
    @PostMapping("/{id}/thresholds")
    public BocloudResult<AlarmThreshold> createAlarm(@PathVariable(value = Common.ID) Long id,
                                                     @RequestBody AlarmThreshold alarmThreshold) {
        alarmThreshold.setTarget(id);
        return clusterService.createAlarm(alarmThreshold);
    }

    /**
     * 查询告警阈值详情
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询告警阈值",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "集群", operation = "查询告警阈值详情")
    @GetMapping("/{id}/thresholds")
    public BocloudResult<AlarmThreshold> getAlarm(@PathVariable(value = Common.ID) Long id) {
        return clusterService.getAlarm(id);
    }

    /**
     * 获取监控数据接口
     *
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "获取监控数据",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "集群", operation = "获取监控数据")
    @RequestMapping(value = "/{id}/metrics", method = {RequestMethod.GET})
    public BocloudResult performance(@RequestParam(value = Common.PARAMS, required = false) String params,
                                     @Value(Common.REQ_CONTEXT) RequestContext context, @PathVariable(value = Common.ID) Long id) {
    	JSONObject object = JSONTools.isJSONObj(params);
		if (null != object) {
			return clusterService.performance(params);
		}
		return ResultTools.formatErrResult();
    }

    /**
     * 查询集群资源使用趋势
     *
     * @param id
     * @param params
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询集群资源使用趋势",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "集群", operation = "查询集群资源使用趋势")
    @GetMapping("/{id}/tendency")
	public BocloudResult<Map<String, Object>> getTrend(@PathVariable(value = Common.ID) Long id,
			@RequestParam(value = Common.PARAMS, required = false) String params) {
		return clusterService.getTrend(id, params);
	}

    /**
     * 批量删除集群
     *
     * @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 clusterService.batchRemove(idArray, context);
    }

    @ApiOperation(value = "同步集群下宿主机",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "云平台", operation = "同步集群下宿主机")
    @PostMapping("/{id}/sync")
    public BocloudResult sync(@PathVariable(value = Common.ID) Long id,
                              @Value(Common.REQ_CONTEXT) RequestContext context) {
        return clusterService.syncHostByCluster(id, context);
    }
}
