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

import com.bocloud.cmp.model.RouterBean;
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.ims.entity.params.AttachSubnetParam;
import com.bocloud.ims.entity.params.RouterActionParam;
import com.bocloud.ims.entity.resource.Router;
import com.bocloud.ims.entity.resource.Subnet;
import com.bocloud.ims.interfaces.resource.RouterService;
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/routers")
@Api(value = "路由", tags = "路由")
public class RouterController {

    @Autowired
    private RouterService routeService;

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

    /**
     * 根据condition查询路由列表
     *
     * @param pager
     * @param condition
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "根据condition查询路由列表", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "路由", operation = "查看路由列表")
    @GetMapping("/condition")
    public BocloudResult condition(Pager pager, @RequestParam(value = Common.CONDITION, required = false) String condition,
                                   @Value(Common.REQ_CONTEXT) RequestContext context) {
        return routeService.routers(condition, context);
    }


    /**
     * 创建路由
     *
     * @param router
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "创建路由", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "路由", operation = "创建路由")
    @PostMapping
    public BocloudResult<Router> create(@RequestBody Router router,
                                        @Value(Common.REQ_CONTEXT) RequestContext context) {
        return routeService.create(router, context);
    }

    /**
     * 删除
     *
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "删除路由", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "路由", operation = "删除路由")
    @DeleteMapping(value = "/{id}")
    public BocloudResult<String> remove(@PathVariable(value = Common.ID) Long id,
                                        @Value(Common.REQ_CONTEXT) RequestContext context) {
        return routeService.remove(id, context);
    }

    /**
     * 修改
     *
     * @param bean
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "修改路由", httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "路由", operation = "修改路由")
    @PutMapping(value = "/{id}")
    public BocloudResult<Router> modify(@RequestBody RouterBean bean,
                                        @PathVariable(value = Common.ID) Long id, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return routeService.modify(bean, context);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询机房列表", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "路由", operation = "查看路由详情")
    @GetMapping(value = "/{id}")
    public BocloudResult<Router> detail(@PathVariable(value = Common.ID) Long id) {
        return routeService.detail(id);
    }

    /**
     * 查询路由下的子网
     *
     * @param pager
     * @param condition
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询路由列表", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "路由", operation = "路由连接子网")
    @GetMapping("/subnets")
    public BocloudResult<List<Subnet>> listSubnets(Pager pager,
                                                   @RequestParam(value = Common.CONDITION, required = false) String condition,
                                                   @Value(Common.REQ_CONTEXT) RequestContext context) {
        return routeService.listSubnets(pager, condition, context);
    }

    /**
     * 连接子网
     *
     * @param id
     * @param param
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "路由连接子网", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "路由", operation = "路由连接子网")
    @PostMapping("/{id}/subnets")
    public BocloudResult<String> attach(@PathVariable(value = Common.ID) Long id,
                                        @RequestBody AttachSubnetParam param,
                                        @Value(Common.REQ_CONTEXT) RequestContext context) {
        return routeService.attach(id, param.getSubnetId(), param.getAddress(), param.getPortId(), context);
    }

    /**
     * 断开子网
     *
     * @param id
     * @param subnetId
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "路由断开子网", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "路由", operation = "路由断开子网")
    @DeleteMapping("/{id}/subnets")
    public BocloudResult<String> detach(@PathVariable(value = Common.ID) Long id,
                                        @RequestParam(value = "subnetId") Long subnetId,
                                        @Value(Common.REQ_CONTEXT) RequestContext context) {
        return routeService.detach(id, subnetId, context);
    }

    /**
     * 设置网关 / 清除网关
     *
     * @param id
     * @param param
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "设置路由网关", httpMethod = "PATCH")
    @Log(module = BoCloudService.IMS, target = "路由", operation = "路由设置网关")
    @PatchMapping("/{id}")
    public BocloudResult<String> operation(@PathVariable(value = Common.ID) Long id,
                                           @RequestBody RouterActionParam param,
                                           @Value(Common.REQ_CONTEXT) RequestContext context) {
        return routeService.operation(id, param.getNetworkId(), param.getAction(), null == param.getParams() ? null : param.getParams().toJSONString(), context);
    }

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

}
