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

import com.bocloud.cmp.params.azure.UserAssignParams;
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.ims.entity.resource.azure.network.PublicIPAddress;
import com.bocloud.ims.interfaces.resource.azure.network.PublicIPAddressService;
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.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "公共IP地址", tags = "公共IP地址")
@RestController
@RequestMapping("/v1/publicIps/addresses")
public class PublicIPAddressController {

    @Autowired
    private PublicIPAddressService service;

    /**
     * 列表
     *
     * @param pager
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "列表公共IP地址", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "公共IP地址", operation = "列表公共IP地址")
    @GetMapping
    public BocloudResult list(Pager pager,
                              @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.list(pager, context);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "详情公共IP地址", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "公共IP地址", operation = "详情公共IP地址")
    @GetMapping("/{id}")
    public BocloudResult detail(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.detail(id, context);
    }

    /**
     * 创建
     *
     * @param entityParams
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "创建公共IP地址", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "公共IP地址", operation = "创建公共IP地址")
    @PostMapping
    public BocloudResult create(@RequestBody PublicIPAddress entityParams,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.create(entityParams, context);
    }


    /**
     * 批量创建
     *
     * @param entityParams
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "批量创建公共IP地址", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "公共IP地址", operation = "批量创建公共IP地址")
    @PostMapping("/batch")
    public BocloudResult createBatch(@RequestBody PublicIPAddress entityParams,
                                     @RequestParam(value = "num") Integer num,
                                     @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.createBatch(entityParams, num, context);
    }


    /**
     * 删除
     *
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "删除公共IP地址", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "公共IP地址", operation = "删除公共IP地址")
    @DeleteMapping("/{id}")
    public BocloudResult remove(@PathVariable(value = Common.ID) Long id,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.remove(id, context);
    }


    /**
     * 删除
     *
     * @param context
     * @return
     */
    @ApiOperation(value = "删除", response = BocloudResult.class, httpMethod = "DELETE", produces = MediaType.APPLICATION_JSON_VALUE)
    @Log(module = BoCloudService.IMS, target = "公共IP地址", operation = "删除公共IP地址")
    @DeleteMapping
    public BocloudResult removeBatch(@RequestParam(value = "ids", required = false) List<Long> ids,
                                     @Value(Common.REQ_CONTEXT) RequestContext context) {
        for (Long id : ids) {
            service.remove(id, context);
        }
        return new BocloudResult(true, "操作成功");
    }

    /**
     * 查询未使用的公共ip
     *
     * @param context
     * @return
     */
    @ApiOperation(value = "查询未使用的公共ip", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @Log(module = BoCloudService.IMS, target = "公共IP地址", operation = "查询未使用的公共ip")
    @GetMapping("/unused")
    public BocloudResult listUnuse(@RequestParam(value = Common.VENDORID) Long vendorId,
                                   @RequestParam(value = Common.REGIONID) String regionId,
                                   @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.listUnuse(vendorId, regionId, context);
    }

    /**
     * 用户分配
     *
     * @param id
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "用户分配公共IP地址", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "公共IP地址", operation = "用户分配公共IP地址")
    @PostMapping("/{id}/users")
    public BocloudResult assign(@PathVariable(value = Common.ID) Long id,
                                @RequestBody UserAssignParams params,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.assign(params, context);
    }

    /**
     * 批量用户分配
     *
     * @param
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "批量用户分配公共IP地址", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "公共IP地址", operation = "批量用户分配公共IP地址")
    @PostMapping("/users/batch")
    public BocloudResult assignBatch(
            @RequestBody UserAssignParams params,
            @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.assignBatch(params, context);
    }

}
