package sh.grimmer.bettershulkers.mixin;

import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.SubmitNodeCollector;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.client.renderer.blockentity.ShulkerBoxRenderer;
import net.minecraft.client.renderer.blockentity.state.ShulkerBoxRenderState;
import net.minecraft.client.renderer.feature.ModelFeatureRenderer;
import net.minecraft.client.renderer.item.ItemStackRenderState;
import net.minecraft.client.renderer.state.CameraRenderState;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.ShulkerBoxBlockEntity;
import net.minecraft.world.phys.Vec3;
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;
import sh.grimmer.bettershulkers.interfaces.BsShulkerBoxRenderState;
import sh.grimmer.bettershulkers.render.ShulkerHintData;
import sh.grimmer.bettershulkers.render.ShulkerHintRenderer;
import sh.grimmer.bettershulkers.util.ItemRenderHelper;

@Mixin(ShulkerBoxRenderer.class)
public abstract class ShulkerBoxRendererMixin implements BlockEntityRenderer<ShulkerBoxBlockEntity, ShulkerBoxRenderState> {

    @Inject(method = "extractRenderState(Lnet/minecraft/world/level/block/entity/ShulkerBoxBlockEntity;Lnet/minecraft/client/renderer/blockentity/state/ShulkerBoxRenderState;FLnet/minecraft/world/phys/Vec3;Lnet/minecraft/client/renderer/feature/ModelFeatureRenderer$CrumblingOverlay;)V", at = @At("HEAD"))
    private void onExtractRenderState(ShulkerBoxBlockEntity be, ShulkerBoxRenderState state, float partialTick, Vec3 vec, ModelFeatureRenderer.CrumblingOverlay overlay, CallbackInfo ci) {
        if (!(state instanceof BsShulkerBoxRenderState bsState)) {
            throw new IllegalStateException("ShulkerBoxRenderState does not implement BsShulkerBoxRenderState");
        }

        if (!BetterShulkers.CONFIG.showBlockHint) {
            bsState.betterShulkers$setLabel(null);
            bsState.betterShulkers$setDisplayItem(null);
            return;
        }

        ShulkerHintData hintData = ShulkerHintData.fromShulkerBox(be.getItems(), be.getCustomName());
        bsState.betterShulkers$setLabel(hintData.getLabelFormatted());

        ItemStack displayItem = ShulkerHintRenderer.createMarkedStack(hintData.getDisplayItem());
        ItemStackRenderState renderState = ItemRenderHelper.createRenderState(be.getLevel(), be.getBlockPos(), displayItem);
        bsState.betterShulkers$setDisplayItem(renderState);
    }

    @Inject(method = "submit(Lnet/minecraft/client/renderer/blockentity/state/ShulkerBoxRenderState;Lcom/mojang/blaze3d/vertex/PoseStack;Lnet/minecraft/client/renderer/SubmitNodeCollector;Lnet/minecraft/client/renderer/state/CameraRenderState;)V", at = @At("RETURN"))
    private void onSubmit(ShulkerBoxRenderState state, PoseStack poseStack, SubmitNodeCollector collector, CameraRenderState camera, CallbackInfo ci) {
        ShulkerHintRenderer.submitBlockHint(state, poseStack, collector, camera);
    }
}
