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

import com.bocloud.cmp.entity.enums.SyncType;
import com.bocloud.cmp.model.CloudVendorBean;
import com.bocloud.cmp.vm.model.ProjectParam;
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.AlarmThreshold;
import com.bocloud.ims.entity.resource.CloudVendor;
import com.bocloud.ims.interfaces.resource.CloudVendorService;
import com.bocloud.ims.interfaces.resource.SyncService;
import com.bocloud.ims.interfaces.resource.VendorProjectService;
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;

/**
 * 云供应商Controller
 *
 * @author DZG
 * @since V1.0 2016年9月1日
 */
@Api(value = "云供应商", tags = "云供应商")
@RestController
@RequestMapping("/v1/vendors")
public class CloudVendorController {

    @Autowired
    private CloudVendorService cloudVendorService;
    @Autowired
    private SyncService syncService;
    @Autowired
    private VendorProjectService vendorProjectService;

    /**
     * 获取云供应商列表
     *
     * @param pager
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "获取云供应商列表", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "云平台", operation = "获取云供应商列表")
    @GetMapping
    public BocloudResult<GridBean<CloudVendor>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return cloudVendorService.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 cloudVendorService.condition(condition, context);
    }

    /**
     * 添加云供应商
     *
     * @param vendor
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "创建云供应商", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "云平台", operation = "添加云供应商")
    @PostMapping
    public BocloudResult<CloudVendor> create(@RequestBody CloudVendor vendor, @Value(Common.REQ_CONTEXT) RequestContext context) {
        vendor.setCreatorId(context.getTarget());
        vendor.setMenderId(context.getTarget());
        return cloudVendorService.create(vendor, 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<CloudVendor> modify(@PathVariable(value = Common.ID) Long id, @RequestBody CloudVendorBean bean,
                                             @Value(Common.REQ_CONTEXT) RequestContext context) {
        bean.setId(id);
        return cloudVendorService.modify(bean, context);
    }

    /**
     * 移除云供应商
     *
     * @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 cloudVendorService.remove(id, context);
    }

    /**
     * 查看云供应商详细信息
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "查看云供应商详细信息", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "云平台", operation = "查看云供应商详情")
    @GetMapping("/{id}")
    public BocloudResult<CloudVendor> detail(@PathVariable(value = Common.ID) Long id) {
        return cloudVendorService.detail(id);
    }

    /**
     * 冻结云供应商
     *
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "云平台操作", httpMethod = "PATCH")
    @Log(module = BoCloudService.IMS, target = "云平台", operation = "云平台操作")
    @PatchMapping("/{id}")
    public BocloudResult action(@PathVariable(value = Common.ID) Long id,
                                @RequestParam(value = Common.ACTION, required = false) String action,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return cloudVendorService.action(id, action, context);
    }

    /**
     * 更新认证信息
     *
     * @param id
     * @param cloudVendor
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "修改云平台密码", httpMethod = "PATCH")
    @Log(module = BoCloudService.IMS, target = "云平台", operation = "更新认证信息")
    @PatchMapping("/{id}/password")
    public BocloudResult updatePassword(@PathVariable(value = Common.ID) Long id, @RequestBody CloudVendor cloudVendor,
                                        @Value(Common.REQ_CONTEXT) RequestContext context) {
        return cloudVendorService.updatePassword(id, cloudVendor, context);
    }
    
    /**
     * 修改云平台超分比
     *
     * @param id
     * @param cloudVendor
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC"}, value = "修改云平台超分比", httpMethod = "PATCH")
    @Log(module = BoCloudService.IMS, target = "云平台", operation = "更新认证信息")
    @PatchMapping("/{id}/excessRatio")
    public BocloudResult updateExcessRatio(@PathVariable(value = Common.ID) Long id, @RequestBody CloudVendor cloudVendor,
                                        @Value(Common.REQ_CONTEXT) RequestContext context) {
        return cloudVendorService.updateExcessRatio(id, cloudVendor, context);
    }

    /**
     * 同步云供应商数据
     *
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "同步云供应商数据", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "云平台", operation = "同步云供应商数据")
    @PostMapping("/{id}/sync")
    public BocloudResult sync(@PathVariable(value = Common.ID) Long id,
                              @Value(Common.REQ_CONTEXT) RequestContext context) {
        return syncService.sync(id, context, false);
    }

    /**
     * 同步平台指定类型数据
     *
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "同步平台指定类型数据", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "云平台", operation = "同步平台指定类型数据")
    @PostMapping("/{id}/sync/{syncType}")
    public BocloudResult sync(@PathVariable(value = Common.ID) Long id, @PathVariable(value = "syncType") String syncType, @Value(Common.REQ_CONTEXT) RequestContext context) {
        try {
            SyncType type = SyncType.valueOf(syncType);
            return syncService.sync(id, context, type);
        } catch (Exception e) {
            return new BocloudResult(false, "不支持的数据类型：" + syncType + "   " + e, "400", "修改数据类型");
        }
    }

    /**
     * 设置云平台告警阈值
     *
     * @param id
     * @param alarmThreshold
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "设置云平台告警阈值", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "云平台", operation = "设置云平台告警阈值")
    @PostMapping("/{id}/alarms")
    public BocloudResult createAlarm(@PathVariable(value = Common.ID) Long id,
                                     @RequestBody AlarmThreshold alarmThreshold) {
        return cloudVendorService.createAlarm(alarmThreshold);
    }

    /**
     * 查询告警阈值详情
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "查询告警阈值", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "云平台", operation = "查询告警阈值详情")
    @GetMapping("/{id}/alarms")
    public BocloudResult<AlarmThreshold> getAlarm(@PathVariable(value = Common.ID) Long id) {
        return cloudVendorService.getAlarm(id);
    }

    /**
     * 查看vcenter平台全部集群的内存/cpu/存储的总使用量
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "查询云平台CPU内存存储总使用量", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "云平台", operation = "查询云平台CPU内存存储总使用量")
    @GetMapping("/{id}/capacity")
    public BocloudResult<CloudVendor> queryCapacity(@PathVariable(value = Common.ID) Long id) {
        return cloudVendorService.queryCapacity(id);
    }


    /**
     * 批量开启/关闭告警
     *
     * @param ids
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "批量开启/关闭告警", httpMethod = "PATCH")
    @PatchMapping("/alarm")
    public BocloudResult batchOperationAlarm(@RequestBody List<Long> ids,
                                             @RequestParam(value = Common.ACTION) String action,
                                             @Value(Common.REQ_CONTEXT) RequestContext context) {
        return cloudVendorService.batchOperationAlarm(ids, action, context.getTarget());
    }

    /**
     * 创建OpenStack项目
     *
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "创建OpenStack项目", httpMethod = "POST")
    @PostMapping("/op/projects")
    @Log(module = BoCloudService.IMS, target = "云平台", operation = "创建OpenStack项目")
    public BocloudResult handleProject(@RequestBody ProjectParam params,
                                       @Value(Common.REQ_CONTEXT) RequestContext context) {
        return vendorProjectService.handle(params.getTenantId(), params.getPoolIds(), context);
    }


    /**
     * 创建租户删除openstack项目
     *
     * @param tenantId
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "创建租户删除openstack项目", httpMethod = "DELETE")
    @DeleteMapping("/op/projects")
    @Log(module = BoCloudService.IMS, target = "云平台", operation = "创建租户删除openstack项目")
    public BocloudResult deleteByTenantId(@RequestParam(value = Common.TENANTID) Long tenantId) {
        return vendorProjectService.deleteByTenant(tenantId);
    }

    /**
     * 同步vcenter网络
     *
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "同步vcenter网络", httpMethod = "POST")
    @PostMapping("/vc/networks")
    @Log(module = BoCloudService.IMS, target = "云平台", operation = "同步vcenter网络")
    public BocloudResult handleProject(@PathVariable(value = Common.ID) Long id,
                                       @Value(Common.REQ_CONTEXT) RequestContext context) {
        BocloudResult result = cloudVendorService.detail(id);
        if (result.isSuccess()) {
            return syncService.syncVcenterNetwork((CloudVendor) result.getData(), context.getTarget());
        }
        return result;
    }


    /**
     * 清除provider中缓存原生平台的session
     *
     * @param id 平台id
     * @param context 用户信息
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "清除Session缓存", httpMethod = "DELETE")
    @DeleteMapping("/{id}/sessions")
    @Log(module = BoCloudService.IMS, target = "云平台", operation = "同步vcenter网络")
    public BocloudResult clearSession(@PathVariable(value = Common.ID) Long id,
                                       @Value(Common.REQ_CONTEXT) RequestContext context) {
        return cloudVendorService.clearSession(id, context);
    }
}
