package cz.jzitnik.game.handlers.pickup.handlers;

import cz.jzitnik.game.Game;
import cz.jzitnik.game.annotations.PickupHandler;
import cz.jzitnik.game.entities.Block;
import cz.jzitnik.game.entities.items.ItemBlockSupplier;
import cz.jzitnik.game.handlers.pickup.CustomPickupHandler;
import cz.jzitnik.game.logic.services.flowing.FlowingData;

@PickupHandler("water_bucket,lava_bucket")
public class FullBuckerPickupHandler implements CustomPickupHandler {

    @Override
    public boolean handle(Game game, int x, int y) {
        var world = game.getWorld();
        var inventory = game.getInventory();

        if (world[y][x].stream().anyMatch(block -> !block.getBlockId().equals("air") && (block.isFlowing() && ((FlowingData) block.getData()).isSource()))) {
            return false;
        }

        world[y][x].add(inventory.getItemInHand().get().getBlock().get());

        inventory.decreaseItemInHand();

        inventory.addItem(ItemBlockSupplier.getItem("bucket"));

        return true;
    }
}
