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

import com.bocloud.cmp.model.VolumeModel;
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.Storage;
import com.bocloud.ims.interfaces.resource.StorageService;
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.*;

/**
 * 存储器管理Controller(cmp)
 *
 * @author lkx007
 */
@RestController
@RequestMapping("/v1/storages")
@Api(value = "存储设备", tags = "存储设备")
public class StorageController {
    @Autowired
    private StorageService storageService;

    @Log(module = BoCloudService.IMS, target = "存储设备", operation = "查询")
    @GetMapping
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询",  httpMethod = "GET")
    public BocloudResult<GridBean<Storage>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) throws Exception {
        return storageService.list(pager);
    }

    @Log(module = BoCloudService.IMS, target = "存储设备", operation = "创建")
    @PostMapping
    @ApiOperation(tags = {"CMC", "CSC"}, value = "创建",  httpMethod = "POST")
    public BocloudResult create(@RequestBody Storage storage,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return storageService.create(storage, context);
    }

    @Log(module = BoCloudService.IMS, target = "存储设备", operation = "修改")
    @PutMapping("/{id}")
    @ApiOperation(tags = {"CMC", "CSC"}, value = "修改",  httpMethod = "PUT")
    public BocloudResult modify(@RequestBody Storage storage,
                                @PathVariable(value = Common.ID) Long id,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        storage.setId(id);
        return storageService.modify(storage, context);
    }

    @Log(module = BoCloudService.IMS, target = "存储设备", operation = "连接")
    @PatchMapping
    @ApiOperation(tags = {"CMC", "CSC"}, value = "连接",  httpMethod = "PATCH")
    public BocloudResult connect(@RequestBody Storage storage,
                                 @Value(Common.REQ_CONTEXT) RequestContext context) {
        return storageService.connect(storage, context);
    }

    @Log(module = BoCloudService.IMS, target = "存储设备", operation = "删除")
    @DeleteMapping("/{id}")
    @ApiOperation(tags = {"CMC", "CSC"}, value = "删除",  httpMethod = "DELETE")
    public BocloudResult remove(@PathVariable(value = Common.ID, required = true) Long id,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return storageService.remove(id, context);
    }

    @Log(module = BoCloudService.IMS, target = "存储设备", operation = "查询可管理的卷")
    @GetMapping("/{id}/volumes")
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询可管理的卷",  httpMethod = "GET")
    public BocloudResult<GridBean<VolumeModel>> availableVolume(Pager pager) {
        return storageService.availableVolume(pager.getPage(), pager.getRows(), pager.getParams(), pager.getSorter(),
                pager.getSimple());
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Log(module = BoCloudService.IMS, target = "存储设备", operation = "详情")
    @GetMapping("/{id}")
    @ApiOperation(tags = {"CMC", "CSC"}, value = "详情",  httpMethod = "GET")
    public BocloudResult<Storage> detail(@PathVariable(value = Common.ID) Long id) {
        return storageService.detail(id);
    }

}
