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.InventoryItem;
import cz.jzitnik.game.entities.items.ItemBlockSupplier;
import cz.jzitnik.game.entities.items.ItemType;
import cz.jzitnik.game.handlers.place.CustomPlaceHandler;

import java.util.Random;

@PlaceHandler("oak_leaves")
public class OakLeavesMineHandler 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 mined with sheers drop leaf
        if (game.getInventory().getItemInHand().isPresent() && game.getInventory().getItemInHand().get().getType() == ItemType.SHEARS) {
            return true;
        }

        if (percentage < 2) {
            // Apple
            game.getInventory().addItem(ItemBlockSupplier.getItem("apple"));
        } else if (percentage < 2 + 4) {
            // Sticks
            game.getInventory().addItem(new InventoryItem(random.nextInt(2) + 1, ItemBlockSupplier.getItem("stick")));
        } else if (percentage < 2 + 4 + 5) {
            // Sapling
            game.getInventory().addItem(ItemBlockSupplier.getItem("oak_sapling"));
        }

        return false;
    }
}
