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

import net.northking.cctp.common.exception.PlatformRuntimeException;
import net.northking.cctp.scriptcase.api.scenescript.bean.Line;
import net.northking.cctp.scriptcase.api.scenescript.bean.Node;
import net.northking.cctp.scriptcase.api.scenescript.bean.SceneScript;
import net.northking.cctp.scriptcase.constants.ScriptConstant;
import net.northking.cctp.scriptcase.enums.ErrorMessage;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SceneRelationVerify implements IDataVerify<SceneScript> {
    @Override
    public boolean verify(SceneScript dataDto) throws Exception{//场景节点挂接关系校验
        //开始、结束节点数量校验
        long startNodeCount = dataDto.getNodes().stream().filter(n->n.nodeType.equals(ScriptConstant.START)).count();
        if(startNodeCount == 0)
            throw new PlatformRuntimeException(new ErrorMessage("场景节点缺少开始节点!"));
        if(startNodeCount > 1)
            throw new PlatformRuntimeException(new ErrorMessage("场景开始节点有仅有一个!"));
        long endNodeCount = dataDto.getNodes().stream().filter(n->n.nodeType.equals(ScriptConstant.END)).count();
        if(endNodeCount == 0)
            throw new PlatformRuntimeException(new ErrorMessage("场景节点缺少结束节点!"));
        if(endNodeCount > 1)
            throw new PlatformRuntimeException(new ErrorMessage("场景结束节点有仅有一个!"));

        //脚本节点关系校验
        Map<String, Node> nodesMap =
                dataDto.getNodes().stream().collect(Collectors.toMap(Node::getId, v->v, (v1, v2)->v1));
         for(Map.Entry<String, Node> entry : nodesMap.entrySet()){
            List<Line> nodeLineList =
                    dataDto.getLines().stream().filter(l->l.source.equals(entry.getKey())).collect(Collectors.toList());
            String nodeName = entry.getValue().nodeName;
            if(!ScriptConstant.END.equals(entry.getValue().nodeType) && nodeLineList.size() == 0)
                throw new PlatformRuntimeException(new ErrorMessage(nodeName + "没有连接任何脚本节点!"));

            if(ScriptConstant.START.equals(entry.getValue().nodeType)) {
                if(nodeLineList.size() > 1)
                    throw new PlatformRuntimeException(new ErrorMessage(nodeName + "只能连接一条线到脚本节点!"));
                if(!nodeLineList.get(0).lineType.equals("commLine"))
                    throw new PlatformRuntimeException(new ErrorMessage(nodeName + "到脚本节点只能用commLine线型!"));
            } else if(ScriptConstant.END.equals(entry.getValue().nodeType)){
                if(dataDto.getLines().stream().filter(l->l.target.equals(entry.getKey())).count() == 0)
                    throw new PlatformRuntimeException(new ErrorMessage(nodeName + "缺少连接线!"));
            }else {//成功连接线必须有,失败线可以没有
              if(nodeLineList.stream().filter(l->l.lineType.equals("succLine")).count() == 0)
                  throw new PlatformRuntimeException(new ErrorMessage(nodeName + "缺少成功连接线!"));
            }
        }


        return true;
    }
}
