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

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.ims.entity.params.PoolGroupAction;
import com.bocloud.ims.entity.resource.PoolGroup;
import com.bocloud.ims.entity.resource.PoolGroupLabel;
import com.bocloud.ims.entity.resource.PoolGroupRelation;
import com.bocloud.ims.interfaces.resource.ResourcePoolService;
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;

@RestController
@RequestMapping("/v1/pool/groups")
@Api(value = "资源池", tags = "资源池")
public class ResourcePoolController {

    @Autowired
    private ResourcePoolService resourcePoolService;

    /**
     * 获取资源池列表
     *
     * @param pager
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "创建资源池分组", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "资源池", operation = "获取资源池列表")
    @GetMapping
    public BocloudResult<GridBean<PoolGroup>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return resourcePoolService.list(pager, context);
    }

    /**
     * 获取资源池列表
     *
     * @param condition
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "创建资源池分组", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "资源池", operation = "获取资源池列表")
    @GetMapping("/condition")
    public BocloudResult condition(Pager pager,
                                   @RequestParam(value = Common.CONDITION, required = false) String condition,
                                   @Value(Common.REQ_CONTEXT) RequestContext context) {
        return resourcePoolService.condition(pager, condition, context);
    }


    /**
     * 资源池容量信息统计
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "资源池容量信息统计", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "资源池", operation = "资源池容量信息统计")
    @GetMapping("/{id}/capacity")
    public BocloudResult capacity(@PathVariable(value = Common.ID) Long id,@Value(Common.REQ_CONTEXT) RequestContext context) {
        return resourcePoolService.capacity(id,context);
    }

    /**
     * 资源池分组树形展示
     *
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查看资源池列表信息", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "资源池", operation = "查询所有资源池")
    @GetMapping("/tree")
    public BocloudResult list(@RequestParam(value = Common.PARAMS, required = false) String params,
                              @Value(Common.REQ_CONTEXT) RequestContext context) {
        return resourcePoolService.groups(params, context);
    }

    /**
     * 资源池关联目标机器
     *
     * @param id
     * @param group
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "资源池关联目标机器", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "资源池", operation = "资源池关联目标机器")
    @PostMapping("/{id}/releation")
    public BocloudResult createReleation(@PathVariable(value = Common.ID) Long id, @RequestBody PoolGroup group) {
        group.setId(id);
        return resourcePoolService.createReleation(group);
    }

    /**
     * 资源池关联目标机器
     *
     * @param id
     * @param group
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "解除资源池关联目标机器", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "资源池", operation = "资源池关联目标机器")
    @DeleteMapping("/{id}/releation")
    public BocloudResult removeReleation(@PathVariable(value = Common.ID) Long id, @RequestBody PoolGroup group) {
        group.setId(id);
        return resourcePoolService.removeReleation(group);
    }

    /**
     * 资源池关联目标机器
     *
     * @param group
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "资源池打标签", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "资源池", operation = "资源池关联目标机器")
    @PostMapping("/labels")
    public BocloudResult createLabel(@RequestBody PoolGroup group) {
        return resourcePoolService.createLabel(group);
    }

    /**
     * 资源池绑定标签集合
     *
     * @param params
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "资源池绑定标签关系", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "资源池", operation = "资源池绑定标签集合")
    @GetMapping("/labels")
    public BocloudResult<List<PoolGroupLabel>> listPoolGroupLabel(
            @RequestParam(value = Common.PARAMS, required = false) String params) {
        return resourcePoolService.listPoolGroupLabel(params);
    }

    /**
     * 删除资源池标签
     *
     * @param id
     * @param group
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "解除资源池标签", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "资源池", operation = "资源池关联目标机器")
    @DeleteMapping("/{id}/labels")
    public BocloudResult removeLabel(@PathVariable(value = Common.ID) Long id, @RequestBody PoolGroup group) {
        return resourcePoolService.removeLabel(group);
    }

    /**
     * 创建资源池
     *
     * @param group
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "创建资源池分组", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "资源池", operation = "创建资源池")
    @PostMapping
    public BocloudResult<PoolGroup> create(@RequestBody PoolGroup group,
                                           @Value(Common.REQ_CONTEXT) RequestContext context) {
        group.setCreatorId(context.getTarget());
        group.setMenderId(context.getTarget());
        return resourcePoolService.create(group);
    }
    
    

    /**
     * 修改资源池
     *
     * @param id
     * @param bean
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "修改资源池分组", httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "资源池", operation = "修改资源池")
    @PutMapping("/{id}")
    public BocloudResult modify(@PathVariable(value = Common.ID) Long id, @RequestBody PoolGroup bean,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        bean.setId(id);
        return resourcePoolService.modify(bean, context.getTarget());
    }

    /**
     * 修改资源池状态
     *
     * @param action
     * @return
     */
    @ApiOperation(tags = {"CMC"}, value = "修改资源池状态", httpMethod = "PATCH")
    @Log(module = BoCloudService.IMS, target = "资源池", operation = "修改资源池状态")
    @PatchMapping
    public BocloudResult disable(@RequestBody PoolGroupAction action,
                                 @Value(Common.REQ_CONTEXT) RequestContext context) {
        switch (action.getAction()) {
            case able:
                return resourcePoolService.updateStatus(action.getIds(), "NORMAL", context);
            case disable:
                return resourcePoolService.updateStatus(action.getIds(), "ABNORMAL", context);
            default:
                return new BocloudResult(false, "不支持这种操作");
        }

    }
    
    /**
     * 修改资源池状态
     *
     * @param action
     * @return
     */
    @ApiOperation(tags = {"CMC"}, value = "修改资源池状态", httpMethod = "PATCH")
    @Log(module = BoCloudService.IMS, target = "资源池", operation = "修改资源池状态")
    @PatchMapping("region-relation")
    public BocloudResult modifyRegionRelation(@RequestBody PoolGroup group,
                                 @Value(Common.REQ_CONTEXT) RequestContext context) {
        return resourcePoolService.modifyRegionRelation(group.getRelationIds(), group.getIsUsed(), context);
    }

    /**
     * 绑定/解绑策略
     *
     * @param id
     * @param group
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "绑定/解绑策略", httpMethod = "PATCH")
    @Log(module = BoCloudService.IMS, target = "资源池", operation = "绑定/解绑策略")
    @PatchMapping("/{id}")
    public BocloudResult patch(@PathVariable(value = Common.ID) Long id, @RequestBody PoolGroup group,
                               @Value(Common.REQ_CONTEXT) RequestContext context) {
        return resourcePoolService.patch(id, group.getPolicyId(),
                null == group.getPriorityType() ? null : group.getPriorityType().name(), 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 resourcePoolService.remove(id, context.getTarget());
    }

    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查看资源池分组详情", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "资源池", operation = "查询详情")
    @GetMapping("/{id}")
    public BocloudResult<PoolGroup> detail(@PathVariable(value = Common.ID) Long id) {
        return resourcePoolService.detail(id);
    }
    
    /**
     * 资源启用、禁用
     *
     * @param id
     * @param poolGroupRelation
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "资源池关联目标机器", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "资源池", operation = "资源池关联目标机器")
    @PatchMapping("/{id}/resources")
    public BocloudResult resourcesEnabled(@PathVariable(value = Common.ID) Long id, @RequestBody PoolGroupRelation poolGroupRelation) {
    	poolGroupRelation.setPoolGroupId(id);
        return resourcePoolService.enabled(poolGroupRelation);
    }
}
