package expression;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

/**
 * @author Doschennikov Nikita (me@fymio.us)
 */
public class Floor extends AbstractExpression {

    private final AbstractExpression operand;

    public Floor(AbstractExpression operand) {
        this.operand = operand;
    }

    private static int floorInt(int n) {
        return Math.floorDiv(n, 1000) * 1000;
    }

    @Override
    public int evaluate(int x) {
        return floorInt(operand.evaluate(x));
    }

    @Override
    public int evaluate(int x, int y, int z) {
        return floorInt(operand.evaluate(x, y, z));
    }

    @Override
    public int evaluate(List<Integer> vars) {
        return floorInt(operand.evaluate(vars));
    }

    @Override
    public BigInteger evaluateBi(List<BigInteger> vars) {
        throw new UnsupportedOperationException(
            "floor not supported for BigInteger"
        );
    }

    @Override
    public BigDecimal evaluateBd(List<BigDecimal> vars) {
        throw new UnsupportedOperationException(
            "floor not supported for BigDecimal"
        );
    }

    @Override
    public String toString() {
        return "floor(" + operand + ")";
    }

    @Override
    public String toMiniString() {
        if (operand instanceof AbstractBinaryOperation) {
            return "floor(" + operand.toMiniString() + ")";
        }
        return "floor " + operand.toMiniString();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (!(obj instanceof Floor)) return false;
        return operand.equals(((Floor) obj).operand);
    }

    @Override
    public int hashCode() {
        return operand.hashCode() ^ 0x464C4F52;
    }
}
