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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.model.FloatingIpPortParam;
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.ResultTools;
import com.bocloud.ims.entity.resource.FloatingIp;
import com.bocloud.ims.entity.resource.Port;
import com.bocloud.ims.interfaces.resource.FloatingIpService;
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;

@RestController
@RequestMapping("/v1/floatips")
@Api(value = "浮动IP", tags = "浮动IP")
public class FloatingIpController {

    @Autowired
    private FloatingIpService floatingIpService;

    /**
     * 查看浮动IP列表
     * @param pager
     * @param condition
     * @param context
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "查询浮动IP列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "浮动IP", operation = "查看浮动IP列表")
    @GetMapping
    public BocloudResult<GridBean<FloatingIp>> list(Pager pager, @RequestParam(value = Common.CONDITION, required = false) String condition,
                                                          @Value(Common.REQ_CONTEXT) RequestContext context) {
        return floatingIpService.list(pager, context);
    }

    /**
     * 根据condition查询浮动IP列表
     * @param condition
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "根据condition查询浮动IP列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "浮动IP", operation = "查看浮动IP列表")
    @GetMapping("/condition")
    public BocloudResult condition(@RequestParam(value = Common.CONDITION, required = false) String condition) {
        return floatingIpService.condition(condition);
    }

    /**
     * 创建浮动IP
     *
     * @param fIp
     * @param context
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "创建浮动IP",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "浮动IP", operation = "创建浮动IP")
    @PostMapping
    public BocloudResult<FloatingIp> create(@RequestBody FloatingIp fIp,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return floatingIpService.create(fIp, context);
    }

    /**
     * 删除浮动IP
     *
     * @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 floatingIpService.remove(id, context);
    }

    /**
     * 查看浮动IP详情
     *
     * @param id
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "浮动IP详情",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "浮动IP", operation = "查看浮动IP详情")
    @GetMapping("/{id}")
    public BocloudResult<FloatingIp> detail(@PathVariable(value = Common.ID) Long id) {
        return floatingIpService.detail(id);
    }

	/**
	 * 关联浮动IP
	 *
	 * @param id
	 * @param floatingIpPortParam
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "关联浮动IP", httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "浮动IP", operation = "关联浮动IP")
	@PostMapping("/{id}/ports")
	public BocloudResult associate(@PathVariable(value = Common.ID) Long id,
			@RequestBody FloatingIpPortParam floatingIpPortParam, @Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null == floatingIpPortParam) {
			return ResultTools.formatErrResult();
		}
		floatingIpPortParam.setId(id);
		return floatingIpService.associate(JSON.toJSONString(floatingIpPortParam), context);
	}

    /**
     * 解绑
     *
     * @param id
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "解绑浮动IP",  httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "浮动IP", operation = "解绑浮动IP")
    @DeleteMapping("/{id}/ports")
    public BocloudResult disassociate(@PathVariable(value = Common.ID) Long id,
                                      @Value(Common.REQ_CONTEXT) RequestContext context) {
        return floatingIpService.disassociate(id, context);
    }

    /**
     * 可用端口
     *
     * @param id
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "查询可用端口",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "浮动IP", operation = "查询可用端口")
    @GetMapping("/{id}/ports")
    public BocloudResult<List<Port>> ports(@PathVariable(value = Common.ID) Long id,
                                           @Value(Common.REQ_CONTEXT) RequestContext context) {
        return floatingIpService.ports(id, context);
    }

	/**
	 * 批量删除浮动IP
	 *
	 * @param params
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "浮动IP批量删除", httpMethod = "DELETE")
	@Log(module = BoCloudService.IMS, target = "浮动IP", operation = "批量删除浮动IP")
	@DeleteMapping
	public BocloudResult batchRemove(@RequestBody List<Long> ids, @Value(Common.REQ_CONTEXT) RequestContext context) {
		return floatingIpService.batchRemove(ids, context);
	}

}
