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

import com.bocloud.cmp.entity.enums.CloudProvider;
import com.bocloud.cmp.model.VdcBean;
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.Server;
import com.bocloud.ims.entity.resource.Vdc;
import com.bocloud.ims.interfaces.resource.VdcService;
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.*;

import java.util.List;

@RestController
@RequestMapping("/v1/vdcs")
@Api(value = "vdc管理", tags = "vdc管理")
public class VdcController {

    @Autowired
    private VdcService vdcService;

    /**
     * 列表查询
     *
     * @param pager
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查看数据中心列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "vcenter数据中心", operation = "查询数据中心列表")
    @GetMapping
    public BocloudResult<GridBean<Vdc>> list(Pager pager) {
        return vdcService.list(pager);
    }

    /**
     * 添加
     *
     * @param vdc
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "创建数据中心",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "vcenter数据中心", operation = "添加数据中心")
    @PostMapping
    public BocloudResult<Vdc> create(@RequestBody Vdc vdc,
                                     @Value(Common.REQ_CONTEXT) RequestContext context) {
        vdc.setCreatorId(context.getTarget());
        vdc.setMenderId(context.getTarget());
        vdc.setType(CloudProvider.VMWARE.name());
        return vdcService.create(vdc);
    }

    /**
     * 修改
     *
     * @param bean
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "更新数据中心",  httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "vcenter数据中心", operation = "修改数据中心")
    @PutMapping("/{id}")
    public BocloudResult<Vdc> modify(@RequestBody VdcBean bean,
                                     @PathVariable(value = Common.ID) Long id, @Value(Common.REQ_CONTEXT) RequestContext context) {
        bean.setId(id);
        bean.setType(CloudProvider.VMWARE.name());
        return vdcService.modify(bean, context.getTarget());
    }

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

    /**
     * 详细信息
     *
     * @param id 任务ID
     * @return 操作结果
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "数据中心详情",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "vcenter数据中心", operation = "查看数据中心详情")
    @RequestMapping(value = "/{id}/detail", method = {RequestMethod.GET})
    public BocloudResult<Vdc> detail(@PathVariable(value = Common.ID) Long id) {
        return vdcService.detail(id);
    }

    /**
     * 宿主机列表信息
     *
     * @param id 任务ID
     * @return 操作结果
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查看宿主机列表信息",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "vcenter数据中心", operation = "查询数据中心宿主机列表")
    @GetMapping("/{id}/hosts")
    public BocloudResult<List<Server>> hosts(@PathVariable(value = Common.ID) Long id) {
        return vdcService.listHost(id);
    }

    @ApiOperation(value = "同步Vdc下指定类型数据",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "云平台", operation = "同步Vdc下指定类型数据")
    @PostMapping("/{id}/sync")
    public BocloudResult sync(@PathVariable(value = Common.ID) Long id, @RequestParam(value = Common.TYPE) String type,
                              @Value(Common.REQ_CONTEXT) RequestContext context) {
        return vdcService.syncByVdc(id, type, context);
    }


}
