package net.minecraft.client.renderer;

import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.biome.Biomes;
import net.minecraft.world.level.material.Fluids;
import com.mojang.blaze3d.platform.GlStateManager;
import net.minecraft.world.level.biome.BiomeManager;
import net.minecraft.world.level.material.FluidState;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.LivingEntity;
import com.mojang.math.Vector3f;
import net.minecraft.util.CubicSampler;
import net.minecraft.world.phys.Vec3;
import net.minecraft.util.Mth;
import net.minecraft.core.BlockPos;
import net.minecraft.Util;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.tags.Tag;
import net.minecraft.tags.FluidTags;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.Camera;

public class FogRenderer {
    private static float fogRed;
    private static float fogGreen;
    private static float fogBlue;
    private static int targetBiomeFog;
    private static int previousBiomeFog;
    private static long biomeChangedTime;
    
    public static void setupColor(final Camera dlj, final float float2, final ClientLevel dym, final int integer, final float float5) {
        final FluidState cxa6 = dlj.getFluidInCamera();
        if (cxa6.is(FluidTags.WATER)) {
            final long long7 = Util.getMillis();
            final int integer7 = dym.getBiome(new BlockPos(dlj.getPosition())).getWaterFogColor();
            if (FogRenderer.biomeChangedTime < 0L) {
                FogRenderer.targetBiomeFog = integer7;
                FogRenderer.previousBiomeFog = integer7;
                FogRenderer.biomeChangedTime = long7;
            }
            final int integer8 = FogRenderer.targetBiomeFog >> 16 & 0xFF;
            final int integer9 = FogRenderer.targetBiomeFog >> 8 & 0xFF;
            final int integer10 = FogRenderer.targetBiomeFog & 0xFF;
            final int integer11 = FogRenderer.previousBiomeFog >> 16 & 0xFF;
            final int integer12 = FogRenderer.previousBiomeFog >> 8 & 0xFF;
            final int integer13 = FogRenderer.previousBiomeFog & 0xFF;
            final float float6 = Mth.clamp((long7 - FogRenderer.biomeChangedTime) / 5000.0f, 0.0f, 1.0f);
            final float float7 = Mth.lerp(float6, (float)integer11, (float)integer8);
            final float float8 = Mth.lerp(float6, (float)integer12, (float)integer9);
            final float float9 = Mth.lerp(float6, (float)integer13, (float)integer10);
            FogRenderer.fogRed = float7 / 255.0f;
            FogRenderer.fogGreen = float8 / 255.0f;
            FogRenderer.fogBlue = float9 / 255.0f;
            if (FogRenderer.targetBiomeFog != integer7) {
                FogRenderer.targetBiomeFog = integer7;
                FogRenderer.previousBiomeFog = (Mth.floor(float7) << 16 | Mth.floor(float8) << 8 | Mth.floor(float9));
                FogRenderer.biomeChangedTime = long7;
            }
        }
        else if (cxa6.is(FluidTags.LAVA)) {
            FogRenderer.fogRed = 0.6f;
            FogRenderer.fogGreen = 0.1f;
            FogRenderer.fogBlue = 0.0f;
            FogRenderer.biomeChangedTime = -1L;
        }
        else {
            float float10 = 0.25f + 0.75f * integer / 32.0f;
            float10 = 1.0f - (float)Math.pow(float10, 0.25);
            final Vec3 dem8 = dym.getSkyColor(dlj.getBlockPosition(), float2);
            final float float11 = (float)dem8.x;
            final float float12 = (float)dem8.y;
            final float float13 = (float)dem8.z;
            final float float14 = Mth.clamp(Mth.cos(dym.getTimeOfDay(float2) * 6.2831855f) * 2.0f + 0.5f, 0.0f, 1.0f);
            final BiomeManager brg13 = dym.getBiomeManager();
            final Vec3 dem9 = dlj.getPosition().subtract(2.0, 2.0, 2.0).scale(0.25);
            final Vec3 dem10 = CubicSampler.gaussianSampleVec3(dem9, (integer4, integer5, integer6) -> dym.effects().getBrightnessDependentFogColor(Vec3.fromRGB24(brg13.getNoiseBiomeAtQuart(integer4, integer5, integer6).getFogColor()), float14));
            FogRenderer.fogRed = (float)dem10.x();
            FogRenderer.fogGreen = (float)dem10.y();
            FogRenderer.fogBlue = (float)dem10.z();
            if (integer >= 4) {
                final float float6 = (Mth.sin(dym.getSunAngle(float2)) > 0.0f) ? -1.0f : 1.0f;
                final Vector3f g17 = new Vector3f(float6, 0.0f, 0.0f);
                float float8 = dlj.getLookVector().dot(g17);
                if (float8 < 0.0f) {
                    float8 = 0.0f;
                }
                if (float8 > 0.0f) {
                    final float[] arr19 = dym.effects().getSunriseColor(dym.getTimeOfDay(float2), float2);
                    if (arr19 != null) {
                        float8 *= arr19[3];
                        FogRenderer.fogRed = FogRenderer.fogRed * (1.0f - float8) + arr19[0] * float8;
                        FogRenderer.fogGreen = FogRenderer.fogGreen * (1.0f - float8) + arr19[1] * float8;
                        FogRenderer.fogBlue = FogRenderer.fogBlue * (1.0f - float8) + arr19[2] * float8;
                    }
                }
            }
            FogRenderer.fogRed += (float11 - FogRenderer.fogRed) * float10;
            FogRenderer.fogGreen += (float12 - FogRenderer.fogGreen) * float10;
            FogRenderer.fogBlue += (float13 - FogRenderer.fogBlue) * float10;
            final float float6 = dym.getRainLevel(float2);
            if (float6 > 0.0f) {
                final float float7 = 1.0f - float6 * 0.5f;
                final float float8 = 1.0f - float6 * 0.4f;
                FogRenderer.fogRed *= float7;
                FogRenderer.fogGreen *= float7;
                FogRenderer.fogBlue *= float8;
            }
            final float float7 = dym.getThunderLevel(float2);
            if (float7 > 0.0f) {
                final float float8 = 1.0f - float7 * 0.5f;
                FogRenderer.fogRed *= float8;
                FogRenderer.fogGreen *= float8;
                FogRenderer.fogBlue *= float8;
            }
            FogRenderer.biomeChangedTime = -1L;
        }
        double double7 = dlj.getPosition().y * dym.getLevelData().getClearColorScale();
        if (dlj.getEntity() instanceof LivingEntity && ((LivingEntity)dlj.getEntity()).hasEffect(MobEffects.BLINDNESS)) {
            final int integer7 = ((LivingEntity)dlj.getEntity()).getEffect(MobEffects.BLINDNESS).getDuration();
            if (integer7 < 20) {
                double7 *= 1.0f - integer7 / 20.0f;
            }
            else {
                double7 = 0.0;
            }
        }
        if (double7 < 1.0 && !cxa6.is(FluidTags.LAVA)) {
            if (double7 < 0.0) {
                double7 = 0.0;
            }
            double7 *= double7;
            FogRenderer.fogRed *= (float)double7;
            FogRenderer.fogGreen *= (float)double7;
            FogRenderer.fogBlue *= (float)double7;
        }
        if (float5 > 0.0f) {
            FogRenderer.fogRed = FogRenderer.fogRed * (1.0f - float5) + FogRenderer.fogRed * 0.7f * float5;
            FogRenderer.fogGreen = FogRenderer.fogGreen * (1.0f - float5) + FogRenderer.fogGreen * 0.6f * float5;
            FogRenderer.fogBlue = FogRenderer.fogBlue * (1.0f - float5) + FogRenderer.fogBlue * 0.6f * float5;
        }
        if (cxa6.is(FluidTags.WATER)) {
            float float11 = 0.0f;
            if (dlj.getEntity() instanceof LocalPlayer) {
                final LocalPlayer ebf10 = (LocalPlayer)dlj.getEntity();
                float11 = ebf10.getWaterVision();
            }
            final float float12 = Math.min(1.0f / FogRenderer.fogRed, Math.min(1.0f / FogRenderer.fogGreen, 1.0f / FogRenderer.fogBlue));
            FogRenderer.fogRed = FogRenderer.fogRed * (1.0f - float11) + FogRenderer.fogRed * float12 * float11;
            FogRenderer.fogGreen = FogRenderer.fogGreen * (1.0f - float11) + FogRenderer.fogGreen * float12 * float11;
            FogRenderer.fogBlue = FogRenderer.fogBlue * (1.0f - float11) + FogRenderer.fogBlue * float12 * float11;
        }
        else if (dlj.getEntity() instanceof LivingEntity && ((LivingEntity)dlj.getEntity()).hasEffect(MobEffects.NIGHT_VISION)) {
            final float float11 = GameRenderer.getNightVisionScale((LivingEntity)dlj.getEntity(), float2);
            final float float12 = Math.min(1.0f / FogRenderer.fogRed, Math.min(1.0f / FogRenderer.fogGreen, 1.0f / FogRenderer.fogBlue));
            FogRenderer.fogRed = FogRenderer.fogRed * (1.0f - float11) + FogRenderer.fogRed * float12 * float11;
            FogRenderer.fogGreen = FogRenderer.fogGreen * (1.0f - float11) + FogRenderer.fogGreen * float12 * float11;
            FogRenderer.fogBlue = FogRenderer.fogBlue * (1.0f - float11) + FogRenderer.fogBlue * float12 * float11;
        }
        RenderSystem.clearColor(FogRenderer.fogRed, FogRenderer.fogGreen, FogRenderer.fogBlue, 0.0f);
    }
    
    public static void setupNoFog() {
        RenderSystem.fogDensity(0.0f);
        RenderSystem.fogMode(GlStateManager.FogMode.EXP2);
    }
    
    public static void setupFog(final Camera dlj, final FogMode a, final float float3, final boolean boolean4) {
        final FluidState cxa5 = dlj.getFluidInCamera();
        final Entity aom6 = dlj.getEntity();
        final boolean boolean5 = cxa5.getType() != Fluids.EMPTY;
        if (cxa5.is(FluidTags.WATER)) {
            float float4 = 1.0f;
            float4 = 0.05f;
            if (aom6 instanceof LocalPlayer) {
                final LocalPlayer ebf9 = (LocalPlayer)aom6;
                float4 -= ebf9.getWaterVision() * ebf9.getWaterVision() * 0.03f;
                final Biome bre10 = ebf9.level.getBiome(ebf9.blockPosition());
                if (bre10 == Biomes.SWAMP || bre10 == Biomes.SWAMP_HILLS) {
                    float4 += 0.005f;
                }
            }
            RenderSystem.fogDensity(float4);
            RenderSystem.fogMode(GlStateManager.FogMode.EXP2);
        }
        else {
            float float4;
            float float5;
            if (cxa5.is(FluidTags.LAVA)) {
                if (aom6 instanceof LivingEntity && ((LivingEntity)aom6).hasEffect(MobEffects.FIRE_RESISTANCE)) {
                    float4 = 0.0f;
                    float5 = 3.0f;
                }
                else {
                    float4 = 0.25f;
                    float5 = 1.0f;
                }
            }
            else if (aom6 instanceof LivingEntity && ((LivingEntity)aom6).hasEffect(MobEffects.BLINDNESS)) {
                final int integer10 = ((LivingEntity)aom6).getEffect(MobEffects.BLINDNESS).getDuration();
                final float float6 = Mth.lerp(Math.min(1.0f, integer10 / 20.0f), float3, 5.0f);
                if (a == FogMode.FOG_SKY) {
                    float4 = 0.0f;
                    float5 = float6 * 0.8f;
                }
                else {
                    float4 = float6 * 0.25f;
                    float5 = float6;
                }
            }
            else if (boolean4) {
                float4 = float3 * 0.05f;
                float5 = Math.min(float3, 192.0f) * 0.5f;
            }
            else if (a == FogMode.FOG_SKY) {
                float4 = 0.0f;
                float5 = float3;
            }
            else {
                float4 = float3 * 0.75f;
                float5 = float3;
            }
            RenderSystem.fogStart(float4);
            RenderSystem.fogEnd(float5);
            RenderSystem.fogMode(GlStateManager.FogMode.LINEAR);
            RenderSystem.setupNvFogDistance();
        }
    }
    
    public static void levelFogColor() {
        RenderSystem.fog(2918, FogRenderer.fogRed, FogRenderer.fogGreen, FogRenderer.fogBlue, 1.0f);
    }
    
    static {
        FogRenderer.targetBiomeFog = -1;
        FogRenderer.previousBiomeFog = -1;
        FogRenderer.biomeChangedTime = -1L;
    }
    
    public enum FogMode {
        FOG_SKY, 
        FOG_TERRAIN;
    }
}
