package com.jfireel.expression.node.impl;

import com.jfireel.expression.token.Operator;
import com.jfireel.expression.util.number.SubtractUtil;
import com.mixsmart.utils.StringUtils;

import java.math.BigDecimal;
import java.util.Map;

public class MinusNode extends OperatorResultNode {
	public MinusNode() {
		super(Operator.MINUS);
	}

	@Override
	public Object calculate(Map<String, Object> variables) {
		Object leftValue = leftOperand.calculate(variables);
		if(leftValue instanceof String && StringUtils.isNumber(leftValue)) {
			leftValue = new BigDecimal(leftValue.toString());
		}
		if (leftValue instanceof Number == false) {
			return null;
		}
		Object rightValue = rightOperand.calculate(variables);
		if(rightValue instanceof String && StringUtils.isNumber(rightValue)) {
			rightValue = new BigDecimal(rightValue.toString());
		}
		if (rightValue instanceof Number == false) {
			return null;
		}
		return SubtractUtil.calculate((Number) leftValue, (Number) rightValue);
	}

}
