package sh.grimmer.bettershulkers.mixin;

import net.minecraft.core.component.DataComponentHolder;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.component.DataComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.component.TooltipDisplay;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import sh.grimmer.bettershulkers.BetterShulkers;
import sh.grimmer.bettershulkers.config.TooltipPreviewMode;

import java.util.function.Consumer;

@Mixin(ItemStack.class)
public abstract class ItemStackMixin implements DataComponentHolder {

    @Shadow
    public abstract Item getItem();

    @Shadow
    public abstract void addToTooltip(DataComponentType<?> dataComponentType, Item.TooltipContext tooltipContext, TooltipDisplay tooltipDisplay, Consumer<Component> consumer, TooltipFlag tooltipFlag);

    @Inject(method = "addToTooltip", at = @At(value = "HEAD"), cancellable = true)
    public void onAddToTooltip(DataComponentType<?> type, Item.TooltipContext ctx, TooltipDisplay display, Consumer<Component> consumer, TooltipFlag flag, CallbackInfo ci) {
        if (type != DataComponents.CONTAINER) {
            return;
        }
        if (!(getItem() instanceof BlockItem blockItem)) {
            return;
        }
        if (!(blockItem.getBlock() instanceof net.minecraft.world.level.block.ShulkerBoxBlock)) {
            return;
        }
        if (BetterShulkers.CONFIG.tooltipPreview != TooltipPreviewMode.NEVER) {
            ci.cancel();
        }
    }
}
