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.world.level.material.Fluid;
import net.minecraft.tags.Tag;
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 dlx, final PoseStack dhl) {
        RenderSystem.disableAlphaTest();
        final Player bec3 = dlx.player;
        if (!bec3.noPhysics) {
            final BlockState cfj4 = getViewBlockingState(bec3);
            if (cfj4 != null) {
                renderTex(dlx, dlx.getBlockRenderer().getBlockModelShaper().getParticleIcon(cfj4), dhl);
            }
        }
        if (!dlx.player.isSpectator()) {
            if (dlx.player.isEyeInFluid(FluidTags.WATER)) {
                renderWater(dlx, dhl);
            }
            if (dlx.player.isOnFire()) {
                renderFire(dlx, dhl);
            }
        }
        RenderSystem.enableAlphaTest();
    }
    
    @Nullable
    private static BlockState getViewBlockingState(final Player bec) {
        final BlockPos.MutableBlockPos a2 = new BlockPos.MutableBlockPos();
        for (int integer3 = 0; integer3 < 8; ++integer3) {
            final double double4 = bec.getX() + ((integer3 >> 0) % 2 - 0.5f) * bec.getBbWidth() * 0.8f;
            final double double5 = bec.getEyeY() + ((integer3 >> 1) % 2 - 0.5f) * 0.1f;
            final double double6 = bec.getZ() + ((integer3 >> 2) % 2 - 0.5f) * bec.getBbWidth() * 0.8f;
            a2.set(double4, double5, double6);
            final BlockState cfj10 = bec.level.getBlockState(a2);
            if (cfj10.getRenderShape() != RenderShape.INVISIBLE && cfj10.isViewBlocking(bec.level, a2)) {
                return cfj10;
            }
        }
        return null;
    }
    
    private static void renderTex(final Minecraft dlx, final TextureAtlasSprite elv, final PoseStack dhl) {
        dlx.getTextureManager().bind(elv.atlas().location());
        final BufferBuilder dhg4 = 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 = elv.getU0();
        final float float12 = elv.getU1();
        final float float13 = elv.getV0();
        final float float14 = elv.getV1();
        final Matrix4f b15 = dhl.last().pose();
        dhg4.begin(7, DefaultVertexFormat.POSITION_COLOR_TEX);
        dhg4.vertex(b15, -1.0f, -1.0f, -0.5f).color(0.1f, 0.1f, 0.1f, 1.0f).uv(float12, float14).endVertex();
        dhg4.vertex(b15, 1.0f, -1.0f, -0.5f).color(0.1f, 0.1f, 0.1f, 1.0f).uv(float11, float14).endVertex();
        dhg4.vertex(b15, 1.0f, 1.0f, -0.5f).color(0.1f, 0.1f, 0.1f, 1.0f).uv(float11, float13).endVertex();
        dhg4.vertex(b15, -1.0f, 1.0f, -0.5f).color(0.1f, 0.1f, 0.1f, 1.0f).uv(float12, float13).endVertex();
        dhg4.end();
        BufferUploader.end(dhg4);
    }
    
    private static void renderWater(final Minecraft dlx, final PoseStack dhl) {
        RenderSystem.enableTexture();
        dlx.getTextureManager().bind(ScreenEffectRenderer.UNDERWATER_LOCATION);
        final BufferBuilder dhg3 = Tesselator.getInstance().getBuilder();
        final float float4 = dlx.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 = -dlx.player.yRot / 64.0f;
        final float float12 = dlx.player.xRot / 64.0f;
        final Matrix4f b13 = dhl.last().pose();
        dhg3.begin(7, DefaultVertexFormat.POSITION_COLOR_TEX);
        dhg3.vertex(b13, -1.0f, -1.0f, -0.5f).color(float4, float4, float4, 0.1f).uv(4.0f + float11, 4.0f + float12).endVertex();
        dhg3.vertex(b13, 1.0f, -1.0f, -0.5f).color(float4, float4, float4, 0.1f).uv(0.0f + float11, 4.0f + float12).endVertex();
        dhg3.vertex(b13, 1.0f, 1.0f, -0.5f).color(float4, float4, float4, 0.1f).uv(0.0f + float11, 0.0f + float12).endVertex();
        dhg3.vertex(b13, -1.0f, 1.0f, -0.5f).color(float4, float4, float4, 0.1f).uv(4.0f + float11, 0.0f + float12).endVertex();
        dhg3.end();
        BufferUploader.end(dhg3);
        RenderSystem.disableBlend();
    }
    
    private static void renderFire(final Minecraft dlx, final PoseStack dhl) {
        final BufferBuilder dhg3 = Tesselator.getInstance().getBuilder();
        RenderSystem.depthFunc(519);
        RenderSystem.depthMask(false);
        RenderSystem.enableBlend();
        RenderSystem.defaultBlendFunc();
        RenderSystem.enableTexture();
        final TextureAtlasSprite elv4 = ModelBakery.FIRE_1.sprite();
        dlx.getTextureManager().bind(elv4.atlas().location());
        final float float5 = elv4.getU0();
        final float float6 = elv4.getU1();
        final float float7 = (float5 + float6) / 2.0f;
        final float float8 = elv4.getV0();
        final float float9 = elv4.getV1();
        final float float10 = (float8 + float9) / 2.0f;
        final float float11 = elv4.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) {
            dhl.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;
            dhl.translate(-(integer17 * 2 - 1) * 0.24f, -0.30000001192092896, 0.0);
            dhl.mulPose(Vector3f.YP.rotationDegrees((integer17 * 2 - 1) * 10.0f));
            final Matrix4f b23 = dhl.last().pose();
            dhg3.begin(7, DefaultVertexFormat.POSITION_COLOR_TEX);
            dhg3.vertex(b23, -0.5f, -0.5f, -0.5f).color(1.0f, 1.0f, 1.0f, 0.9f).uv(float13, float15).endVertex();
            dhg3.vertex(b23, 0.5f, -0.5f, -0.5f).color(1.0f, 1.0f, 1.0f, 0.9f).uv(float12, float15).endVertex();
            dhg3.vertex(b23, 0.5f, 0.5f, -0.5f).color(1.0f, 1.0f, 1.0f, 0.9f).uv(float12, float14).endVertex();
            dhg3.vertex(b23, -0.5f, 0.5f, -0.5f).color(1.0f, 1.0f, 1.0f, 0.9f).uv(float13, float14).endVertex();
            dhg3.end();
            BufferUploader.end(dhg3);
            dhl.popPose();
        }
        RenderSystem.disableBlend();
        RenderSystem.depthMask(true);
        RenderSystem.depthFunc(515);
    }
    
    static {
        UNDERWATER_LOCATION = new ResourceLocation("textures/misc/underwater.png");
    }
}
