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

import net.northking.cctp.common.exception.PlatformRuntimeException;
import net.northking.cctp.scriptcase.api.scenescript.bean.SceneScript;
import net.northking.cctp.scriptcase.api.scenescript.bean.Variable;
import net.northking.cctp.scriptcase.db.entity.AtuGlobalVariable;
import net.northking.cctp.scriptcase.db.service.AtuGlobalVariableService;
import net.northking.cctp.scriptcase.enums.ErrorMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

@Service
public class VariableVerify implements IDataVerify<SceneScript>{

    @Autowired
    private AtuGlobalVariableService atuGlobalVariableService;

    private String projectId = "";

    public void setProjectId(String projectId){
        this.projectId = projectId;
    }

    @Override
    public boolean verify(SceneScript data) throws Exception {
       //duplicate check
       List<Variable> variableList = data.getVariables();
        Map<String, List<Variable>> variableMap =
        variableList.stream().collect(Collectors.toMap(Variable::getName, s ->{
            List<Variable> variableList1 = new ArrayList<>();
            variableList1.add(s);
            return variableList1;
        }, (List<Variable> v1, List<Variable> v2)->{
            v1.addAll(v2);
            return v1;
        }));

        StringBuilder sb = new StringBuilder();
        for(Map.Entry<String, List<Variable>> entry : variableMap.entrySet()) {
            if(entry.getValue().size() > 1)
                sb.append(entry.getKey()).append(",");
        }
        String msg = sb.toString();
        if(StringUtils.hasText(msg)){
            msg = msg.substring(0, msg.length() - 1);
            throw new PlatformRuntimeException(new ErrorMessage("场景脚本中存在重名变量(变量名是"+msg+")"));
        }

        //节点输入项校验
        StringBuilder sb1 = new StringBuilder();
        data.getNodes().forEach(node->{
            if(node.getInputs() == null)
                return;

            node.getInputs().forEach(argument->{
                if(StringUtils.isEmpty(argument.getParameter()))
                    return;

                String parameter = argument.getParameter().replace("${", "")
                        .replace("#{", "").replace("}", "");
                if(argument.getParameter().contains("${")){
                    //局部变量
                   if(!variableMap.containsKey(parameter))
                       sb1.append(parameter).append(", ");

                }else if(argument.getParameter().contains("#{")){
                    //全局部变量
                    AtuGlobalVariable where  = new AtuGlobalVariable();
                    where.setName(parameter);
                    where.setDelete(false);
                    where.setProjectId(projectId);
                    where.setVariableType(false);
                    AtuGlobalVariable variable =
                            atuGlobalVariableService.queryVariable(where);
                    if(variable == null)
                        sb1.append(parameter).append(", ");
                }
            });
        });
        String msg1 = sb1.toString();
        if(StringUtils.hasText(msg1)){
            msg1 = msg1.substring(0, msg1.length() - 1);
            throw new PlatformRuntimeException(new ErrorMessage("场景脚本设置的变量不存在(变量名是"+msg1+")"));
        }

        return true;
    }
}
