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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.model.f5.MonitorModel;
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.f5.BiMonitor;
import com.bocloud.ims.interfaces.resource.f5.F5BiMonitorService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "BiMonitor", tags = "BiMonitor")
@RestController
@RequestMapping("/v1/f5/monitors")
public class F5BiMonitorController {
    @Autowired
    private F5BiMonitorService f5BiMonitorService;

    /**
     * 列表
     *
     * @param pager
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查看BiMonitor列表", httpMethod = "GET")
    @GetMapping
    public BocloudResult<GridBean<BiMonitor>> list(Pager pager) {
        return f5BiMonitorService.list(pager);
    }

    /**
     * 添加
     *
     * @param monitorModel
     * @param context
     * @return 添加结果
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "添加BiMonitor", httpMethod = "POST")
    @PostMapping
    public BocloudResult create(@RequestBody MonitorModel monitorModel,
        @Value(Common.REQ_CONTEXT) RequestContext context) {
        if (null != monitorModel) {
            BiMonitor biMonitor = JSONObject.parseObject(
                JSONObject.parseObject(JSONObject.toJSONString(monitorModel)).toJSONString(), BiMonitor.class);
            return f5BiMonitorService.create(biMonitor, context.getTarget(), monitorModel);
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 修改
     *
     * @param monitorModel
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "修改BiMonitor", httpMethod = "PUT")
    @PutMapping(value = "/{id}")
    public BocloudResult modify(@RequestBody MonitorModel monitorModel, @PathVariable(value = Common.ID) Long id,
        @Value(Common.REQ_CONTEXT) RequestContext context) {
        if (null != monitorModel) {
            monitorModel.setId(id);
            BiMonitor biMonitor = JSONObject.parseObject(
                JSONObject.parseObject(JSONObject.toJSONString(monitorModel)).toJSONString(), BiMonitor.class);
            return f5BiMonitorService.modify(biMonitor, context.getTarget(), monitorModel);
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 移除
     *
     * @param id
     * @param context
     * @return 操作结果
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "删除BiMonitor", httpMethod = "DELETE")
    @DeleteMapping(value = "/{id}")
    public BocloudResult remove(@PathVariable(value = Common.ID) Long id,
        @Value(Common.REQ_CONTEXT) RequestContext context) {
        return f5BiMonitorService.remove(id, context.getTarget());
    }

    /**
     * 详细信息
     *
     * @param id
     * @return 操作结果
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询BiMonitor详情", httpMethod = "GET")
    @GetMapping(value = "/{id}")
    public BocloudResult<BiMonitor> detail(@PathVariable(value = Common.ID) Long id) {
        return f5BiMonitorService.detail(id);
    }

}
