package eu.mhsl.minecraft.pixelblocks.listeners;

import eu.mhsl.minecraft.pixelblocks.PixelBlockItem;
import eu.mhsl.minecraft.pixelblocks.Translations;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.inventory.ItemStack;

import java.util.Locale;
import java.util.UUID;

public class CraftPixelBlockListener implements Listener {
    @EventHandler
    public void onCraft(CraftItemEvent event) {
        ItemStack result = event.getInventory().getResult();
        if(result == null) return;

        PixelBlockItem.BlockInfo info = PixelBlockItem.getBlockInfo(result);
        if(info == null || !info.id().equals(PixelBlockItem.emptyBlockUUID)) return;

        Locale locale = event.getWhoClicked() instanceof Player player ? player.locale() : Translations.defaultLocale;
        ItemStack localizedResult = PixelBlockItem.getEmptyPixelBlock(locale);
        PixelBlockItem.setBlockId(localizedResult, UUID.randomUUID());
        event.getInventory().setResult(localizedResult);
    }
}
