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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.model.DeleteParam;
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.azure.blob.Container;
import com.bocloud.ims.interfaces.resource.azure.blob.ContainerService;
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;

/**
 * azure容器访问
 *
 * @author wangyu
 * @version 5.0.0-SNAPSHOT
 * @since 2021/2/2
 */
@RestController
@RequestMapping("/v1/azure/containers")
@Api(value = "容器管理管理", tags = "容器管理管理")
public class ContainerController {


    @Autowired
    private ContainerService containerService;

    @ApiOperation(value = "获取容器列表")
    @Log(module = BoCloudService.IMS, target = "容器", operation = "获取容器列表")
    @GetMapping
    public BocloudResult<GridBean<Container>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return containerService.list(pager, context);
    }

    @ApiOperation(value = "添加容器")
    @Log(module = BoCloudService.IMS, target = "容器", operation = "添加容器")
    @PostMapping
    public BocloudResult<Container> create(@RequestBody Container container,
                                           @Value(Common.REQ_CONTEXT) RequestContext context) {
        return containerService.create(container, context);
    }


    @ApiOperation(value = "更新容器元数据")
    @Log(module = BoCloudService.IMS, target = "容器", operation = "更新容器元数据")
    @PutMapping("/{id}/metadata")
    public BocloudResult updateMetadata(@PathVariable(value = Common.ID) Long id,
                                        @RequestBody Map<String, String> metadata, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return containerService.updateMetadata(id, metadata, context);
    }


    @ApiOperation(value = "删除容器")
    @Log(module = BoCloudService.IMS, target = "容器", operation = "删除容器")
    @DeleteMapping("/{id}")
    public BocloudResult remove(@PathVariable(value = Common.ID) Long id,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return containerService.remove(id, context);
    }

    @ApiOperation(value = "查看容器详情")
    @Log(module = BoCloudService.IMS, target = "容器", operation = "查看容器详情")
    @GetMapping("/{id}")
    public BocloudResult<Container> detail(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return containerService.detail(id, context);
    }

    @ApiOperation(value = "批量删除容器")
    @Log(module = BoCloudService.IMS, target = "容器", operation = "批量删除容器")
    @DeleteMapping
    public BocloudResult batchRemove(@RequestBody DeleteParam params,
                                     @Value(Common.REQ_CONTEXT) RequestContext context) {
        return containerService.batchRemove(params, context);
    }

    @ApiOperation(value = "分配容器")
    @Log(module = BoCloudService.IMS, target = "容器", operation = "分配容器")
    @PostMapping("/{id}/users")
    public BocloudResult assign(@PathVariable(value = Common.ID) Long id, @RequestParam(value = Common.PARAMS) String params, @Value(Common.REQ_CONTEXT) RequestContext context) {
        JSONObject object = JSONTools.isJSONObj(params);
        if (null != object) {
            object.put("id", id);
            return containerService.assign(object, context);
        } else {
            return ResultTools.formatErrResult();
        }
    }


    @ApiOperation(value = "批量分配容器")
    @Log(module = BoCloudService.IMS, target = "容器", operation = "批量分配容器")
    @PostMapping("/users/batch")
    public BocloudResult assignBatch(@RequestParam(value = Common.PARAMS) String params, @Value(Common.REQ_CONTEXT) RequestContext context) {
        JSONObject object = JSONObject.parseObject(params);
        Long userId = object.getLong("userId");
        // 租户用户所在项目
        Long projectId = object.getLong("projectId");
        Long tenantId = object.getLong("tenantId");
        List<Long> ids = JSONArray.parseArray(object.getString("ids"), Long.class);
        for (Long id : ids) {
            JSONObject paramsObject = new JSONObject();
            paramsObject.put("userId", userId);
            paramsObject.put("projectId", projectId);
            paramsObject.put("tenantId", tenantId);
            paramsObject.put("id", id);
            containerService.assign(paramsObject, context);
        }
        return new BocloudResult(true, "操作成功");
    }

    @ApiOperation(value = "容器操作")
    @Log(module = BoCloudService.IMS, target = "容器", operation = "容器操作")
    @PatchMapping("/{id}")
    public BocloudResult action(@PathVariable(value = Common.ID) Long id, @RequestParam(value = Common.PARAMS) String params, @RequestParam(value = Common.ACTION) String action,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return containerService.action(id, action, JSONObject.parseObject(params), context);
    }

}
