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

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.model.NetworkInterfaceModel;
import com.bocloud.cmp.params.azure.NetworkInterfaceOperationParams;
import com.bocloud.cmp.params.azure.NicChangeSubnetParams;
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.NetworkInterface;
import com.bocloud.ims.interfaces.resource.azure.NetworkInterfaceService;
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.*;

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: NetworkInterfacesController
 * @Description:网络接口
 * @author: zhenpingLi/李振平
 * @date: 2019/9/29 15:05
 */
@Api(value = "网络接口", tags = "网络接口管理")
@RestController
@RequestMapping("/v1/azure/nics")
public class NetworkInterfacesController {

    @Autowired
    private NetworkInterfaceService service;

    /**
     * 查询网络接口列表
     *
     * @param pager
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询网络接口列表", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "网络接口", operation = "查询网络接口列表")
    @GetMapping
    public BocloudResult<GridBean<NetworkInterface>> list(Pager pager,
                                                          @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.list(pager, context);
    }

    /**
     * 查询网络接口详情
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询网络接口详情", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "网络接口", operation = "查询网络接口详情")
    @GetMapping("/{id}")
    public BocloudResult<NetworkInterface> detail(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.detail(id, context);
    }

    /**
     * 创建
     *
     * @param
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "创建网络接口", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "网络接口", operation = "创建网络接口")
    @PostMapping
    public BocloudResult create(@RequestBody NetworkInterface entityParams,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.create(entityParams, context);
    }

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

    /**
     * 更换子网
     *
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "更换子网", httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "网络接口", operation = "网络接口更换子网")
    @PutMapping(value = "/{id}/subnets")
    public BocloudResult changeSubnet(@PathVariable(value = Common.ID) Long id,
                                      @RequestBody NicChangeSubnetParams params,
                                      @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.changeSubnet(params, context);
    }

    /**
     * 网络接口相关操作
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "网络接口操作", httpMethod = "PATCH")
    @Log(module = BoCloudService.IMS, target = "网络接口", operation = "网络接口相关操作")
    @PatchMapping("/{id}")
    public BocloudResult operations(@PathVariable(value = Common.ID) Long id,
                                    @Value(Common.REQ_CONTEXT) RequestContext context,
                                    @RequestBody NetworkInterfaceOperationParams params) {
        return service.operations(params, context);
    }

    /**
     * 修改网络接口
     *
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "修改网络接口", httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "网络接口", operation = "修改网络接口")
    @PutMapping(value = "/{id}")
    public BocloudResult modify(@PathVariable(value = Common.ID) Long id,
                                @RequestBody NetworkInterface entityParams,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.modify(entityParams, context);
    }

    /**
     * 分配安全组
     *
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "分配安全组", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "网络接口", operation = "分配安全组")
    @PostMapping(value = "/{id}/sgroups")
    public BocloudResult sgroups(@PathVariable(value = Common.ID) Long id,
                                 @RequestBody NetworkInterfaceModel model,
                                 @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.distributionSecurityGroup(id, model, context);
    }

    /**
     * 批量操作
     *
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "批量删除", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "网络接口", operation = "批量删除")
    @DeleteMapping
    public BocloudResult batchRemove(@RequestParam(value = Common.PARAMS) String params,
                                     @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.batchRemove(params, context);
    }

	@ApiOperation(value = "查询可绑定网卡", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@Log(module = BoCloudService.IMS, target = "弹性网卡", operation = "查询可绑定网卡")
	@GetMapping("/availableNics")
	public BocloudResult listAvailableNics(@RequestParam(value = Common.VENDORID, required = true) Long vendorId,
			@RequestParam(value = Common.REGIONID, required = true) String regionId,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return service.listAvailableNics(vendorId, regionId);
	}

	@ApiOperation(value = "条件查询网卡列表", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@Log(module = BoCloudService.IMS, target = "弹性网卡", operation = "条件查询网卡列表")
	@GetMapping("/list")
	public BocloudResult list(@RequestParam(value = Common.VENDORID, required = false) Long vendorId,
			@RequestParam(value = Common.REGIONID, required = false) String regionId,
			@RequestParam(value = "vpcId", required = false) Long vpcId,
			@RequestParam(value = "interfaceType", required = false) String interfaceType,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return service.list(vendorId, regionId, vpcId, interfaceType);
	}

    /***
     * 辅助私网IP
     *
     * @param id
     * @param action
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(value = "辅助私网IP", response = BocloudResult.class, httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @Log(module = BoCloudService.IMS, target = "弹性网卡", operation = "辅助私网IP")
    @PostMapping(value = "/{id}/privateIp")
    public BocloudResult privateIp(@PathVariable(value = Common.ID) Long id,
                                   @RequestBody NetworkInterfaceModel networkInterfaceModel,
                                   @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.privateIpAddresses(id, networkInterfaceModel, context);
    }

}
