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

import com.bocloud.cmp.model.VpcOperationParam;
import com.bocloud.cmp.params.azure.VpcWithDdosParams;
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.ResultTools;
import com.bocloud.ims.entity.resource.CidrBlock;
import com.bocloud.ims.entity.resource.Vpc;
import com.bocloud.ims.interfaces.resource.VpcService;
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/vpcs")
@Api(value = "VPC管理", tags = "VPC管理")
public class VpcController {

    @Autowired
    private VpcService vpcService;

    /**
     * 查询vpc列表
     *
     * @param pager
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "VPC列表查询", httpMethod = "GET")
    @GetMapping
    public BocloudResult<GridBean<Vpc>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return vpcService.list(pager, context);
    }

    /**
     * 通过condition方式查询vpc列表
     *
     * @param condition
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "通过condition方式查询vpc列表", httpMethod = "GET")
    @GetMapping("/condition")
    public BocloudResult<List<Vpc>> condition(
            @RequestParam(value = Common.CONDITION, required = false) String condition,
            @Value(Common.REQ_CONTEXT) RequestContext context) {
        return vpcService.condition(condition, context);
    }

    /**
     * 保存vpc
     *
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "保存VPC", httpMethod = "POST")
    @PostMapping
    public BocloudResult<String> create(@RequestBody Vpc vpc, @Value(Common.REQ_CONTEXT) RequestContext context) {
        if (null != vpc) {
            return vpcService.create(vpc, context);
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 删除vpc
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "删除VPC", httpMethod = "DELETE")
    @DeleteMapping(value = "/{id}")
    public BocloudResult<String> remove(@PathVariable(value = Common.ID) Long id,
                                        @Value(Common.REQ_CONTEXT) RequestContext context) {
        return vpcService.remove(id, context);
    }

    /**
     * 批量操作
     *
     * @param params
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "VPC批量删除操作", httpMethod = "PATCH")
    @DeleteMapping
    public BocloudResult<String> batchRemove(@RequestBody List<Long> ids,
                                             @Value(Common.REQ_CONTEXT) RequestContext context) {
        return vpcService.batchRemove(ids, context);
    }

    /**
     * 更新vpc
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "更新VPC", httpMethod = "PUT")
    @PutMapping(value = "/{id}")
    public BocloudResult<String> modify(@PathVariable(value = Common.ID) Long id, @RequestBody Vpc vpc,
                                        @Value(Common.REQ_CONTEXT) RequestContext context) {
        if (null != vpc) {
            return vpcService.modify(vpc, context);
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 查询vpc详情
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询VPC详情", httpMethod = "GET")
    @GetMapping(value = "/{id}")
    public BocloudResult<Vpc> detail(@PathVariable(value = Common.ID) Long id) {
        return vpcService.detail(id);
    }

    /**
     * vpc操作类
     *
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "vpc操作", httpMethod = "PATCH")
    @PatchMapping(value = "/{id}/operation")
    public BocloudResult operation(@PathVariable(value = Common.ID) Long id,
                                   @Value(Common.REQ_CONTEXT) RequestContext context, @RequestBody VpcOperationParam vpcOperationParam) {
        return vpcService.operations(id, vpcOperationParam, context);
    }

    /**
     * 查询vpc下cidr
     *
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "VPC-cidr列表查询", httpMethod = "GET")
    @GetMapping(value = "/cidr")
    public BocloudResult<GridBean<CidrBlock>> listCidr(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return vpcService.cidrs(pager, context);
    }

    @ApiOperation(tags = {"CMC", "CSC"}, value = "VPC分配DDOS", httpMethod = "POST")
    @PostMapping("/{id}/ddos")
    public BocloudResult withDdos(@PathVariable(value = Common.ID) Long id, @RequestBody VpcWithDdosParams params,
                                  @Value(Common.REQ_CONTEXT) RequestContext context) {
        return vpcService.withDdos(id, params.getPlanId(), context);
    }

    @ApiOperation(tags = {"CMC", "CSC"}, value = "VPC取消分配DDOS", httpMethod = "DELETE")
    @DeleteMapping("/{id}/ddos")
    public BocloudResult withoutDdos(@PathVariable(value = Common.ID) Long id,
                                     @Value(Common.REQ_CONTEXT) RequestContext context) {
        return vpcService.withoutDdos(id, context);
    }

}
