package cz.jzitnik.game.handlers.place;

import cz.jzitnik.game.Game;
import cz.jzitnik.game.entities.Block;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DefaultPlaceHandler implements CustomPlaceHandler {
    @Override
    public boolean place(Game game, int x, int y) {
        var blocks = game.getWorld()[y][x];
        var inventory = game.getInventory();
        var block = inventory.getItemInHand().get().getBlock().get();

        blocks.add(block);
        blocks.removeAll(blocks.stream().filter(Block::isFlowing).toList());

        inventory.decreaseItemInHand();

        log.info("Block {} was placed at coordinates x:{},y:{}", block.getBlockId(), x, y);

        return true;
    }

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

        log.info("Block was mined at coordinates x:{},y:{}", x, y);
        blocks.removeAll(blocks.stream().filter(i -> !i.getBlockId().equals("air") && !i.isMob()).toList());

        return true;
    }
}
