package cn.ibizlab.codegen.model;

import net.ibizsys.model.dataentity.logic.*;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class LogicLinkModel extends BaseModel {

    private LogicModel logicModel;

    public LogicLinkModel(LogicModel logicModel, IPSDELogicLink iPSDELogicLink) {
        this.opt = iPSDELogicLink;
        this.logicModel = logicModel;
        this.setCodeName(iPSDELogicLink.getCodeName());
        this.setName(iPSDELogicLink.getName());
        this.setId(String.format("%1$s-%2$s", logicModel.getCodeName(), iPSDELogicLink.getCodeName()));

    }

    public LogicModel getLogicModel() {
        return logicModel;
    }

    public IPSDELogicLink getPSDELogicLink() {
        return (IPSDELogicLink) opt;
    }

    public boolean hasCondtion() {
        if (this.getPSDELogicLink().getPSDELogicLinkGroupCond() != null) {
            return true;
        }
        return false;
    }

    public String getSourceNodeId() {
        String strSourceNodeId = this.getPSDELogicLink().getSrcPSDELogicNode().getCodeName().toLowerCase();
        if (this.getPSDELogicLink().getPSDELogicLinkGroupCond() != null) {
            strSourceNodeId = String.format("gateway-%s", strSourceNodeId);
        }
        return strSourceNodeId;
    }

    public String getTargetNodeId() {
        String strTargetNodeId = this.getPSDELogicLink().getDstPSDELogicNode().getCodeName().toLowerCase();
        return strTargetNodeId;
    }

    public String getConditionExpression() {
        StringBuffer strConditionException = new StringBuffer();
        if (hasCondtion()) {
            if (CollectionUtils.isEmpty(logicModel.getPSDELogic().getPSDELogicParams())) {
                for (IPSDELogicParam iPSDELogicParam : logicModel.getPSDELogic().getPSDELogicParams()) {
                    if (iPSDELogicParam.getParamPSDataEntity() != null) {
                        strConditionException.append(String.format("$%s:%s();\t\n", iPSDELogicParam.getCodeName().toLowerCase(), iPSDELogicParam.getParamPSDataEntity().getCodeName()));
                    } else {
                        strConditionException.append(String.format("$%s:Map();\t\n", iPSDELogicParam.getCodeName().toLowerCase()));
                    }
                }
                for (int i = 0; i < logicModel.getPSDELogic().getPSDELogicParams().size(); i++) {
                    IPSDELogicParam iPSDELogicParam = logicModel.getPSDELogic().getPSDELogicParams().get(i);
                    if (iPSDELogicParam.getParamPSDataEntity() != null) {
                        strConditionException.append(String.format("$%s==%s;\t\n", iPSDELogicParam.getCodeName().toLowerCase(), iPSDELogicParam.getCodeName().toLowerCase()));
                    }
                    if (i < logicModel.getPSDELogic().getPSDELogicParams().size() - 1)
                        strConditionException.append(" && ");
                }
            }
            strConditionException.append("eval");
            appendGroupCondition(strConditionException, this.getPSDELogicLink().getPSDELogicLinkGroupCond());
        }
        return strConditionException.toString();
    }

    public void appendGroupCondition(StringBuffer strConditionException, IPSDELogicLinkGroupCond iPSDELogicLinkGroupCond) {
        strConditionException.append("(");
        for (int i = 0; i < iPSDELogicLinkGroupCond.getPSDELogicLinkConds().size(); i++) {
            IPSDELogicLinkCond childLinkCond = iPSDELogicLinkGroupCond.getPSDELogicLinkConds().get(i);
            if (childLinkCond instanceof IPSDELogicLinkGroupCond) {
                appendGroupCondition(strConditionException, (IPSDELogicLinkGroupCond) childLinkCond);
            } else if (childLinkCond instanceof IPSDELogicLinkSingleCond) {
                appendSignleCondition(strConditionException, (IPSDELogicLinkSingleCond) childLinkCond);
            }
            if (i < logicModel.getPSDELogic().getPSDELogicParams().size() - 1) {
                if (iPSDELogicLinkGroupCond.getGroupOP().equals("AND")) {
                    strConditionException.append(" && ");
                } else if (iPSDELogicLinkGroupCond.getGroupOP().equals("OR")) {
                    strConditionException.append(" || ");
                }
            }
        }
        strConditionException.append(")");
    }

    public void appendSignleCondition(StringBuffer strConditionException, IPSDELogicLinkSingleCond iPSDELogicLinkSingleCond) {
        strConditionException.append("(");
        strConditionException.append("RuleUtils.test($");
        strConditionException.append(iPSDELogicLinkSingleCond.getDstLogicParam().getCodeName().toLowerCase());
        strConditionException.append(".get(\"");
        strConditionException.append(iPSDELogicLinkSingleCond.getDstFieldName().toLowerCase());
        strConditionException.append("\"),\"");
        strConditionException.append(iPSDELogicLinkSingleCond.getCondOP());
        strConditionException.append("\",\"");
        strConditionException.append(iPSDELogicLinkSingleCond.getValue());
        strConditionException.append("\")");
        strConditionException.append(")");
    }

}
