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

import com.bocloud.cmp.model.NetBean;
import com.bocloud.cmp.model.NetworkModel;
import com.bocloud.cmp.model.SwitchBean;
import com.bocloud.cmp.model.SwitchModel;
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.resource.*;
import com.bocloud.ims.interfaces.resource.NetworkService;
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
@Api(value = "网络", tags = "网络")
public class NetworkController {

    @Autowired
    private NetworkService networkService;

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

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

	/**
	 * 创建网络
	 *
	 * @param params
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "创建网络", httpMethod = "POST")
	@Log(module = BoCloudService.IMS, target = "网络", operation = "创建网络")
	@PostMapping("/v1/networks")
	public BocloudResult create(@RequestBody NetworkModel networkModel,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return networkService.create(networkModel, context, null);
	}

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

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "查看网络详情",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "网络", operation = "查看网络详情")
    @GetMapping("/v1/networks/{id}")
    public BocloudResult<Network> detail(@PathVariable(value = Common.ID) Long id) {
        return networkService.detail(id);
    }

    /**
     * 修改网络
     *
     * @param id
     * @param bean
     * @param context
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "修改网络信息",  httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "网络", operation = "修改网络")
    @PutMapping("/v1/networks/{id}")
    public BocloudResult modify(@PathVariable(value = Common.ID) Long id,
                                @RequestBody NetBean bean, @Value(Common.REQ_CONTEXT) RequestContext context) {
        bean.setId(id);
        return networkService.modify(bean, context);
    }

    /**
     * 查看云主机列表
     *
     * @param id
     * @param pager
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "查询网络下云主机列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "网络", operation = "查看云主机列表")
    @GetMapping("/v1/networks/{id}/vms")
    public BocloudResult<GridBean<CloudServer>> servers(@PathVariable(value = Common.ID) Long id, Pager pager) {
        return networkService.servers(pager, id);
    }

    /**
     * 查看宿主机列表
     *
     * @param id
     * @param pager
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "查询网络下主机列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "网络", operation = "查看宿主机列表")
    @GetMapping("/v1/networks/{id}/hosts")
    public BocloudResult<GridBean<Server>> hosts(@PathVariable(value = Common.ID) Long id, Pager pager) {
        return networkService.hosts(pager, id);
    }

    /**
     * 删除分布式交换机
     *
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "删除分布式交换机",  httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "网络", operation = "删除分布式交换机")
    @DeleteMapping("/v1/vswitches/{id}")
    public BocloudResult removeSwitch(@PathVariable(value = Common.ID) Long id,
                                      @RequestParam(value = "hostId", required = false) Long hostId,
                                      @Value(Common.REQ_CONTEXT) RequestContext context) {
        return networkService.removeSwitch(id, hostId, context);
    }

    /**
     * 创建分布式交换机
     *
     * @param model
     * @param context
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "创建分布式交换机",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "网络", operation = "创建分布式交换机")
    @PostMapping("/v1/vswitches")
    public BocloudResult createDSwitch(@RequestBody SwitchModel model,
                                       @Value(Common.REQ_CONTEXT) RequestContext context) {
        return networkService.createDSwitch(model, context);
    }

    /**
     * 修改分布式交换机
     *
     * @param id
     * @param bean
     * @param context
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "修改分布式交换机",  httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "网络", operation = "修改分布式交换机")
    @PutMapping("/v1/vswitches/{id}")
    public BocloudResult modifySwitch(@PathVariable(value = Common.ID) Long id,
                                      @RequestBody SwitchBean bean, @Value(Common.REQ_CONTEXT) RequestContext context) {
        bean.setId(id);
        return networkService.modifySwitch(bean, context);
    }

    /**
     * 查询交换机详情
     *
     * @param id
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "查询交换机详情",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "网络", operation = "查询交换机详情")
    @GetMapping("/v1/vswitches/{id}")
    public BocloudResult<Switcher> getSwitcher(@PathVariable(Common.ID) Long id) {
        return networkService.getSwitcher(id);
    }

    /**
     * 分布式交换机添加主机
     *
     * @param id
     * @param bean
     * @param context
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "分布式交换机修改添加主机",  httpMethod = "PATCH")
    @Log(module = BoCloudService.IMS, target = "网络", operation = "分布式交换机添加主机")
    @PatchMapping("/v1/vswitches/{id}/hosts")
    public BocloudResult addHost(@PathVariable(Common.ID) Long id, @RequestBody SwitchBean bean,
                                 @Value(Common.REQ_CONTEXT) RequestContext context) {
        bean.setId(id);
        return networkService.addHost(bean, context);
    }

    /**
     * 查询交换机绑定的物理网卡
     *
     * @param switcherId
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "查询交换机绑定的物理网卡",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "网络", operation = "查询交换机绑定的物理网卡")
    @GetMapping("/v1/networks/nics")
    public BocloudResult<List<NetworkCard>> listCardBySwitcher(@RequestParam(value = "switcherId") Long switcherId) {
        return networkService.listCardBySwitcher(switcherId);
    }

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

    /**
     * 查询网络下的mac地址
     *
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "查询网络下的mac地址",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "网络", operation = "查询网络下的mac地址")
    @GetMapping("/v1/networks/mac")
    public BocloudResult listMac(@RequestParam("vendorId") Long vendorId) {
        return networkService.listMac(vendorId);
    }


    /**
     * 创建端口组
     * @param vlanGroup
     * @param context
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "创建端口组",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "网络", operation = "创建端口组")
    @PostMapping("/v1/vlanGroup")
    public BocloudResult createVlanGroup(@RequestBody SangforVlanGroup vlanGroup,
                                       @Value(Common.REQ_CONTEXT) RequestContext context) {
        return networkService.createVlanGroup(vlanGroup, context);
    }

    /**
     * 修改端口组
     * @param id
     * @param vlanGroup
     * @param context
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "修改端口组",  httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "网络", operation = "修改端口组")
    @PutMapping("/v1/vlanGroup/{id}")
    public BocloudResult modifyVlanGroup(@PathVariable(value = Common.ID) Long id,
                                      @RequestBody SangforVlanGroup vlanGroup, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return networkService.modifyVlanGroup(vlanGroup, context);
    }

}
