package net.minecraft.client.renderer;

import com.mojang.math.Vector3f;
import net.minecraft.util.Mth;
import net.minecraft.client.resources.model.ModelBakery;
import com.mojang.math.Matrix4f;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.BufferUploader;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.Tesselator;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import javax.annotation.Nullable;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.entity.player.Player;
import net.minecraft.tags.FluidTags;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;

public class ScreenEffectRenderer {
    private static final ResourceLocation UNDERWATER_LOCATION;
    
    public static void renderScreenEffect(final Minecraft dbn, final PoseStack cyl) {
        RenderSystem.disableAlphaTest();
        final Player ayj3 = dbn.player;
        if (!ayj3.noPhysics) {
            final BlockState byj4 = getViewBlockingState(ayj3);
            if (byj4 != null) {
                renderTex(dbn, dbn.getBlockRenderer().getBlockModelShaper().getParticleIcon(byj4), cyl);
            }
        }
        if (!dbn.player.isSpectator()) {
            if (dbn.player.isUnderLiquid(FluidTags.WATER)) {
                renderWater(dbn, cyl);
            }
            if (dbn.player.isOnFire()) {
                renderFire(dbn, cyl);
            }
        }
        RenderSystem.enableAlphaTest();
    }
    
    @Nullable
    private static BlockState getViewBlockingState(final Player ayj) {
        final BlockPos.MutableBlockPos a2 = new BlockPos.MutableBlockPos();
        for (int integer3 = 0; integer3 < 8; ++integer3) {
            final double double4 = ayj.getX() + ((integer3 >> 0) % 2 - 0.5f) * ayj.getBbWidth() * 0.8f;
            final double double5 = ayj.getEyeY() + ((integer3 >> 1) % 2 - 0.5f) * 0.1f;
            final double double6 = ayj.getZ() + ((integer3 >> 2) % 2 - 0.5f) * ayj.getBbWidth() * 0.8f;
            a2.set(double4, double5, double6);
            final BlockState byj10 = ayj.level.getBlockState(a2);
            if (byj10.getRenderShape() != RenderShape.INVISIBLE && byj10.isViewBlocking(ayj.level, a2)) {
                return byj10;
            }
        }
        return null;
    }
    
    private static void renderTex(final Minecraft dbn, final TextureAtlasSprite eaj, final PoseStack cyl) {
        dbn.getTextureManager().bind(eaj.atlas().location());
        final BufferBuilder cyg4 = Tesselator.getInstance().getBuilder();
        final float float5 = 0.1f;
        final float float6 = -1.0f;
        final float float7 = 1.0f;
        final float float8 = -1.0f;
        final float float9 = 1.0f;
        final float float10 = -0.5f;
        final float float11 = eaj.getU0();
        final float float12 = eaj.getU1();
        final float float13 = eaj.getV0();
        final float float14 = eaj.getV1();
        final Matrix4f b15 = cyl.last().pose();
        cyg4.begin(7, DefaultVertexFormat.POSITION_COLOR_TEX);
        cyg4.vertex(b15, -1.0f, -1.0f, -0.5f).color(0.1f, 0.1f, 0.1f, 1.0f).uv(float12, float14).endVertex();
        cyg4.vertex(b15, 1.0f, -1.0f, -0.5f).color(0.1f, 0.1f, 0.1f, 1.0f).uv(float11, float14).endVertex();
        cyg4.vertex(b15, 1.0f, 1.0f, -0.5f).color(0.1f, 0.1f, 0.1f, 1.0f).uv(float11, float13).endVertex();
        cyg4.vertex(b15, -1.0f, 1.0f, -0.5f).color(0.1f, 0.1f, 0.1f, 1.0f).uv(float12, float13).endVertex();
        cyg4.end();
        BufferUploader.end(cyg4);
    }
    
    private static void renderWater(final Minecraft dbn, final PoseStack cyl) {
        dbn.getTextureManager().bind(ScreenEffectRenderer.UNDERWATER_LOCATION);
        final BufferBuilder cyg3 = Tesselator.getInstance().getBuilder();
        final float float4 = dbn.player.getBrightness();
        RenderSystem.enableBlend();
        RenderSystem.defaultBlendFunc();
        final float float5 = 4.0f;
        final float float6 = -1.0f;
        final float float7 = 1.0f;
        final float float8 = -1.0f;
        final float float9 = 1.0f;
        final float float10 = -0.5f;
        final float float11 = -dbn.player.yRot / 64.0f;
        final float float12 = dbn.player.xRot / 64.0f;
        final Matrix4f b13 = cyl.last().pose();
        cyg3.begin(7, DefaultVertexFormat.POSITION_COLOR_TEX);
        cyg3.vertex(b13, -1.0f, -1.0f, -0.5f).color(float4, float4, float4, 0.1f).uv(4.0f + float11, 4.0f + float12).endVertex();
        cyg3.vertex(b13, 1.0f, -1.0f, -0.5f).color(float4, float4, float4, 0.1f).uv(0.0f + float11, 4.0f + float12).endVertex();
        cyg3.vertex(b13, 1.0f, 1.0f, -0.5f).color(float4, float4, float4, 0.1f).uv(0.0f + float11, 0.0f + float12).endVertex();
        cyg3.vertex(b13, -1.0f, 1.0f, -0.5f).color(float4, float4, float4, 0.1f).uv(4.0f + float11, 0.0f + float12).endVertex();
        cyg3.end();
        BufferUploader.end(cyg3);
        RenderSystem.disableBlend();
    }
    
    private static void renderFire(final Minecraft dbn, final PoseStack cyl) {
        final BufferBuilder cyg3 = Tesselator.getInstance().getBuilder();
        RenderSystem.depthFunc(519);
        RenderSystem.depthMask(false);
        RenderSystem.enableBlend();
        RenderSystem.defaultBlendFunc();
        final TextureAtlasSprite eaj4 = ModelBakery.FIRE_1.sprite();
        dbn.getTextureManager().bind(eaj4.atlas().location());
        final float float5 = eaj4.getU0();
        final float float6 = eaj4.getU1();
        final float float7 = (float5 + float6) / 2.0f;
        final float float8 = eaj4.getV0();
        final float float9 = eaj4.getV1();
        final float float10 = (float8 + float9) / 2.0f;
        final float float11 = eaj4.uvShrinkRatio();
        final float float12 = Mth.lerp(float11, float5, float7);
        final float float13 = Mth.lerp(float11, float6, float7);
        final float float14 = Mth.lerp(float11, float8, float10);
        final float float15 = Mth.lerp(float11, float9, float10);
        final float float16 = 1.0f;
        for (int integer17 = 0; integer17 < 2; ++integer17) {
            cyl.pushPose();
            final float float17 = -0.5f;
            final float float18 = 0.5f;
            final float float19 = -0.5f;
            final float float20 = 0.5f;
            final float float21 = -0.5f;
            cyl.translate(-(integer17 * 2 - 1) * 0.24f, -0.30000001192092896, 0.0);
            cyl.mulPose(Vector3f.YP.rotationDegrees((integer17 * 2 - 1) * 10.0f));
            final Matrix4f b23 = cyl.last().pose();
            cyg3.begin(7, DefaultVertexFormat.POSITION_COLOR_TEX);
            cyg3.vertex(b23, -0.5f, -0.5f, -0.5f).color(1.0f, 1.0f, 1.0f, 0.9f).uv(float13, float15).endVertex();
            cyg3.vertex(b23, 0.5f, -0.5f, -0.5f).color(1.0f, 1.0f, 1.0f, 0.9f).uv(float12, float15).endVertex();
            cyg3.vertex(b23, 0.5f, 0.5f, -0.5f).color(1.0f, 1.0f, 1.0f, 0.9f).uv(float12, float14).endVertex();
            cyg3.vertex(b23, -0.5f, 0.5f, -0.5f).color(1.0f, 1.0f, 1.0f, 0.9f).uv(float13, float14).endVertex();
            cyg3.end();
            BufferUploader.end(cyg3);
            cyl.popPose();
        }
        RenderSystem.disableBlend();
        RenderSystem.depthMask(true);
        RenderSystem.depthFunc(515);
    }
    
    static {
        UNDERWATER_LOCATION = new ResourceLocation("textures/misc/underwater.png");
    }
}
