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.HmcPartition;
import com.bocloud.ims.entity.resource.hmc.HmcVirtualDisk;
import com.bocloud.ims.entity.resource.hmc.params.*;
import com.bocloud.ims.interfaces.resource.hmc.HmcPartitionService;
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.List;

@RestController
@RequestMapping("/v1/hmc/partitions")
@Api(value = "HMC逻辑分区", tags = "HMC逻辑分区")
public class HmcPartitionController {
    @Autowired
    private HmcPartitionService hmcPartitionService;

    /**
     * 列表
     *
     * @param pager   分页参数
     * @param context 操作者信息
     * @return 数据结果集
     */
    @Log(module = BoCloudService.IMS, target = "HMC逻辑分区", operation = "查询")
    @RequestMapping(method = {RequestMethod.GET})
    @ApiOperation(tags={"CMC","CSC"},value = "查询",  httpMethod = "GET")
    public BocloudResult<GridBean<Long>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return hmcPartitionService.list(pager, context);
    }

    /**
     * 详细信息
     *
     * @param id
     * @return 操作结果
     */
    @Log(module = BoCloudService.IMS, target = "HMC逻辑分区", operation = "详情")
    @RequestMapping(value = "/{id}", method = {RequestMethod.GET})
    @ApiOperation(tags={"CMC","CSC"},value = "详情",  httpMethod = "GET")
    public BocloudResult<HmcPartition> detail(@PathVariable(value = Common.ID) Long id) {
        return hmcPartitionService.detail(id);
    }

    /**
     * 添加logical partition
     *
     * @param par
     * @return 添加结果
     */
    @Log(module = BoCloudService.IMS, target = "HMC逻辑分区", operation = "创建")
    @RequestMapping(method = {RequestMethod.POST})
    @ApiOperation(tags={"CMC","CSC"},value = "创建",  httpMethod = "POST")
    public BocloudResult create(@RequestBody HmcPartition par,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return hmcPartitionService.create(par, context);
    }

    /**
     * 修改logical partition
     *
     * @param params 逻辑分区属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
     * @param id     需要修改的逻辑分区ID
     */
    @Log(module = BoCloudService.IMS, target = "HMC逻辑分区", operation = "修改")
    @RequestMapping(value = "/{id}", method = {RequestMethod.PUT})
    @ApiOperation(tags={"CMC","CSC"},value = "修改",  httpMethod = "PUT")
    public BocloudResult modify(@RequestBody HmcPartition partition,
                                @PathVariable(value = Common.ID) Long id,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return hmcPartitionService.modify(partition, context);
    }

    /**
     * 删除lpar
     *
     * @param id HMC逻辑分区 ID
     * @return 操作结果
     */
    @Log(module = BoCloudService.IMS, target = "HMC逻辑分区", operation = "删除")
    @RequestMapping(value = "/{id}", method = {RequestMethod.DELETE})
    @ApiOperation(tags={"CMC","CSC"},value = "删除",  httpMethod = "DELETE")
    public BocloudResult remove(@RequestBody HmcPartitionRemoveParams params,
                                @PathVariable(value = Common.ID) Long id,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        List<Long> vds = JSONObject.parseArray(params.getVds(), Long.class);
        List<Long> vfcs = JSONObject.parseArray(params.getVfcs(), Long.class);
        return hmcPartitionService.remove(id, vds, vfcs, context);
    }

    /**
     * HMC逻辑分区操作
     *
     * @param id      HMC逻辑分区ID
     * @param context 操作用户
     * @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 hmcPartitionService.powerOn(id, context);
            case "powerOff":
                return hmcPartitionService.powerOff(id, context);
            case "restart":
                return hmcPartitionService.restart(id, context);
            default:
                return new BocloudResult(false, "不支持的分区操作类型：" + action);
        }
    }

    // 2019-08-15 : 去掉自动获取槽号的相关功能，这个接口不需要了。暂时屏蔽。 详见PBSM-5039
    // /**
    // * 获取逻辑分区空闲的槽号
    // *
    // * @param id 逻辑分区id
    // * @return 操作结果
    // */
    // @Log(module = BoCloudService.IMS, target = "HMC逻辑分区", operation = "获取逻辑分区空闲的槽号")
    // @RequestMapping(value = "/{id}/empty/slots", method = {RequestMethod.GET})
    // public BocloudResult getEmptyAdapterId(@PathVariable(value = Common.ID) Long
    // id,
    // @RequestParam(value = Common.PARAMS, required = false) String params) {
    // return
    // hmcPartitionService.getEmptyAdapterId(id,JSON.parseObject(params).getJSONArray("slots").toJavaList(Integer.class));
    // }

    /**
     * 获取虚拟scsi磁盘
     *
     * @return 操作结果
     */
    @Log(module = BoCloudService.IMS, target = "HMC逻辑分区", operation = "获取虚拟scsi/vfc磁盘")
    @RequestMapping(value = "/{id}/disks", method = {RequestMethod.GET})
    @ApiOperation(tags={"CMC","CSC"},value = "获取虚拟scsi/vfc磁盘",  httpMethod = "GET")
    public BocloudResult<List<HmcVirtualDisk>> getDisks(@RequestParam(value = "adapterId") Integer adapterId,
                                                        @PathVariable(value = Common.ID) Long id,
                                                        @Value(Common.REQ_CONTEXT) RequestContext context) {
        return hmcPartitionService.getScsiDisk(id, adapterId);
    }

    /**
     * 为逻辑分区创建VG
     * @param params
     * @param id
     * @param context
     * @return
     */
    @Log(module = BoCloudService.IMS, target = "HMC逻辑分区", operation = "在AIX操作系统中创建VG")
    @RequestMapping(value = "/{id}/vg", method = {RequestMethod.POST})
    @ApiOperation(tags={"CMC","CSC"},value = "在AIX操作系统中创建VG",  httpMethod = "POST")
    public BocloudResult createVG(@RequestBody HmcPartitionCreateVGParams params,
                                  @PathVariable(value = Common.ID) Long id,
                                  @Value(Common.REQ_CONTEXT) RequestContext context) {
        params.setPartitionId(id);
        return hmcPartitionService.createVG(params, context);
    }

    /**
     * 为逻辑分区删除VG
     * @param params
     * @param id
     * @param context
     * @return
     */
    @Log(module = BoCloudService.IMS, target = "HMC逻辑分区", operation = "在AIX操作系统中删除VG")
    @RequestMapping(value = "/{id}/vg", method = {RequestMethod.DELETE})
    @ApiOperation(tags={"CMC","CSC"},value = "在AIX操作系统中删除VG",  httpMethod = "DELETE")
    public BocloudResult removeVG(@RequestBody HmcPartitionRemoveVGParams params,
                                  @PathVariable(value = Common.ID) Long id,
                                  @Value(Common.REQ_CONTEXT) RequestContext context) {
        params.setPartitionId(id);
        return hmcPartitionService.removeVG(params, context);
    }

    /**
     * 为逻辑分区创建LV
     * @param params
     * @param id
     * @param context
     * @return
     */
    @Log(module = BoCloudService.IMS, target = "HMC逻辑分区", operation = "在AIX操作系统中创建LV")
    @RequestMapping(value = "/{id}/lv", method = {RequestMethod.POST})
    @ApiOperation(tags={"CMC","CSC"},value = "在AIX操作系统中创建LV",  httpMethod = "POST")
    public BocloudResult createLV(@RequestBody HmcPartitionCreateLVParams params,
                                  @PathVariable(value = Common.ID) Long id,
                                  @Value(Common.REQ_CONTEXT) RequestContext context) {
        params.setPartitionId(id);
        return hmcPartitionService.createLV(params, context);
    }

    /**
     * 为逻辑分区删除LV
     * @param params
     * @param id
     * @param context
     * @return
     */
    @Log(module = BoCloudService.IMS, target = "HMC逻辑分区", operation = "在AIX操作系统中删除LV")
    @RequestMapping(value = "/{id}/lv", method = {RequestMethod.DELETE})
    @ApiOperation(tags={"CMC","CSC"},value = "在AIX操作系统中删除LV",  httpMethod = "DELETE")
    public BocloudResult removeLV(@RequestBody HmcPartitionRemoveLVParams params,
                                  @PathVariable(value = Common.ID) Long id,
                                  @Value(Common.REQ_CONTEXT) RequestContext context) {
        params.setPartitionId(id);
        return hmcPartitionService.removeLV(params, context);
    }



    /**
     * 为逻辑分区创建高可用
     * @param params
     * @param context
     * @return
     */
    @Log(module = BoCloudService.IMS, target = "HMC逻辑分区", operation = "在AIX操作系统中创建高可用")
    @ApiOperation(tags={"CMC","CSC"},value = "在AIX操作系统中创建高可用",  httpMethod = "POST")
    @RequestMapping(value = "/ha", method = {RequestMethod.POST})
    public BocloudResult ha(@RequestBody HmcPartitionHaParams params,
                            @Value(Common.REQ_CONTEXT) RequestContext context) {
        return hmcPartitionService.ha(params, context);
    }


}
