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

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.JSONTools;
import com.bocloud.common.utils.ResultTools;
import com.bocloud.ims.interfaces.resource.f5.F5ApplyService;
import com.bocloud.ims.interfaces.resource.f5.F5BiVirtualServerService;
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.Map;

/**
 * f5资源申请
 * <p>
 * Created by liuyuanyuan on 2019/5/8 9:49
 */
@Api(value = "f5资源申请", tags = "f5资源申请")
@RestController
@RequestMapping("/v1/f5")
public class F5ApplyController {
    @Autowired
    private F5ApplyService f5ApplyService;
    @Autowired
    private F5BiVirtualServerService f5BiVirtualServerService;

    /**
     * f5资源申请
     *
     * @param params
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "申请f5资源", httpMethod = "POST")
    @PostMapping
    public BocloudResult applyBigIp(@RequestParam(value = Common.PARAMS) String params,
                                    @Value(Common.REQ_CONTEXT) RequestContext context) {
        JSONObject applyResourceData = JSONTools.isJSONObj(params);
        if (null != applyResourceData) {
            BocloudResult bocloudResult = new BocloudResult();
            String applyCategory = applyResourceData.getString("resourceCategory");
            Long instanceId = applyResourceData.getLong("outstandingIssuesId");
            switch (applyCategory) {
                case "ResourceF5Apply":
                    bocloudResult = f5ApplyService.applyBigIp(applyResourceData, instanceId, context);
                    break;
                case "F5ExpansionApply":
                    bocloudResult = f5ApplyService.applyExpansion(applyResourceData, instanceId, context);
                    break;
                default:
                    break;
            }
            return bocloudResult;
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * F5扩容申请列表
     *
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "F5扩容申请列表", httpMethod = "GET")
    @GetMapping
    public BocloudResult<GridBean<Map<String, Object>>> expansionApplyList(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return f5BiVirtualServerService.expansionApplyList(pager, context);
    }
}
