package net.northking.cctp.scriptcase.api.scenescript;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.scriptcase.api.scenescript.service.coordinate.SceneCoordinatorService;
import net.northking.cctp.scriptcase.dto.sceneScript.ReqSceneFirstNodeDto;
import net.northking.cctp.scriptcase.dto.sceneScript.ReqSceneNextNodeDto;
import net.northking.cctp.scriptcase.dto.sceneScript.ResNodeInfoDto;
import net.northking.cctp.scriptcase.dto.sceneScript.StopCaseDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "场景节点调度管理")
@RequestMapping("/pub/scene")
@RestController
public class SceneCoordinatorCtrl {
    private final static Logger logger = LoggerFactory.getLogger(SceneCoordinatorCtrl.class);

    @Autowired
    private SceneCoordinatorService sceneCoordinatorService;

    @ApiOperation(value = "获取场景首节点")
    @PostMapping(value = "/getFirstNode")
    public ResultWrapper<ResNodeInfoDto> getFirstNode(@RequestBody @Validated
                ReqSceneFirstNodeDto reqSceneFirstNodeDto)
    {
        logger.debug("获取场景首节点入参:" + JSONObject.toJSONString(reqSceneFirstNodeDto));
        ResultWrapper<ResNodeInfoDto> result = new ResultWrapper<ResNodeInfoDto>();
        ResNodeInfoDto reqNodeInfoDto = sceneCoordinatorService.getFirstNode(reqSceneFirstNodeDto);
        return result.success(reqNodeInfoDto);
    }

    @ApiOperation(value = "获取场景下一调度节点")
    @PostMapping(value = "/getNextNode")
    public ResultWrapper<ResNodeInfoDto> getNextNode(@RequestBody @Validated
                ReqSceneNextNodeDto reqSceneNextNodeDto)
    {
        logger.debug(" 获取场景下一调度节点入参:" + JSONObject.toJSONString(reqSceneNextNodeDto));
        ResultWrapper<ResNodeInfoDto> result = new ResultWrapper<ResNodeInfoDto>();
        ResNodeInfoDto reqNodeInfoDto = sceneCoordinatorService.getNextNode(reqSceneNextNodeDto);
        return result.success(reqNodeInfoDto);
    }

    @ApiOperation(value = "终止调度")
    @PostMapping(value = "/stopCase")
    public ResultWrapper<String> stopCase(@RequestBody @Validated StopCaseDto stopCaseDto)
    {
        logger.debug("终止调度入参:" + JSONObject.toJSONString(stopCaseDto));
        ResultWrapper<String> result = new ResultWrapper<>();
        sceneCoordinatorService.stopCase(stopCaseDto);
        return result.success();
    }

}
