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;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Stack;

public class IsEmptyFunction  extends PostfixMathCommand implements CallbackEvaluationI {

    public IsEmptyFunction() {
        // 使用参数的数量
        numberOfParameters = -1;
    }

    @Override
    public Object evaluate(Node node, EvaluatorI evaluatorI) throws ParseException {
        int num = node.jjtGetNumChildren();
        if (num < 1) {
            throw new ParseException("IsEmpty operator at least must have 1 arguments.");
        } else {
            Object judgeCondition = evaluatorI.eval(node.jjtGetChild(0));
            if (judgeCondition == null) {
                return 1.0;
            } else if (judgeCondition instanceof String) {
                if (((String) judgeCondition).isEmpty()) {
                    return 1.0;
                } else {
                    if (((String) judgeCondition).trim().isEmpty() ||
                            "null".equals(((String) judgeCondition).trim())
                            || "NULL".equals(((String) judgeCondition).trim())
                            || "NO-VALUE".equals(((String) judgeCondition).trim())) {
                        return 1.0;
                    }
                    return 0.0;
                }
            } else if (judgeCondition instanceof Boolean) {
                return (Boolean) judgeCondition ? 1.0 : 0.0;
            } else if (judgeCondition instanceof Double) {
                return ((Double) judgeCondition == 0.0) ? 1.0 : 0.0;
            } else if (judgeCondition instanceof BigDecimal) {
                return ((BigDecimal) judgeCondition).compareTo(BigDecimal.ZERO) == 0 ? 1.0 : 0.0;
            }
        }
        return null;
    }
}