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

import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.RequestContext;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.ResultTools;
import com.bocloud.ims.entity.topology.InstanceLocation;
import com.bocloud.ims.interfaces.topology.InstanceRelationService;
import com.bocloud.ims.interfaces.topology.InstanceService;
import com.bocloud.ims.interfaces.topology.RelationCategoryService;
import com.bocloud.ims.service.topology.InstanceMessageDispatcher;
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.Map;

/**
 * @author lyy
 * @version 1.0
 * @date 2021/4/13 15:09
 */
@Api(value = "资源实例", tags = "资源实例")
@RestController
@RequestMapping("/v1/instances")
public class InstanceController {
    @Autowired
    private InstanceMessageDispatcher messageDispatcher;
    @Autowired
    private InstanceRelationService instanceRelationService;
    @Autowired
    private RelationCategoryService relationCategoryService;
    @Autowired
    private InstanceService instanceService;

    /**
     * 资源实例同步
     *
     * @param resources
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC"}, value = "资源实例同步", httpMethod = "POST")
    @PostMapping(value = "/sync")
    public BocloudResult sync(@RequestParam(value = "resources") String resources,
                              @Value(Common.REQ_CONTEXT) RequestContext context) {
        if (null != resources) {
            try {
                messageDispatcher.cacheMessage(resources);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return new BocloudResult(true, "资源数据同步任务已下发。。。");
        } else {
            return new BocloudResult(false, "同步数据为空");
        }
    }


    /**
     * 资源拓扑
     *
     * @param categoryId
     * @param uuid
     * @param oneLevel
     * @param x
     * @param y
     * @param isGenerate
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC"}, value = "资源拓扑", httpMethod = "GET")
    @GetMapping(value = "/topology")
    public BocloudResult<Map<String, Object>> topology(@RequestParam(value = "categoryId", required = false) Long categoryId,
                                                       @RequestParam(value = "uuid", required = false) String uuid,
                                                       @RequestParam(value = "oneLevel", required = false) Boolean oneLevel,
                                                       @RequestParam(value = "x", required = false) String x,
                                                       @RequestParam(value = "y", required = false) String y,
                                                       @RequestParam(value = "isGenerate", required = false) Boolean isGenerate,
                                                       @RequestParam(value = "nodeId", required = false) Long nodeId,
                                                       @Value(Common.REQ_CONTEXT) RequestContext context) {
        // 在平台管理展示平台拓扑时只能通过uuid来查询平台数据对应的实例
        if (categoryId != null) {
            return instanceRelationService.topology(nodeId, uuid, categoryId, context.getTarget(), oneLevel, x, y, isGenerate);
        }
        return ResultTools.formatErrResult();
    }

    /**
     * 实例关系拓扑坐标设置
     *
     * @param id
     * @param instanceLocation
     * @return
     */
    @ApiOperation(tags = {"CMC"}, value = "拓扑坐标设置", httpMethod = "PATCH")
    @PatchMapping(value = "/{id}/topology/coordinates")
    public BocloudResult location(@PathVariable(value = Common.ID) Long id,
                                  @RequestBody InstanceLocation instanceLocation,
                                  @Value(Common.REQ_CONTEXT) RequestContext context) {
        if (null != instanceLocation) {
            instanceLocation.setInstanceId(id);
            instanceLocation.setManagerId(context.getTarget());
            return instanceRelationService.location(instanceLocation);
        }
        return ResultTools.formatErrResult();
    }

    /**
     * 查询维度
     *
     * @return
     */
    @ApiOperation(tags = {"CMC"}, value = "查询维度", httpMethod = "GET")
    @GetMapping("/categories")
    public BocloudResult listall() {
        return relationCategoryService.list();
    }

    /**
     * 查看CI实例明细
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "查看模型实例详情", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping(value = "/{id}")
    public BocloudResult detail(@PathVariable(value = Common.ID) Long id) {
        return instanceService.detail(id);
    }
}
