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

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Axis;
import com.r3944realms.leashedplayer.modInterface.IPlayerRenderStateExtension;
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.client.renderer.entity.state.PlayerRenderState;
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<S extends PlayerRenderState, M extends EntityModel<S>> extends RenderLayer<S, M> {

    private final ItemInHandRenderer heldItemRenderer;

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


    @Override
    public void render(@NotNull PoseStack poseStack, @NotNull MultiBufferSource bufferSource, int packedLight, @NotNull S renderState, float yRot, float xRot) {
        ItemDisplayContext mode = ItemDisplayContext.FIXED;
        IPlayerRenderStateExtension rs = (IPlayerRenderStateExtension) renderState;
        LivingEntity entity = rs.getPlayerSlotItemLayerState().entity;
        ItemStack chestStack = entity.getItemBySlot(EquipmentSlot.CHEST);
        if (!chestStack.isEmpty()) {
            if (!(entity.getEquipmentSlotForItem(chestStack).equals(EquipmentSlot.CHEST))) {
                poseStack.pushPose();
                ((PlayerModel) this.getParentModel()).body.translateAndRotate(poseStack);
                poseStack.mulPose(Axis.XP.rotationDegrees(180));
                poseStack.translate(0, -0.24f, 0);
                poseStack.mulPose(Axis.YP.rotationDegrees(180));
                this.heldItemRenderer.renderItem(entity, chestStack, mode, poseStack, bufferSource, packedLight);
                poseStack.scale(1.01f, 1.01f, 1.01f);
                poseStack.translate(0, -1 / 4f, 0);
                this.heldItemRenderer.renderItem(entity, chestStack, mode, poseStack, bufferSource, packedLight);
                poseStack.popPose();
                poseStack.pushPose();
                ((PlayerModel) this.getParentModel()).rightArm.translateAndRotate(poseStack);
                poseStack.mulPose(Axis.XP.rotationDegrees(180));
                poseStack.scale(2/3f, 2/3f, 2/3f);
                poseStack.translate(-1/12f, 0, 0);
                poseStack.mulPose(Axis.YP.rotationDegrees(180));
                this.heldItemRenderer.renderItem(entity, chestStack, mode, poseStack, bufferSource, packedLight);
                poseStack.scale(0.99f, 0.99f, 0.99f);
                poseStack.translate(0, -1/2f, 0);
                this.heldItemRenderer.renderItem(entity, chestStack, mode, poseStack, bufferSource, packedLight);
                poseStack.popPose();
                poseStack.pushPose();
                ((PlayerModel) this.getParentModel()).leftArm.translateAndRotate(poseStack);
                poseStack.mulPose(Axis.XP.rotationDegrees(180));
                poseStack.scale(2/3f, 2/3f, 2/3f);
                poseStack.translate(1/12f, 0, 0);
                poseStack.mulPose(Axis.YP.rotationDegrees(180));
                this.heldItemRenderer.renderItem(entity, chestStack, mode, poseStack, bufferSource, packedLight);
                poseStack.scale(0.99f, 0.99f, 0.99f);
                poseStack.translate(0, -1/2f, 0);
                this.heldItemRenderer.renderItem(entity, chestStack, mode, poseStack, bufferSource, packedLight);
                poseStack.popPose();
            }
        }
        ItemStack legsStack = entity.getItemBySlot(EquipmentSlot.LEGS);
        if (!legsStack.isEmpty()) {
            if (!(entity.getEquipmentSlotForItem(legsStack).equals(EquipmentSlot.LEGS))) {
                poseStack.pushPose();
                ((PlayerModel) this.getParentModel()).rightLeg.translateAndRotate(poseStack);
                poseStack.mulPose(Axis.XP.rotationDegrees(180));
                poseStack.scale(2/3f, 2/3f, 2/3f);
                poseStack.translate(0, -1/6f, 0);
                poseStack.mulPose(Axis.YP.rotationDegrees(180));
                this.heldItemRenderer.renderItem(entity, legsStack, mode, poseStack, bufferSource, packedLight);
                poseStack.scale(1.01f, 1.01f, 1.01f);
                poseStack.translate(0, -1/3f, 0);
                this.heldItemRenderer.renderItem(entity, legsStack, mode, poseStack, bufferSource, packedLight);
                poseStack.popPose();
                poseStack.pushPose();
                ((PlayerModel) this.getParentModel()).leftLeg.translateAndRotate(poseStack);
                poseStack.mulPose(Axis.XP.rotationDegrees(180));
                poseStack.scale(2/3f, 2/3f, 2/3f);
                poseStack.translate(0, -1/6f, 0);
                poseStack.mulPose(Axis.YP.rotationDegrees(180));
                this.heldItemRenderer.renderItem(entity, legsStack, mode, poseStack, bufferSource, packedLight);
                poseStack.scale(1.01f, 1.01f, 1.01f);
                poseStack.translate(0, -1/3f, 0);
                this.heldItemRenderer.renderItem(entity, legsStack, mode, poseStack, bufferSource, packedLight);
                poseStack.popPose();
            }
        }
        ItemStack feetStack = entity.getItemBySlot(EquipmentSlot.FEET);
        if (!feetStack.isEmpty()) {
            if (!(entity.getEquipmentSlotForItem(feetStack).equals(EquipmentSlot.FEET))) {
                poseStack.pushPose();
                ((PlayerModel) this.getParentModel()).rightLeg.translateAndRotate(poseStack);
                poseStack.mulPose(Axis.XP.rotationDegrees(180));
                poseStack.scale(0.75f, 0.75f, 0.75f);
                poseStack.translate(0, -0.8f, 0);
                poseStack.mulPose(Axis.YP.rotationDegrees(180));
                this.heldItemRenderer.renderItem(entity, feetStack, mode, poseStack, bufferSource, packedLight);
                poseStack.popPose();
                poseStack.pushPose();
                ((PlayerModel) this.getParentModel()).leftLeg.translateAndRotate(poseStack);
                poseStack.mulPose(Axis.XP.rotationDegrees(180));
                poseStack.scale(0.75f, 0.75f, 0.75f);
                poseStack.translate(0, -0.8f, 0);
                poseStack.mulPose(Axis.YP.rotationDegrees(180));
                this.heldItemRenderer.renderItem(entity, feetStack, mode, poseStack, bufferSource, packedLight);
                poseStack.popPose();
            }
        }
    }
}