package cz.jzitnik.game.handlers.place.handlers;

import cz.jzitnik.game.annotations.PlaceHandler;
import cz.jzitnik.game.entities.Block;
import cz.jzitnik.game.Game;
import cz.jzitnik.game.entities.items.ItemBlockSupplier;
import cz.jzitnik.game.handlers.place.CustomPlaceHandler;

import java.util.Random;

@PlaceHandler("gravel")
public class GravelMiningHandler implements CustomPlaceHandler {
    @Override
    public boolean place(Game game, int x, int y) {
        var blocks = game.getWorld()[y][x];
        var inventory = game.getInventory();

        blocks.add(inventory.getItemInHand().get().getBlock().get());
        blocks.removeAll(blocks.stream().filter(Block::isFlowing).toList());

        inventory.decreaseItemInHand();

        return true;
    }

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

        blocks.removeAll(blocks.stream().filter(i -> !i.getBlockId().equals("air") && !i.isMob()).toList());

        Random random = new Random();
        int percentage = random.nextInt(100);

        if (percentage < 10) {
            game.getInventory().addItem(ItemBlockSupplier.getItem("flint"));
            return false;
        }

        return true;
    }
}
