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

import com.alibaba.fastjson.JSONArray;
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.SchedulerPolicy;
import com.bocloud.ims.interfaces.resource.SchedulerPolicyService;
import io.swagger.annotations.ApiOperation;
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;

/**
 * 资源调度Controller
 *
 * @author wangyu
 * @version 4.1.0-SNAPSHOT
 * @since 2018/12/24
 */
@RestController
@RequestMapping("/v1/rsps")
public class SchedulerPolicyController {

    @Autowired
    private SchedulerPolicyService schedulerPolicyService;

    /**
     * 查询调度策略列表
     *
     * @param pager
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "查询调度策略列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "调度策略", operation = "查询调度策略列表")
    @GetMapping
    public BocloudResult<GridBean<SchedulerPolicy>> list(Pager pager) {
        return schedulerPolicyService.list(pager);
    }

    /**
     * 创建调度策略
     *
     * @param policy
     * @param context
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "创建调度策略",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "调度策略", operation = "创建调度策略")
    @PostMapping
    public BocloudResult<SchedulerPolicy> create(@RequestBody SchedulerPolicy policy,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return schedulerPolicyService.create(policy, context);
    }

    /**
     * 修改调度策略
     *
     * @param policy
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "修改调度策略",  httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "调度策略", operation = "修改调度策略")
    @PutMapping(value = "/{id}")
    public BocloudResult<SchedulerPolicy> modify(@RequestBody SchedulerPolicy policy,
                                @PathVariable(value = Common.ID) Long id, @Value(Common.REQ_CONTEXT) RequestContext context) {
        policy.setId(id);
        return schedulerPolicyService.modify(policy, context);
    }

    /**
     * 删除调度策略
     *
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "删除调度策略",  httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "调度策略", operation = "删除调度策略")
    @RequestMapping(value = "/{id}/remove", method = {RequestMethod.DELETE})
    public BocloudResult<String> remove(@PathVariable(value = Common.ID) Long id,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return schedulerPolicyService.remove(id, context);
    }

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

    /**
     * 绑定集群
     *
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "绑定集群",  httpMethod = "PATCH")
    @Log(module = BoCloudService.IMS, target = "调度策略", operation = "绑定集群")
    @PatchMapping("/{id}/clusters")
    public BocloudResult<SchedulerPolicy> clusterBind(@PathVariable(value = Common.ID) Long id,
                                     @RequestParam(value = Common.PARAMS, required = false) String params,
                                     @Value(Common.REQ_CONTEXT) RequestContext context) {
        JSONObject object = JSONTools.isJSONObj(params);
        if (null != object) {
            List<Long> clusterIds = JSONArray.parseArray(object.getString("clusterIds"), Long.class);
            return schedulerPolicyService.clusterBind(object.getLong("id"), clusterIds, context);
        } else {
            return ResultTools.formatErrResult();
        }
    }
}
