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

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.vm.model.IpScanParam;
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.params.IpPatchParams;
import com.bocloud.ims.entity.resource.Ip;
import com.bocloud.ims.interfaces.resource.IpService;
import io.lettuce.core.dynamic.annotation.Param;
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.Optional;

@RestController
@RequestMapping("/v1/ips")
@Api(value = "IP管理", tags = "IP管理")
public class IpController {

    @Autowired
    private IpService ipService;

    /**
     * 查看IP列表
     *
     * @param pager
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "查询IP列表", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "IP管理", operation = "查看IP列表")
    @GetMapping
    public BocloudResult<GridBean<Ip>> list(Pager pager) {
        pager = Optional.ofNullable(pager).orElse(new Pager());
        return ipService.list(pager);
    }

    /**
     * 根据condition查询IP列表
     *
     * @param condition
     * @param context
     * @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,
                                   @Value(Common.REQ_CONTEXT) RequestContext context) {
        return ipService.condition(condition, 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<Ip> detail(@PathVariable(value = Common.ID) Long id) {
        return ipService.detail(id);
    }

    /**
     * 删除IP
     *
     * @param id
     * @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) {
        return ipService.remove(id);
    }

    /**
     * 批量保存IP
     *
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "批量保存IP", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "IP管理", operation = "批量保存IP")
    @PostMapping
    public BocloudResult batchSave(@RequestParam(value = Common.PARAMS, required = true) String params,
                                   @Value(Common.REQ_CONTEXT) RequestContext context) {
        JSONObject object = JSONObject.parseObject(params);
        if (null == object) {
            return ResultTools.formatErrResult();
        }
        return ipService.batchSave(object, context);
    }

    /**
     * IP地址池扫描
     *
     * @param param
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "IP地址池扫描", httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "IP管理", operation = "IP地址池扫描")
    @PutMapping
    public BocloudResult scan(@RequestBody IpScanParam param, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return ipService.scan(param, context);
    }

    /**
     * IP操作
     *
     * @param id
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "IP地址操作", httpMethod = "PATCH")
    @Log(module = BoCloudService.IMS, target = "IP管理", operation = "IP操作")
    @PatchMapping("/{id}")
    public BocloudResult patch(@PathVariable(value = Common.ID) Long id,
                               @RequestBody IpPatchParams params,
                               @Value(Common.REQ_CONTEXT) RequestContext context) {
        return ipService.action(id, params.getAction(), params.getRemark(), context);
    }

    /**
     * IP锁定
     *
     * @param ipInfoString
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "IP锁定", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "IP管理", operation = "IP操作")
    @PostMapping("/preemptOrFreeIp")
    public BocloudResult preemptOrFreeIp(@RequestParam(value = "ipInfoString") String ipInfoString,
                               @Value(Common.REQ_CONTEXT) RequestContext context) {
        return ipService.preemptOrFreeIp(ipInfoString, context);
    }


    /**
     * IP批量操作
     *
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "IP批量操作", httpMethod = "PATCH")
    @Log(module = BoCloudService.IMS, target = "IP管理", operation = "IP批量操作")
    @PatchMapping
    public BocloudResult batchUpdate(@RequestBody IpPatchParams params, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return ipService.batchUpdate(params.getIds(), params.getAction(),params.getRemark(), context);
    }

    /**
     * 根据subnet删除ip,pms服务调用
     *
     * @param params
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "根据subnet删除ip", httpMethod = "PATCH")
    @Log(module = BoCloudService.IMS, target = "IP管理", operation = "根据subnet删除ip")
    @DeleteMapping("/subnets")
    public BocloudResult deleteBySubnetId(@RequestParam(value = Common.PARAMS) String params) {
        JSONObject object = JSONObject.parseObject(params);
        if (null == object) {
            return ResultTools.formatErrResult();
        }
        String ip = object.getString("ip");
        Long subnetId = object.getLong("subnetId");
        return ipService.deleteBySubnetId(ip, subnetId);
    }
}
