package net.minecraft.client.renderer.entity;

import net.minecraft.world.entity.Entity;
import net.minecraft.client.renderer.culling.Frustum;
import net.minecraft.util.Mth;
import net.minecraft.core.BlockPos;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.math.Quaternion;
import com.mojang.math.Vector3f;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.client.renderer.MultiBufferSource;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.model.geom.ModelPart;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.boss.enderdragon.EndCrystal;

public class EndCrystalRenderer extends EntityRenderer<EndCrystal> {
    private static final ResourceLocation END_CRYSTAL_LOCATION;
    private static final RenderType RENDER_TYPE;
    private static final float SIN_45;
    private final ModelPart cube;
    private final ModelPart glass;
    private final ModelPart base;
    
    public EndCrystalRenderer(final EntityRenderDispatcher dvf) {
        super(dvf);
        this.shadowRadius = 0.5f;
        (this.glass = new ModelPart(64, 32, 0, 0)).addBox(-4.0f, -4.0f, -4.0f, 8.0f, 8.0f, 8.0f);
        (this.cube = new ModelPart(64, 32, 32, 0)).addBox(-4.0f, -4.0f, -4.0f, 8.0f, 8.0f, 8.0f);
        (this.base = new ModelPart(64, 32, 0, 16)).addBox(-6.0f, 0.0f, -6.0f, 12.0f, 4.0f, 12.0f);
    }
    
    @Override
    public void render(final EndCrystal aur, final float float2, final float float3, final PoseStack cyl, final MultiBufferSource dqt, final int integer) {
        cyl.pushPose();
        final float float4 = getY(aur, float3);
        final float float5 = (aur.time + float3) * 3.0f;
        final VertexConsumer cyo10 = dqt.getBuffer(EndCrystalRenderer.RENDER_TYPE);
        cyl.pushPose();
        cyl.scale(2.0f, 2.0f, 2.0f);
        cyl.translate(0.0, -0.5, 0.0);
        final int integer2 = OverlayTexture.NO_OVERLAY;
        if (aur.showsBottom()) {
            this.base.render(cyl, cyo10, integer, integer2);
        }
        cyl.mulPose(Vector3f.YP.rotationDegrees(float5));
        cyl.translate(0.0, 1.5f + float4 / 2.0f, 0.0);
        cyl.mulPose(new Quaternion(new Vector3f(EndCrystalRenderer.SIN_45, 0.0f, EndCrystalRenderer.SIN_45), 60.0f, true));
        this.glass.render(cyl, cyo10, integer, integer2);
        final float float6 = 0.875f;
        cyl.scale(0.875f, 0.875f, 0.875f);
        cyl.mulPose(new Quaternion(new Vector3f(EndCrystalRenderer.SIN_45, 0.0f, EndCrystalRenderer.SIN_45), 60.0f, true));
        cyl.mulPose(Vector3f.YP.rotationDegrees(float5));
        this.glass.render(cyl, cyo10, integer, integer2);
        cyl.scale(0.875f, 0.875f, 0.875f);
        cyl.mulPose(new Quaternion(new Vector3f(EndCrystalRenderer.SIN_45, 0.0f, EndCrystalRenderer.SIN_45), 60.0f, true));
        cyl.mulPose(Vector3f.YP.rotationDegrees(float5));
        this.cube.render(cyl, cyo10, integer, integer2);
        cyl.popPose();
        cyl.popPose();
        final BlockPos fk13 = aur.getBeamTarget();
        if (fk13 != null) {
            final float float7 = fk13.getX() + 0.5f;
            final float float8 = fk13.getY() + 0.5f;
            final float float9 = fk13.getZ() + 0.5f;
            final float float10 = (float)(float7 - aur.getX());
            final float float11 = (float)(float8 - aur.getY());
            final float float12 = (float)(float9 - aur.getZ());
            cyl.translate(float10, float11, float12);
            EnderDragonRenderer.renderCrystalBeams(-float10, -float11 + float4, -float12, float3, aur.time, cyl, dqt, integer);
        }
        super.render(aur, float2, float3, cyl, dqt, integer);
    }
    
    public static float getY(final EndCrystal aur, final float float2) {
        final float float3 = aur.time + float2;
        float float4 = Mth.sin(float3 * 0.2f) / 2.0f + 0.5f;
        float4 = (float4 * float4 + float4) * 0.4f;
        return float4 - 1.4f;
    }
    
    @Override
    public ResourceLocation getTextureLocation(final EndCrystal aur) {
        return EndCrystalRenderer.END_CRYSTAL_LOCATION;
    }
    
    @Override
    public boolean shouldRender(final EndCrystal aur, final Frustum dtm, final double double3, final double double4, final double double5) {
        return super.shouldRender(aur, dtm, double3, double4, double5) || aur.getBeamTarget() != null;
    }
    
    static {
        END_CRYSTAL_LOCATION = new ResourceLocation("textures/entity/end_crystal/end_crystal.png");
        RENDER_TYPE = RenderType.entityCutoutNoCull(EndCrystalRenderer.END_CRYSTAL_LOCATION);
        SIN_45 = (float)Math.sin(0.7853981633974483);
    }
}
