/*
 * WorldEdit, a Minecraft world manipulation toolkit
 * Copyright (C) sk89q <http://www.sk89q.com>
 * Copyright (C) WorldEdit team and contributors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package com.sk89q.worldedit.function.mask;

import com.sk89q.worldedit.internal.expression.EvaluationException;
import com.sk89q.worldedit.internal.expression.Expression;
import com.sk89q.worldedit.internal.expression.ExpressionException;
import com.sk89q.worldedit.math.BlockVector2;

import javax.annotation.Nullable;
import java.util.function.IntSupplier;

import static com.google.common.base.Preconditions.checkNotNull;

public class ExpressionMask2D extends AbstractMask2D {

    private final Expression expression;
    private final IntSupplier timeout;

    /**
     * Create a new instance.
     *
     * @param expression the expression
     * @throws ExpressionException thrown if there is an error with the expression
     */
    public ExpressionMask2D(String expression) throws ExpressionException {
        this(Expression.compile(checkNotNull(expression), "x", "z"));
    }

    /**
     * Create a new instance.
     *
     * @param expression the expression
     */
    public ExpressionMask2D(Expression expression) {
        this(expression, null);
    }

    public ExpressionMask2D(Expression expression, @Nullable IntSupplier timeout) {
        checkNotNull(expression);
        this.expression = expression;
        this.timeout = timeout;
    }

    @Override
    public boolean test(BlockVector2 vector) {
        try {
            if (timeout == null) {
                return expression.evaluate(vector.getX(), 0, vector.getZ()) > 0;
            } else {
                return expression.evaluate(timeout.getAsInt(), vector.getX(), 0, vector.getZ()) > 0;
            }
        } catch (EvaluationException e) {
            return false;
        }
    }

    //FAWE start
    @Override
    public Mask2D copy2D() {
        return new ExpressionMask2D(expression.clone(), timeout);
    }
    //FAWE end

}
