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

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.ddos.DdosProtectionPlan;
import com.bocloud.ims.interfaces.resource.azure.ddos.DdosProtectionPlanService;
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;

/**
 * azureDDOS防护计划访问
 *
 * @author wangyu
 * @version 5.0.0-SNAPSHOT
 * @since 2021/2/2
 */
@RestController
@RequestMapping("/v1/azure/ddos")
@Api(value = "DDOS防护计划管理管理", tags = "DDOS防护计划管理管理")
public class DdosProtectionPlanController {


    @Autowired
    private DdosProtectionPlanService service;

    @ApiOperation(value = "获取DDOS防护计划列表")
    @Log(module = BoCloudService.IMS, target = "DDOS防护计划", operation = "获取DDOS防护计划列表")
    @GetMapping
    public BocloudResult<GridBean<DdosProtectionPlan>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.list(pager, context);
    }

    @ApiOperation(value = "添加DDOS防护计划")
    @Log(module = BoCloudService.IMS, target = "DDOS防护计划", operation = "添加DDOS防护计划")
    @PostMapping
    public BocloudResult<DdosProtectionPlan> create(@RequestBody DdosProtectionPlan plan,
                                                    @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.create(plan, context);
    }

    @ApiOperation(value = "修改DDOS防护计划")
    @Log(module = BoCloudService.IMS, target = "DDOS防护计划", operation = "修改DDOS防护计划")
    @PutMapping("/{id}")
    public BocloudResult<DdosProtectionPlan> modify(@PathVariable(value = Common.ID) Long id, @RequestBody Map<String,String> map,
                                                    @Value(Common.REQ_CONTEXT) RequestContext context) {
        DdosProtectionPlan plan = new DdosProtectionPlan();
        plan.setId(id);
        plan.setTags(JSONObject.toJSONString(map));
        return service.modify(plan, context);
    }


    @ApiOperation(value = "删除DDOS防护计划")
    @Log(module = BoCloudService.IMS, target = "DDOS防护计划", operation = "删除DDOS防护计划")
    @DeleteMapping("/{id}")
    public BocloudResult remove(@PathVariable(value = Common.ID) Long id,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.remove(id, context);
    }

    @ApiOperation(value = "查看DDOS防护计划详情")
    @Log(module = BoCloudService.IMS, target = "DDOS防护计划", operation = "查看DDOS防护计划详情")
    @GetMapping("/{id}")
    public BocloudResult<DdosProtectionPlan> detail(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.detail(id, context);
    }

    @ApiOperation(value = "批量删除DDOS防护计划")
    @Log(module = BoCloudService.IMS, target = "DDOS防护计划", operation = "批量删除DDOS防护计划")
    @DeleteMapping
    public BocloudResult batchRemove(@RequestBody DeleteParam params,
                                     @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.batchRemove(params, context);
    }

    @ApiOperation(value = "分配DDOS防护计划")
    @Log(module = BoCloudService.IMS, target = "DDOS防护计划", operation = "分配DDOS防护计划")
    @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 service.assign(object, context);
        } else {
            return ResultTools.formatErrResult();
        }
    }


    @ApiOperation(value = "批量分配DDOS防护计划")
    @Log(module = BoCloudService.IMS, target = "DDOS防护计划", operation = "批量分配DDOS防护计划")
    @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);
            service.assign(paramsObject, context);
        }
        return new BocloudResult(true, "操作成功");
    }

}
