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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.params.azure.AzureDdosProtectionPlanParams;
import com.bocloud.common.annotations.Log;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BocloudResult;
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;

/**
 * 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(tags = {"CMC", "CSC"}, value = "获取DDOS防护计划列表", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "DDOS防护计划", operation = "获取DDOS防护计划列表")
    @GetMapping
    public BocloudResult list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.list(pager, context);
    }

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

    @ApiOperation(tags = {"CMC", "CSC"}, value = "修改DDOS防护计划", httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "DDOS防护计划", operation = "修改DDOS防护计划")
    @PutMapping("/{id}")
    public BocloudResult modify(@PathVariable(value = Common.ID) Long id, @RequestBody AzureDdosProtectionPlanParams params,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        DdosProtectionPlan plan = new DdosProtectionPlan();
        plan.setId(params.getId());
        plan.setTags(params.getTags());
        return service.modify(plan, context);
    }


    @ApiOperation(tags = {"CMC", "CSC"}, value = "删除DDOS防护计划", httpMethod = "DELETE")
    @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(tags = {"CMC", "CSC"}, value = "查看DDOS防护计划详情", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "DDOS防护计划", operation = "查看DDOS防护计划详情")
    @GetMapping("/{id}")
    public BocloudResult detail(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.detail(id, context);
    }

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

    @ApiOperation(tags = {"CMC", "CSC"}, value = "分配DDOS防护计划", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "DDOS防护计划", operation = "分配DDOS防护计划")
    @PostMapping("/{id}/users")
    public BocloudResult assign(@PathVariable(value = Common.ID) Long id, @RequestBody AzureDdosProtectionPlanParams params, @Value(Common.REQ_CONTEXT) RequestContext context) {
        if (null != params) {
            return service.assign(params, context);
        } else {
            return ResultTools.formatErrResult();
        }
    }


    @ApiOperation(tags = {"CMC", "CSC"}, value = "批量分配DDOS防护计划", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "DDOS防护计划", operation = "批量分配DDOS防护计划")
    @PostMapping("/users/batch")
    public BocloudResult assignBatch(@RequestBody AzureDdosProtectionPlanParams params, @Value(Common.REQ_CONTEXT) RequestContext context) {
        if (null != params) {
            return service.batchAssign(params, context);
        } else {
            return ResultTools.formatErrResult();
        }
    }

}
