package cz.jzitnik.game.handlers.tooluse.handlers;

import cz.jzitnik.game.Game;
import cz.jzitnik.game.annotations.Farmable;
import cz.jzitnik.game.annotations.ToolUse;
import cz.jzitnik.game.entities.Block;
import cz.jzitnik.game.entities.items.ItemBlockSupplier;
import cz.jzitnik.game.entities.items.ItemType;
import cz.jzitnik.game.handlers.tooluse.ToolUseHandler;

@ToolUse(ItemType.HOE)
public class HoeUse implements ToolUseHandler{

	@Override
	public void handle(Game game, int x, int y) {
        var blocks = game.getWorld()[y][x];

        if (!blocks.stream().anyMatch(block -> block.getBlockId().equals("dirt") || block.getBlockId().equals("grass"))) {
            return;
        }

        if (game.getWorld()[y-1][x].stream().anyMatch(block -> isBlock(block))) {
            return;
        }

        blocks.removeIf(block -> block.getBlockId().equals("dirt") || block.getBlockId().equals("grass"));
        blocks.add(ItemBlockSupplier.getBlock("farmland"));
	}
    
    public static boolean isBlock(Block block) {
        return !block.getBlockId().equals("air") && !block.isMob() && !block.getClass().isAnnotationPresent(Farmable.class);
    }
}
