// Copyright Daniel Ratcliffe, 2011-2022. Do not distribute without permission.
//
// SPDX-License-Identifier: LicenseRef-CCPL

package dan200.computercraft.shared.turtle.core;

import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.TurtleCommand;
import dan200.computercraft.api.turtle.TurtleCommandResult;
import dan200.computercraft.shared.util.WorldUtil;

public class TurtleDetectCommand implements TurtleCommand {
    private final InteractDirection direction;

    public TurtleDetectCommand(InteractDirection direction) {
        this.direction = direction;
    }

    @Override
    public TurtleCommandResult execute(ITurtleAccess turtle) {
        // Get world direction from direction
        var direction = this.direction.toWorldDir(turtle);

        // Check if thing in front is air or not
        var level = turtle.getLevel();
        var pos = turtle.getPosition().relative(direction);

        return !WorldUtil.isEmptyBlock(level.getBlockState(pos))
            ? TurtleCommandResult.success()
            : TurtleCommandResult.failure();
    }
}
