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

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.ims.entity.resource.hmc.params.HmcServerCreateParams;
import com.bocloud.ims.entity.resource.hmc.params.HmcServerUpdateParams;
import com.bocloud.ims.interfaces.resource.hmc.HmcServerService;
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.Map;

/**
 * HMC服务器控制器(booter)
 *
 * @author sxp
 * @since May 22, 2018
 */
@RestController
@RequestMapping("/v1/hmc/servers")
@Api(value = "HMC服务器", tags = "HMC服务器")
public class HmcServerController {
    @Autowired
    private HmcServerService hmcServerService;

    /**
     * 查询HMC服务器列表
     */
    @Log(module = BoCloudService.IMS, target = "HMC服务器", operation = "查询")
    @RequestMapping(method = {RequestMethod.GET})
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询", httpMethod = "GET")
    public BocloudResult<GridBean> list(Pager pager) {
        return hmcServerService.list(pager);
    }

    /**
     * 查看HMC服务器详细信息
     *
     * @param id   HMC服务器ID
     * @param user 操作者
     * @return 操作结果
     */
    @Log(module = BoCloudService.IMS, target = "HMC服务器", operation = "详情")
    @RequestMapping(value = "/{id}", method = {RequestMethod.GET})
    @ApiOperation(tags = {"CMC", "CSC"}, value = "详情", httpMethod = "GET")
    public BocloudResult<Map<String, Object>> detail(@PathVariable(value = Common.ID) Long id) {
        return hmcServerService.detail(id);
    }

    /**
     * 修改HMC服务器信息
     *
     * @param id   HMC服务器ID
     * @param user 操作者
     * @return 操作结果
     */
    @Log(module = BoCloudService.IMS, target = "HMC服务器", operation = "修改")
    @RequestMapping(value = "/{id}", method = {RequestMethod.PUT})
    @ApiOperation(tags = {"CMC", "CSC"}, value = "修改", httpMethod = "PUT")
    public BocloudResult update(@PathVariable(value = Common.ID) Long id,
                                @RequestBody HmcServerUpdateParams params,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        params.setId(id);
        return hmcServerService.update(params, context);
    }

    /**
     * HMC服务器开机
     *
     * @param id   HMC服务器ID
     * @param user 操作用户
     * @return 操作结果
     * @author sxp
     * @since Jun 12, 2018
     */
    @Log(module = BoCloudService.IMS, target = "HMC服务器", operation = "开机、关机、删除连接、重置链接、监控")
    @RequestMapping(value = "/{id}", method = {RequestMethod.PATCH})
    @ApiOperation(tags = {"CMC", "CSC"}, value = "开机、关机、删除连接、重置链接、监控", httpMethod = "PATCH")
    public BocloudResult operate(@PathVariable(value = Common.ID) Long id,
                                 @RequestBody String action,
                                 @Value(Common.REQ_CONTEXT) RequestContext context) {
        JSONObject jsonObject = JSONObject.parseObject(action);
        action = jsonObject.getString("action");
        switch (action) {
            case "powerOn":
                return hmcServerService.powerOn(id, context);
            case "powerOff":
                return hmcServerService.powerOff(id, context);
            case "removeConnection":
                return hmcServerService.removeConnection(id, context);
            case "resetConnection":
                return hmcServerService.resetConnection(id, context);
            default:
                return new BocloudResult(false, "不支持的分区操作类型：" + action);
        }
    }

    /**
     * hmc平台新增受管系统
     *
     * @param params 物理主机属性信息的json字符串
     * @param user   操作用户
     * @return 添加结果
     * @author sxp
     * @since Jun 12, 2018
     */
    @Log(module = BoCloudService.IMS, target = "HMC云平台", operation = "创建")
    @RequestMapping(method = {RequestMethod.POST})
    @ApiOperation(tags = {"CMC", "CSC"}, value = "创建", httpMethod = "POST")
    public BocloudResult create(@RequestBody HmcServerCreateParams params,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return hmcServerService.create(params, context);
    }
}
