package sh.grimmer.bettershulkers.mixin;

import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.inventory.ShulkerBoxMenu;
import net.minecraft.world.level.block.entity.ShulkerBoxBlockEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import sh.grimmer.bettershulkers.BetterShulkers;
import sh.grimmer.bettershulkers.data.ShulkerBoxTracker;

@Mixin(ShulkerBoxMenu.class)
public class ShulkerBoxMenuMixin {

    @ModifyVariable(method = "<init>(ILnet/minecraft/world/entity/player/Inventory;Lnet/minecraft/world/Container;)V", at = @At("HEAD"), ordinal = 0, argsOnly = true)
    private static Container onInit(Container container, @Local(argsOnly = true) int containerId) {
        if (!BetterShulkers.CONFIG.showBlockHint) {
            return container;
        }
        if (!(container instanceof SimpleContainer)) {
            return container;
        }
        ShulkerBoxBlockEntity lastOpened = ShulkerBoxTracker.getLastOpened();
        if (lastOpened == null) {
            return container;
        }
        lastOpened.clearContent();
        ShulkerBoxTracker.setLastOpened(null);
        return lastOpened;
    }
}
