package com.ruoyi.project.business.dto.JEPCustomFunction;

import org.nfunk.jep.EvaluatorI;
import org.nfunk.jep.Node;
import org.nfunk.jep.ParseException;
import org.nfunk.jep.function.CallbackEvaluationI;
import org.nfunk.jep.function.PostfixMathCommand;

/**
 * 类似contains  用于查找在指定索引 位置开始，查询某个字符串中 是否存在某个字符串
 *
 * @author jubs
 */
public class FindFunction extends PostfixMathCommand implements CallbackEvaluationI {
    public FindFunction() {
        // 使用参数的数量
        numberOfParameters = 3;
    }

    @Override
    public Object evaluate(Node node, EvaluatorI pv) throws ParseException {
        int num = node.jjtGetNumChildren();
        if (!this.checkNumberOfParameters(num)) {
            throw new ParseException("If operator must have 3 arguments.");
        } else {
            Object searchBy = pv.eval(node.jjtGetChild(0));
            Object searchIn = pv.eval(node.jjtGetChild(1));
            Object startIndex = pv.eval(node.jjtGetChild(2));
            if (searchBy instanceof String && searchIn instanceof String && startIndex instanceof Number) {
                return  ((String) searchIn).indexOf((String) searchBy, ((Number) startIndex).intValue()) ;
            } else {
                throw new ParseException("Parameters must be String and Integer");
            }
        }
    }
}
