package sh.grimmer.bettershulkers.mixin;

import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.SubmitNodeCollector;
import net.minecraft.client.renderer.special.ShulkerBoxSpecialRenderer;
import net.minecraft.client.renderer.special.SpecialModelRenderer;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import sh.grimmer.bettershulkers.BetterShulkers;
import sh.grimmer.bettershulkers.render.ShulkerHintData;
import sh.grimmer.bettershulkers.render.ShulkerHintRenderer;
import sh.grimmer.bettershulkers.util.ShulkerBoxHelper;

import org.jetbrains.annotations.Nullable;

@Mixin(ShulkerBoxSpecialRenderer.class)
public abstract class ShulkerBoxSpecialRendererMixin implements SpecialModelRenderer<ShulkerHintData> {

    @Shadow
    public abstract void submit(ItemDisplayContext ctx, PoseStack poseStack, SubmitNodeCollector collector, int light, int overlay, boolean bl, int k);

    @Nullable
    @Override
    public ShulkerHintData extractArgument(ItemStack stack) {
        return ShulkerHintData.fromShulkerBox(ShulkerBoxHelper.getItems(stack), ShulkerBoxHelper.getCustomName(stack));
    }

    @Override
    public void submit(@Nullable ShulkerHintData data, ItemDisplayContext ctx, PoseStack poseStack, SubmitNodeCollector collector, int light, int overlay, boolean bl, int k) {
        boolean isGui = ctx == ItemDisplayContext.GUI;

        boolean shouldHideModel = BetterShulkers.CONFIG.hideModelBehindHint
                && isGui
                && data != null
                && data.getDisplayItem() != null;

        if (!shouldHideModel) {
            submit(ctx, poseStack, collector, light, overlay, bl, k);
        }

        if (BetterShulkers.CONFIG.showItemHint && data != null) {
            float scale = isGui ? BetterShulkers.CONFIG.hintItemScale / 100F : 1F;
            ShulkerHintRenderer.submitItemHint(data, poseStack, collector, light, scale);
        }
    }
}
