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

import com.alibaba.fastjson.JSONObject;
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.LbMember;
import com.bocloud.ims.interfaces.resource.LbMemberService;
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
@Api(value = "负载均衡器成员", tags = "负载均衡器成员")
public class LbMemberController {

    @Autowired
    private LbMemberService lbMemberService;

    /**
     * 查看负载均衡成员列表
     *
     * @param pager
     * @param context
     * @return
     * @throws Exception
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "查询负载均衡成员列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "负载均衡器成员", operation = "查看负载均衡成员列表")
    @GetMapping("/v1/lb/members")
    public BocloudResult<GridBean<LbMember>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) throws Exception {
        return lbMemberService.list(pager, context);
    }

    /**
     * 创建负载均衡成员
     *
     * @param member
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "创建负载均衡成员",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "负载均衡器成员", operation = "创建负载均衡成员")
    @PostMapping("/v1/lb/members")
    public BocloudResult create(@RequestBody LbMember member, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return lbMemberService.create(member, context);
    }

    /**
     * 修改负载均衡成员
     *
     * @param id
     * @param member
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "修改负载均衡成员",  httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "负载均衡器成员", operation = "修改负载均衡成员")
    @PutMapping("/v1/lb/members/{id}")
    public BocloudResult modify(@PathVariable(value = Common.ID) Long id, @RequestBody LbMember member,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        member.setId(id);
        return lbMemberService.modify(member, context.getTarget());
    }

    /**
     * 查看负载均衡成员详情
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "查看负载均衡成员详情",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "负载均衡器成员", operation = "查看负载均衡成员详情")
    @GetMapping("/v1/lb/members/{id}")
    public BocloudResult<LbMember> detail(@PathVariable(value = Common.ID) Long id) {
        return lbMemberService.detail(id);
    }

    /**
     * 删除负载均衡成员
     *
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "移除负载均衡成员",  httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "负载均衡器成员", operation = "删除负载均衡成员")
    @DeleteMapping("/v1/lb/members/{id}")
    public BocloudResult remove(@PathVariable(value = Common.ID) Long id,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return lbMemberService.remove(id, context.getTarget());
    }

    /**
     * 添加或移除负载均衡成员
     *
     * @param id
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "管理负载均衡成员",  httpMethod = "PATCH")
    @Log(module = BoCloudService.IMS, target = "负载均衡器成员", operation = "添加或移除负载均衡成员")
    @PatchMapping("/v1/lb/pools/{id}/members")
    public BocloudResult manage(@PathVariable(value = Common.ID) Long id,
                                @RequestBody LbMember lbMember, @Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null != lbMember) {
			Long vendorId = lbMember.getVendorId();
			List<LbMember> addList = JSONObject.parseArray(
					lbMember.getAddObject() == null ? "" : lbMember.getAddObject().toJSONString(), LbMember.class);
			List<LbMember> delList = JSONObject.parseArray(
					lbMember.getDelObject() == null ? "" : lbMember.getDelObject().toJSONString(), LbMember.class);
			return lbMemberService.manage(addList, delList, context, vendorId, id, null);
		} else {
			return ResultTools.formatErrResult();
		}
    }
}
