package com.remains_cave.item.client.layer;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.remains_cave.item.client.BonePauldronArmorRenderer;
import com.remains_cave.item.client.TombQuiverArmorRenderer;
import com.remains_cave.item.custom.BonePauldronItem;
import com.remains_cave.item.custom.TombQuiverItem;
import net.minecraft.client.model.PlayerModel;
import net.minecraft.client.player.AbstractClientPlayer;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.entity.ItemRenderer;
import net.minecraft.client.renderer.entity.LivingEntityRenderer;
import net.minecraft.client.renderer.entity.layers.RenderLayer;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;

public class BackItemRenderLayer extends RenderLayer<AbstractClientPlayer, PlayerModel<AbstractClientPlayer>> {
    private final TombQuiverArmorRenderer quiverRenderer = new TombQuiverArmorRenderer();
    private final BonePauldronArmorRenderer pauldronRenderer = new BonePauldronArmorRenderer();

    public BackItemRenderLayer(LivingEntityRenderer<AbstractClientPlayer, PlayerModel<AbstractClientPlayer>> renderer) {
        super(renderer);
    }

    @Override
    public void render(@NotNull PoseStack poseStack, @NotNull MultiBufferSource bufferSource, int packedLight,
                       AbstractClientPlayer player, float limbSwing, float limbSwingAmount,
                       float partialTick, float ageInTicks, float netHeadYaw, float headPitch) {
        ItemStack offhand = player.getOffhandItem();
        if (offhand.isEmpty()) return;

        if (offhand.getItem() instanceof TombQuiverItem) {
            renderQuiver(poseStack, bufferSource, packedLight, player,
                    limbSwing, limbSwingAmount, partialTick, netHeadYaw, headPitch, offhand);
        } else if (offhand.getItem() instanceof BonePauldronItem) {
            renderPauldron(poseStack, bufferSource, packedLight, player,
                    limbSwing, limbSwingAmount, partialTick, netHeadYaw, headPitch, offhand);
        }
    }

    private void renderQuiver(PoseStack poseStack, MultiBufferSource bufferSource, int packedLight,
                              AbstractClientPlayer player, float limbSwing, float limbSwingAmount,
                              float partialTick, float netHeadYaw, float headPitch, ItemStack offhand) {
        quiverRenderer.prepForRender(player, offhand, EquipmentSlot.OFFHAND,
                getParentModel(), bufferSource, partialTick,
                limbSwing, limbSwingAmount, netHeadYaw, headPitch);

        // 不再手动应用身体变换，直接让 GeoArmorRenderer 处理
        RenderType renderType = quiverRenderer.getRenderType(
                quiverRenderer.getAnimatable(),
                quiverRenderer.getTextureLocation(quiverRenderer.getAnimatable()),
                bufferSource, partialTick);
        VertexConsumer buffer = null;
        if (renderType != null) {
            buffer = ItemRenderer.getFoilBuffer(bufferSource, renderType, offhand.hasFoil(), true);
        }
        quiverRenderer.renderToBuffer(poseStack, buffer, packedLight,
                LivingEntityRenderer.getOverlayCoords(player, 0.0F), 0xFFFFFFFF);
    }

    private void renderPauldron(PoseStack poseStack, MultiBufferSource bufferSource, int packedLight,
                                AbstractClientPlayer player, float limbSwing, float limbSwingAmount,
                                float partialTick, float netHeadYaw, float headPitch, ItemStack offhand) {
        pauldronRenderer.prepForRender(player, offhand, EquipmentSlot.OFFHAND,
                getParentModel(), bufferSource, partialTick,
                limbSwing, limbSwingAmount, netHeadYaw, headPitch);

        // 同样移除手动变换
        RenderType renderType = pauldronRenderer.getRenderType(
                pauldronRenderer.getAnimatable(),
                pauldronRenderer.getTextureLocation(pauldronRenderer.getAnimatable()),
                bufferSource, partialTick);
        VertexConsumer buffer = null;
        if (renderType != null) {
            buffer = ItemRenderer.getFoilBuffer(bufferSource, renderType, offhand.hasFoil(), true);
        }
        pauldronRenderer.renderToBuffer(poseStack, buffer, packedLight,
                LivingEntityRenderer.getOverlayCoords(player, 0.0F), 0xFFFFFFFF);
    }
}