package sh.grimmer.bettershulkers.mixin;

import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.gui.screens.inventory.ShulkerBoxScreen;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.component.DataComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.contents.TranslatableContents;
import net.minecraft.world.entity.player.Inventory;
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.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import sh.grimmer.bettershulkers.BetterShulkers;

@Mixin(ShulkerBoxScreen.class)
public abstract class ShulkerBoxScreenMixin extends AbstractContainerScreen<ShulkerBoxMenu> {

    public ShulkerBoxScreenMixin(ShulkerBoxMenu menu, Inventory inventory, Component title) {
        super(menu, inventory, title);
    }

    @Inject(method = "<init>", at = @At("RETURN"))
    private void onInit(ShulkerBoxMenu menu, Inventory inventory, Component component, CallbackInfo ci) {
        if (!BetterShulkers.CONFIG.showBlockHint) {
            return;
        }

        if (!(menu.container instanceof ShulkerBoxBlockEntity shulkerBe)) {
            return;
        }

        if (component.getContents() instanceof TranslatableContents translatable) {
            if (translatable.getKey().equals("container.shulkerBox")) {
                return;
            }
        }

        DataComponentMap components = shulkerBe.collectComponents();
        DataComponentMap updated = DataComponentMap.builder()
                .addAll(components)
                .set(DataComponents.CUSTOM_NAME, component)
                .build();
        shulkerBe.applyComponents(updated, DataComponentPatch.EMPTY);
    }
}
