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

import com.bocloud.cmp.model.AutoScalingModel;
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.AutoScaling;
import com.bocloud.ims.entity.resource.AutoScalingAlarmTask;
import com.bocloud.ims.entity.resource.AutoScalingConfig;
import com.bocloud.ims.entity.resource.AutoScalingTimedTask;
import com.bocloud.ims.interfaces.resource.AutoScalingService;
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.*;

@RestController
@RequestMapping("/v1/autoscaling")
@Api(value = "弹性伸缩管理", tags = "弹性伸缩管理")
public class AutoScalingController {

    @Autowired
    private AutoScalingService autoScalingService;

    /**
     * 查询伸缩组列表
     *
     * @param pager
     * @return
     * @throws Exception
     */
    @Log(module = BoCloudService.IMS, target = "弹性伸缩", operation = "伸缩组列表")
    @GetMapping
    @ApiOperation(tags = {"CMC", "CSC"}, value = "弹性伸缩列表",  httpMethod = "GET")
    public BocloudResult<GridBean<AutoScaling>> listAutoScalingGroup(Pager pager) {
        return autoScalingService.listAutoScalingGroup(pager);
    }

    /**
     * 查询报警列表
     *
     * @param pager
     * @return
     * @throws Exception
     */
    @Log(module = BoCloudService.IMS, target = "弹性伸缩", operation = "伸缩组列表")
    @GetMapping("/alarmtask")
    @ApiOperation(tags = {"CMC", "CSC"}, value = "弹性伸缩告警任务列表",  httpMethod = "GET")
    public BocloudResult<GridBean<AutoScalingAlarmTask>> listAlarmTask(Pager pager) {
        return autoScalingService.listAlarmTask(pager);
    }

    /**
     * 查询定时列表
     *
     * @param pager
     * @return
     * @throws Exception
     */
    @Log(module = BoCloudService.IMS, target = "弹性伸缩", operation = "伸缩组列表")
    @GetMapping("/timedtask")
    @ApiOperation(tags = {"CMC", "CSC"}, value = "弹性伸缩定时任务列表",  httpMethod = "GET")
    public BocloudResult<GridBean<AutoScalingTimedTask>> listTimedTask(Pager pager) {
        return autoScalingService.listTimedTask(pager);
    }

    /**
     * 查询定时列表
     *
     * @param pager
     * @return
     * @throws Exception
     */
    @Log(module = BoCloudService.IMS, target = "弹性伸缩", operation = "伸缩组列表")
    @GetMapping("/config")
    @ApiOperation(tags = {"CMC", "CSC"}, value = "弹性伸缩config列表",  httpMethod = "GET")
    public BocloudResult<GridBean<AutoScalingConfig>> listAutoScalingConfig(Pager pager) {
        return autoScalingService.listAutoScalingConfig(pager);
    }

    /**
     * 添加弹性伸缩资源
     *
     * @param autoScalingModel
     * @param context
     * @return
     */
    @Log(module = BoCloudService.IMS, target = "弹性伸缩管理", operation = "保存弹性伸缩")
    @PostMapping
    @ApiOperation(tags = {"CMC", "CSC"}, value = "弹性伸缩新增",  httpMethod = "POST")
    public BocloudResult createAutoScaling(@RequestBody AutoScalingModel autoScalingModel,
                                           @Value(Common.REQ_CONTEXT) RequestContext context) {
        return autoScalingService.createAutoScaling(autoScalingModel, context);
    }

    /**
     * 删除弹性伸缩
     *
     * @param id
     * @param context
     * @return
     */
    @Log(module = BoCloudService.IMS, target = "弹性伸缩", operation = "删除伸缩组")
    @DeleteMapping("/{id}")
    @ApiOperation(tags = {"CMC", "CSC"}, value = "弹性伸缩删除",  httpMethod = "DELETE")
    public BocloudResult removeAutoScaling(@PathVariable(value = Common.ID) Long id,
                                           @Value(Common.REQ_CONTEXT) RequestContext context) {
        return autoScalingService.removeAutoScaling(id, context);
    }

    /**
     * 更新弹性伸缩
     *
     * @param id
     * @param autoScaling
     * @param context
     * @return
     */
    @Log(module = BoCloudService.IMS, target = "弹性伸缩管理", operation = "更新弹性伸缩")
    @PutMapping(value = "/{id}")
    @ApiOperation(tags = {"CMC", "CSC"}, value = "弹性伸缩更新",  httpMethod = "PUT")
    public BocloudResult modify(@PathVariable(value = Common.ID) Long id,
                                @RequestBody AutoScaling autoScaling,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        autoScaling.setId(id);
        return autoScalingService.modifyAutoScaling(autoScaling, context);
    }

    /**
     * 添加告警任务
     *
     * @param autoScalingModel
     * @param context
     * @return
     */
    @Log(module = BoCloudService.IMS, target = "弹性伸缩管理", operation = "新建报警任务")
    @PostMapping("/alarmtask")
    @ApiOperation(tags = {"CMC", "CSC"}, value = "弹性伸缩添加告警任务",  httpMethod = "POST")
    public BocloudResult createAlarmtask(@RequestBody AutoScalingModel autoScalingModel,
                                         @Value(Common.REQ_CONTEXT) RequestContext context) {
        return autoScalingService.createAlarmTask(autoScalingModel, context);
    }

    /**
     * 删除告警任务
     *
     * @param id
     * @param context
     * @return
     */
    @Log(module = BoCloudService.IMS, target = "弹性伸缩", operation = "删除告警任务")
    @DeleteMapping("/alarmtask/{id}")
    @ApiOperation(tags = {"CMC", "CSC"}, value = "弹性伸缩删除告警任务",  httpMethod = "DELETE")
    public BocloudResult removeAlarmtask(@PathVariable(value = Common.ID) Long id,
                                         @Value(Common.REQ_CONTEXT) RequestContext context) {
        return autoScalingService.removeAlarmTask(id, context);
    }

    /**
     * 添加定时任务
     *
     * @param autoScalingModel
     * @param context
     * @return
     */
    @Log(module = BoCloudService.IMS, target = "弹性伸缩管理", operation = "定时任务")
    @PostMapping("/timedtask")
    @ApiOperation(tags = {"CMC", "CSC"}, value = "弹性伸缩添加定时任务",  httpMethod = "POST")
    public BocloudResult<AutoScalingTimedTask> createTimedtask(@RequestBody AutoScalingModel autoScalingModel,
                                                               @Value(Common.REQ_CONTEXT) RequestContext context) {
        return autoScalingService.createTimedTask(autoScalingModel, context);
    }

    /**
     * 删除定时任务
     *
     * @param id
     * @param context
     * @return
     */
    @Log(module = BoCloudService.IMS, target = "弹性伸缩", operation = "删除定时任务")
    @DeleteMapping("/timedtask/{id}")
    @ApiOperation(tags = {"CMC", "CSC"}, value = "弹性伸缩删除定时任务",  httpMethod = "DELETE")
    public BocloudResult removeTimedtask(@PathVariable(value = Common.ID) Long id,
                                         @Value(Common.REQ_CONTEXT) RequestContext context) {
        return autoScalingService.removeTimedTask(id, context);
    }

    /**
     * 添加伸缩配置
     *
     * @param autoScalingModel
     * @param context
     * @return
     */
    @Log(module = BoCloudService.IMS, target = "弹性伸缩管理", operation = "伸缩配置")
    @PostMapping("/config")
    @ApiOperation(tags = {"CMC", "CSC"}, value = "弹性伸缩新增",  httpMethod = "POST")
    public BocloudResult<AutoScalingConfig> createAutoScalingConfig(@RequestBody AutoScalingModel autoScalingModel,
                                                                    @Value(Common.REQ_CONTEXT) RequestContext context) {
        return autoScalingService.createAutoScalingConfig(autoScalingModel, context);
    }

    /**
     * 删除伸缩配置
     *
     * @param id
     * @param context
     * @return
     */
    @Log(module = BoCloudService.IMS, target = "弹性伸缩", operation = "删除伸缩配置")
    @DeleteMapping("/config/{id}")
    @ApiOperation(tags = {"CMC", "CSC"}, value = "弹性伸缩删除",  httpMethod = "DELETE")
    public BocloudResult removeAutoScalingConfig(@PathVariable(value = Common.ID) Long id,
                                                 @Value(Common.REQ_CONTEXT) RequestContext context) {
        return autoScalingService.removeAutoScalingConfig(id, context);
    }

    /**
     * @param id
     * @return
     */
    @Log(module = BoCloudService.IMS, target = "弹性伸缩", operation = "伸缩组详情")
    @GetMapping("/{id}")
    @ApiOperation(tags = {"CMC", "CSC"}, value = "弹性伸缩详情",  httpMethod = "GET")
    public BocloudResult<AutoScaling> detail(@PathVariable(value = Common.ID) Long id) {
        return autoScalingService.detail(id);
    }

    /**
     * 同步伸缩组信息
     *
     * @param id
     * @param context
     * @return
     */
    @Log(module = BoCloudService.IMS, target = "伸缩组", operation = "伸缩组同步")
    @PatchMapping("/{id}")
    @ApiOperation(tags = {"CMC", "CSC"}, value = "弹性伸缩操作",  httpMethod = "PATCH")
    public BocloudResult operation(@PathVariable(value = Common.ID) Long id,
                                   @Value(Common.REQ_CONTEXT) RequestContext context) {
        return autoScalingService.syncStackInfo(id, context);
    }

}