package net.minecraft.client.renderer.entity.player;

import com.mojang.math.Vector3f;
import net.minecraft.util.Mth;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.model.geom.ModelPart;
import net.minecraft.world.scores.Score;
import net.minecraft.world.scores.Objective;
import net.minecraft.world.scores.Scoreboard;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.entity.Entity;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.CrossbowItem;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.UseAnim;
import net.minecraft.world.entity.HumanoidArm;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.PlayerModelPart;
import net.minecraft.world.phys.Vec3;
import net.minecraft.client.renderer.MultiBufferSource;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.entity.layers.BeeStingerLayer;
import net.minecraft.client.renderer.entity.layers.SpinAttackEffectLayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.client.renderer.entity.layers.ParrotOnShoulderLayer;
import net.minecraft.client.renderer.entity.layers.ElytraLayer;
import net.minecraft.client.renderer.entity.layers.CustomHeadLayer;
import net.minecraft.client.renderer.entity.layers.CapeLayer;
import net.minecraft.client.renderer.entity.layers.Deadmau5EarsLayer;
import net.minecraft.client.renderer.entity.layers.ArrowLayer;
import net.minecraft.client.renderer.entity.layers.ItemInHandLayer;
import net.minecraft.client.renderer.entity.layers.RenderLayer;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.client.renderer.entity.RenderLayerParent;
import net.minecraft.client.renderer.entity.layers.HumanoidArmorLayer;
import net.minecraft.client.model.HumanoidModel;
import net.minecraft.client.renderer.entity.EntityRenderDispatcher;
import net.minecraft.client.model.PlayerModel;
import net.minecraft.client.player.AbstractClientPlayer;
import net.minecraft.client.renderer.entity.LivingEntityRenderer;

public class PlayerRenderer extends LivingEntityRenderer<AbstractClientPlayer, PlayerModel<AbstractClientPlayer>> {
    public PlayerRenderer(final EntityRenderDispatcher egm) {
        this(egm, false);
    }
    
    public PlayerRenderer(final EntityRenderDispatcher egm, final boolean boolean2) {
        super(egm, new PlayerModel(0.0f, boolean2), 0.5f);
        this.addLayer((RenderLayer<AbstractClientPlayer, PlayerModel<AbstractClientPlayer>>)new HumanoidArmorLayer((RenderLayerParent<LivingEntity, HumanoidModel>)this, new HumanoidModel(0.5f), new HumanoidModel(1.0f)));
        this.addLayer(new ItemInHandLayer<AbstractClientPlayer, PlayerModel<AbstractClientPlayer>>(this));
        this.addLayer(new ArrowLayer<AbstractClientPlayer, PlayerModel<AbstractClientPlayer>>(this));
        this.addLayer(new Deadmau5EarsLayer(this));
        this.addLayer(new CapeLayer(this));
        this.addLayer(new CustomHeadLayer<AbstractClientPlayer, PlayerModel<AbstractClientPlayer>>(this));
        this.addLayer(new ElytraLayer<AbstractClientPlayer, PlayerModel<AbstractClientPlayer>>(this));
        this.addLayer((RenderLayer<AbstractClientPlayer, PlayerModel<AbstractClientPlayer>>)new ParrotOnShoulderLayer((RenderLayerParent<Player, PlayerModel<Player>>)this));
        this.addLayer((RenderLayer<AbstractClientPlayer, PlayerModel<AbstractClientPlayer>>)new SpinAttackEffectLayer((RenderLayerParent<LivingEntity, PlayerModel<LivingEntity>>)this));
        this.addLayer(new BeeStingerLayer<AbstractClientPlayer, PlayerModel<AbstractClientPlayer>>(this));
    }
    
    @Override
    public void render(final AbstractClientPlayer ebc, final float float2, final float float3, final PoseStack dhl, final MultiBufferSource ebz, final int integer) {
        this.setModelProperties(ebc);
        super.render(ebc, float2, float3, dhl, ebz, integer);
    }
    
    public Vec3 getRenderOffset(final AbstractClientPlayer ebc, final float float2) {
        if (ebc.isCrouching()) {
            return new Vec3(0.0, -0.125, 0.0);
        }
        return super.getRenderOffset((T)ebc, float2);
    }
    
    private void setModelProperties(final AbstractClientPlayer ebc) {
        final PlayerModel<AbstractClientPlayer> dww3 = ((LivingEntityRenderer<T, PlayerModel<AbstractClientPlayer>>)this).getModel();
        if (ebc.isSpectator()) {
            dww3.setAllVisible(false);
            dww3.head.visible = true;
            dww3.hat.visible = true;
        }
        else {
            dww3.setAllVisible(true);
            dww3.hat.visible = ebc.isModelPartShown(PlayerModelPart.HAT);
            dww3.jacket.visible = ebc.isModelPartShown(PlayerModelPart.JACKET);
            dww3.leftPants.visible = ebc.isModelPartShown(PlayerModelPart.LEFT_PANTS_LEG);
            dww3.rightPants.visible = ebc.isModelPartShown(PlayerModelPart.RIGHT_PANTS_LEG);
            dww3.leftSleeve.visible = ebc.isModelPartShown(PlayerModelPart.LEFT_SLEEVE);
            dww3.rightSleeve.visible = ebc.isModelPartShown(PlayerModelPart.RIGHT_SLEEVE);
            dww3.crouching = ebc.isCrouching();
            final HumanoidModel.ArmPose a4 = getArmPose(ebc, InteractionHand.MAIN_HAND);
            HumanoidModel.ArmPose a5 = getArmPose(ebc, InteractionHand.OFF_HAND);
            if (a4.isTwoHanded()) {
                a5 = (ebc.getOffhandItem().isEmpty() ? HumanoidModel.ArmPose.EMPTY : HumanoidModel.ArmPose.ITEM);
            }
            if (ebc.getMainArm() == HumanoidArm.RIGHT) {
                dww3.rightArmPose = a4;
                dww3.leftArmPose = a5;
            }
            else {
                dww3.rightArmPose = a5;
                dww3.leftArmPose = a4;
            }
        }
    }
    
    private static HumanoidModel.ArmPose getArmPose(final AbstractClientPlayer ebc, final InteractionHand anf) {
        final ItemStack bki3 = ebc.getItemInHand(anf);
        if (bki3.isEmpty()) {
            return HumanoidModel.ArmPose.EMPTY;
        }
        if (ebc.getUsedItemHand() == anf && ebc.getUseItemRemainingTicks() > 0) {
            final UseAnim blu4 = bki3.getUseAnimation();
            if (blu4 == UseAnim.BLOCK) {
                return HumanoidModel.ArmPose.BLOCK;
            }
            if (blu4 == UseAnim.BOW) {
                return HumanoidModel.ArmPose.BOW_AND_ARROW;
            }
            if (blu4 == UseAnim.SPEAR) {
                return HumanoidModel.ArmPose.THROW_SPEAR;
            }
            if (blu4 == UseAnim.CROSSBOW && anf == ebc.getUsedItemHand()) {
                return HumanoidModel.ArmPose.CROSSBOW_CHARGE;
            }
        }
        else if (!ebc.swinging && bki3.getItem() == Items.CROSSBOW && CrossbowItem.isCharged(bki3)) {
            return HumanoidModel.ArmPose.CROSSBOW_HOLD;
        }
        return HumanoidModel.ArmPose.ITEM;
    }
    
    @Override
    public ResourceLocation getTextureLocation(final AbstractClientPlayer ebc) {
        return ebc.getSkinTextureLocation();
    }
    
    @Override
    protected void scale(final AbstractClientPlayer ebc, final PoseStack dhl, final float float3) {
        final float float4 = 0.9375f;
        dhl.scale(0.9375f, 0.9375f, 0.9375f);
    }
    
    protected void renderNameTag(final AbstractClientPlayer ebc, final Component mr, final PoseStack dhl, final MultiBufferSource ebz, final int integer) {
        final double double7 = this.entityRenderDispatcher.distanceToSqr(ebc);
        dhl.pushPose();
        if (double7 < 100.0) {
            final Scoreboard dfm9 = ebc.getScoreboard();
            final Objective dfj10 = dfm9.getDisplayObjective(2);
            if (dfj10 != null) {
                final Score dfl11 = dfm9.getOrCreatePlayerScore(ebc.getScoreboardName(), dfj10);
                super.renderNameTag((T)ebc, new TextComponent(Integer.toString(dfl11.getScore())).append(" ").append(dfj10.getDisplayName()), dhl, ebz, integer);
                final double double8 = 0.0;
                this.getFont().getClass();
                dhl.translate(double8, 9.0f * 1.15f * 0.025f, 0.0);
            }
        }
        super.renderNameTag((T)ebc, mr, dhl, ebz, integer);
        dhl.popPose();
    }
    
    public void renderRightHand(final PoseStack dhl, final MultiBufferSource ebz, final int integer, final AbstractClientPlayer ebc) {
        this.renderHand(dhl, ebz, integer, ebc, ((PlayerModel)this.model).rightArm, ((PlayerModel)this.model).rightSleeve);
    }
    
    public void renderLeftHand(final PoseStack dhl, final MultiBufferSource ebz, final int integer, final AbstractClientPlayer ebc) {
        this.renderHand(dhl, ebz, integer, ebc, ((PlayerModel)this.model).leftArm, ((PlayerModel)this.model).leftSleeve);
    }
    
    private void renderHand(final PoseStack dhl, final MultiBufferSource ebz, final int integer, final AbstractClientPlayer ebc, final ModelPart dyg5, final ModelPart dyg6) {
        final PlayerModel<AbstractClientPlayer> dww8 = ((LivingEntityRenderer<T, PlayerModel<AbstractClientPlayer>>)this).getModel();
        this.setModelProperties(ebc);
        dww8.attackTime = 0.0f;
        dww8.crouching = false;
        dww8.setupAnim(ebc, dww8.swimAmount = 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
        dyg5.xRot = 0.0f;
        dyg5.render(dhl, ebz.getBuffer(RenderType.entitySolid(ebc.getSkinTextureLocation())), integer, OverlayTexture.NO_OVERLAY);
        dyg6.xRot = 0.0f;
        dyg6.render(dhl, ebz.getBuffer(RenderType.entityTranslucent(ebc.getSkinTextureLocation())), integer, OverlayTexture.NO_OVERLAY);
    }
    
    @Override
    protected void setupRotations(final AbstractClientPlayer ebc, final PoseStack dhl, final float float3, final float float4, final float float5) {
        final float float6 = ebc.getSwimAmount(float5);
        if (ebc.isFallFlying()) {
            super.setupRotations(ebc, dhl, float3, float4, float5);
            final float float7 = ebc.getFallFlyingTicks() + float5;
            final float float8 = Mth.clamp(float7 * float7 / 100.0f, 0.0f, 1.0f);
            if (!ebc.isAutoSpinAttack()) {
                dhl.mulPose(Vector3f.XP.rotationDegrees(float8 * (-90.0f - ebc.xRot)));
            }
            final Vec3 dem10 = ebc.getViewVector(float5);
            final Vec3 dem11 = ebc.getDeltaMovement();
            final double double12 = Entity.getHorizontalDistanceSqr(dem11);
            final double double13 = Entity.getHorizontalDistanceSqr(dem10);
            if (double12 > 0.0 && double13 > 0.0) {
                final double double14 = (dem11.x * dem10.x + dem11.z * dem10.z) / (Math.sqrt(double12) * Math.sqrt(double13));
                final double double15 = dem11.x * dem10.z - dem11.z * dem10.x;
                dhl.mulPose(Vector3f.YP.rotation((float)(Math.signum(double15) * Math.acos(double14))));
            }
        }
        else if (float6 > 0.0f) {
            super.setupRotations(ebc, dhl, float3, float4, float5);
            final float float7 = ebc.isInWater() ? (-90.0f - ebc.xRot) : -90.0f;
            final float float8 = Mth.lerp(float6, 0.0f, float7);
            dhl.mulPose(Vector3f.XP.rotationDegrees(float8));
            if (ebc.isVisuallySwimming()) {
                dhl.translate(0.0, -1.0, 0.30000001192092896);
            }
        }
        else {
            super.setupRotations(ebc, dhl, float3, float4, float5);
        }
    }
}
