package com.r3944realms.leashedplayer.client.renders.entities;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Axis;
import net.minecraft.client.model.EntityModel;
import net.minecraft.client.model.PlayerModel;
import net.minecraft.client.renderer.ItemInHandRenderer;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.RenderLayerParent;
import net.minecraft.client.renderer.entity.layers.RenderLayer;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;

public class ChestItemLayerRenderer<T extends LivingEntity, M extends EntityModel<T>> extends RenderLayer<T, M> {

    private final ItemInHandRenderer heldItemRenderer;

    public ChestItemLayerRenderer(RenderLayerParent<T, M> context, ItemInHandRenderer heldItemRenderer) {
        super(context);
        this.heldItemRenderer = heldItemRenderer;
    }

    @Override
    public void render(@NotNull PoseStack matrices, @NotNull MultiBufferSource vertexConsumers, int light, @NotNull T entity, float limbAngle, float limbDistance, float tickDelta, float animationProgress, float headYaw, float headPitch) {
        ItemDisplayContext mode = ItemDisplayContext.FIXED;
        ItemStack chestStack = entity.getItemBySlot(EquipmentSlot.CHEST);
        if (!chestStack.isEmpty()) {
            if (!(entity.getEquipmentSlotForItem(chestStack).equals(EquipmentSlot.CHEST))) {
                matrices.pushPose();
                ((PlayerModel<?>) this.getParentModel()).body.translateAndRotate(matrices);
                matrices.mulPose(Axis.XP.rotationDegrees(180));
                matrices.translate(0, -1 / 4f, 0);
                matrices.mulPose(Axis.YP.rotationDegrees(180));
                this.heldItemRenderer.renderItem(entity, chestStack, mode, false, matrices, vertexConsumers, light);
                matrices.scale(1.01f, 1.01f, 1.01f);
                matrices.translate(0, -1 / 4f, 0);
                this.heldItemRenderer.renderItem(entity, chestStack, mode, false, matrices, vertexConsumers, light);
                matrices.popPose();
                matrices.pushPose();
                ((PlayerModel<?>) this.getParentModel()).rightArm.translateAndRotate(matrices);
                matrices.mulPose(Axis.XP.rotationDegrees(180));
                matrices.scale(2/3f, 2/3f, 2/3f);
                matrices.translate(-1/12f, 0, 0);
                matrices.mulPose(Axis.YP.rotationDegrees(180));
                this.heldItemRenderer.renderItem(entity, chestStack, mode, false, matrices, vertexConsumers, light);
                matrices.scale(0.99f, 0.99f, 0.99f);
                matrices.translate(0, -1/2f, 0);
                this.heldItemRenderer.renderItem(entity, chestStack, mode, false, matrices, vertexConsumers, light);
                matrices.popPose();
                matrices.pushPose();
                ((PlayerModel<?>) this.getParentModel()).leftArm.translateAndRotate(matrices);
                matrices.mulPose(Axis.XP.rotationDegrees(180));
                matrices.scale(2/3f, 2/3f, 2/3f);
                matrices.translate(1/12f, 0, 0);
                matrices.mulPose(Axis.YP.rotationDegrees(180));
                this.heldItemRenderer.renderItem(entity, chestStack, mode, false, matrices, vertexConsumers, light);
                matrices.scale(0.99f, 0.99f, 0.99f);
                matrices.translate(0, -1/2f, 0);
                this.heldItemRenderer.renderItem(entity, chestStack, mode, false, matrices, vertexConsumers, light);
                matrices.popPose();
            }
        }
        ItemStack legsStack = entity.getItemBySlot(EquipmentSlot.LEGS);
        if (!legsStack.isEmpty()) {
            if (!(entity.getEquipmentSlotForItem(legsStack).equals(EquipmentSlot.LEGS))) {
                matrices.pushPose();
                ((PlayerModel<?>) this.getParentModel()).rightLeg.translateAndRotate(matrices);
                matrices.mulPose(Axis.XP.rotationDegrees(180));
                matrices.scale(2/3f, 2/3f, 2/3f);
                matrices.translate(0, -1/6f, 0);
                matrices.mulPose(Axis.YP.rotationDegrees(180));
                this.heldItemRenderer.renderItem(entity, legsStack, mode, false, matrices, vertexConsumers, light);
                matrices.scale(1.01f, 1.01f, 1.01f);
                matrices.translate(0, -1/3f, 0);
                this.heldItemRenderer.renderItem(entity, legsStack, mode, false, matrices, vertexConsumers, light);
                matrices.popPose();
                matrices.pushPose();
                ((PlayerModel<?>) this.getParentModel()).leftLeg.translateAndRotate(matrices);
                matrices.mulPose(Axis.XP.rotationDegrees(180));
                matrices.scale(2/3f, 2/3f, 2/3f);
                matrices.translate(0, -1/6f, 0);
                matrices.mulPose(Axis.YP.rotationDegrees(180));
                this.heldItemRenderer.renderItem(entity, legsStack, mode, false, matrices, vertexConsumers, light);
                matrices.scale(1.01f, 1.01f, 1.01f);
                matrices.translate(0, -1/3f, 0);
                this.heldItemRenderer.renderItem(entity, legsStack, mode, false, matrices, vertexConsumers, light);
                matrices.popPose();
            }
        }
        ItemStack feetStack = entity.getItemBySlot(EquipmentSlot.FEET);
        if (!feetStack.isEmpty()) {
            if (!(entity.getEquipmentSlotForItem(feetStack).equals(EquipmentSlot.FEET))) {
                matrices.pushPose();
                ((PlayerModel<?>) this.getParentModel()).rightLeg.translateAndRotate(matrices);
                matrices.mulPose(Axis.XP.rotationDegrees(180));
                matrices.scale(0.75f, 0.75f, 0.75f);
                matrices.translate(0, -0.8f, 0);
                matrices.mulPose(Axis.YP.rotationDegrees(180));
                this.heldItemRenderer.renderItem(entity, feetStack, mode, false, matrices, vertexConsumers, light);
                matrices.popPose();
                matrices.pushPose();
                ((PlayerModel<?>) this.getParentModel()).leftLeg.translateAndRotate(matrices);
                matrices.mulPose(Axis.XP.rotationDegrees(180));
                matrices.scale(0.75f, 0.75f, 0.75f);
                matrices.translate(0, -0.8f, 0);
                matrices.mulPose(Axis.YP.rotationDegrees(180));
                this.heldItemRenderer.renderItem(entity, feetStack, mode, false, matrices, vertexConsumers, light);
                matrices.popPose();
            }
        }
    }
}