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

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.model.DataStoreBean;
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.params.DataStoreUploadParam;
import com.bocloud.ims.entity.params.LabelParam;
import com.bocloud.ims.entity.resource.CloudServer;
import com.bocloud.ims.entity.resource.DataStore;
import com.bocloud.ims.entity.resource.Server;
import com.bocloud.ims.interfaces.resource.DataStoreService;
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.*;

@Api(value = "数据存储", tags = "数据存储")
@RestController
@RequestMapping("/v1/datastores")
public class DataStoreController {

    @Autowired
    private DataStoreService dataStoreService;

    /**
     * 查看数据存储列表
     *
     * @param pager
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "查询数据存储列表", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "数据存储", operation = "查看数据存储列表")
    @GetMapping
    public BocloudResult<GridBean<DataStore>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return dataStoreService.list(pager, context);
    }

    /**
     * 查看数据存储列表
     *
     * @param condition
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "通过condition方式查询数据存储列表", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "数据存储", operation = "查看数据存储列表")
    @GetMapping("/condition")
    public BocloudResult condition(@RequestParam(value = Common.CONDITION, required = false) String condition,
                                   @Value(Common.REQ_CONTEXT) RequestContext context) {
        return dataStoreService.condition(condition, context);
    }

    /**
     * 添加数据存储
     *
     * @param dataStore
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "创建数据存储", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "数据存储", operation = "添加数据存储")
    @PostMapping
    public BocloudResult<DataStore> create(@RequestBody DataStore dataStore, @Value(Common.REQ_CONTEXT) RequestContext context) {
        dataStore.setCreatorId(context.getTarget());
        dataStore.setMenderId(context.getTarget());
        return dataStoreService.create(dataStore, context);
    }

    /**
     * 修改数据存储
     *
     * @param id
     * @param bean
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "更新数据存储信息", httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "数据存储", operation = "修改数据存储")
    @PutMapping("/{id}")
    public BocloudResult<DataStore> modify(@PathVariable(value = Common.ID) Long id, @RequestBody DataStoreBean bean,
                                           @Value(Common.REQ_CONTEXT) RequestContext context) {
        bean.setId(id);
        return dataStoreService.modify(bean, context.getTarget());
    }

    /**
     * 移除
     *
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "删除数据存储存储", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "数据存储", operation = "移除数据存储")
    @DeleteMapping("/{id}")
    public BocloudResult remove(@PathVariable(value = Common.ID) Long id,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return dataStoreService.remove(id, context);
    }

    /**
     * 详细信息
     *
     * @param id 任务ID
     * @return 操作结果
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "查询数据存储详情", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "数据存储", operation = "查看数据存储详情")
    @GetMapping("/{id}")
    public BocloudResult<DataStore> detail(@PathVariable(value = Common.ID) Long id) {
        return dataStoreService.detail(id);
    }

    /**
     * 查看虚拟机列表
     *
     * @param id
     * @param pager
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "查询数据存储列表", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "数据存储", operation = "查看虚拟机列表")
    @GetMapping("/{id}/vms")
    public BocloudResult<GridBean<CloudServer>> servers(@PathVariable(value = Common.ID) Long id, Pager pager) {
        return dataStoreService.servers(pager, id);
    }

    /**
     * 查看宿主机列表
     *
     * @param id
     * @param pager
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "查询主机列表", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "数据存储", operation = "查看宿主机列表")
    @GetMapping("/{id}/hosts")
    public BocloudResult<GridBean<Server>> hosts(@PathVariable(value = Common.ID) Long id, Pager pager) {
        return dataStoreService.hosts(pager, id);
    }

    /**
     * 上传文件至数据存储
     *
     * @param id
     * @param param
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "上传文件至数据存储", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "数据存储", operation = "上传文件至数据存储")
    @PostMapping("/{id}/files")
    public BocloudResult uploadFile(@PathVariable(value = Common.ID) Long id,
                                    @RequestBody DataStoreUploadParam param,
                                    @Value(Common.REQ_CONTEXT) RequestContext context) {
        return dataStoreService.uploadFile(id, param.getFilePath(), context);
    }

    /**
     * 卸载数据存储
     *
     * @param id
     * @param hostId
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "卸载数据存储", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "数据存储", operation = "卸载数据存储")
    @DeleteMapping("/{id}/hosts")
    public BocloudResult unmount(@PathVariable(value = Common.ID) Long id, @RequestParam(value = "hostId") Long hostId,
                                 @Value(Common.REQ_CONTEXT) RequestContext context) {
        return dataStoreService.unmount(id, hostId, context.getTarget());
    }

    /**
     * 批量配置数据存储标签
     *
     * @param param
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "批量配置数据存储标签", httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "数据存储", operation = "批量配置数据存储标签")
    @PutMapping("/tags")
    public BocloudResult batchModifyLabel(@RequestBody LabelParam param,
                                          @Value(Common.REQ_CONTEXT) RequestContext context) {
        String params = null;
        if (null != param) {
            params = JSONObject.toJSONString(param);
        }
        return dataStoreService.batchModifyLabel(params, context);
    }

}
