package net.minecraft.client.renderer;

import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.Biomes;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.material.Fluids;
import com.mojang.blaze3d.platform.GlStateManager;
import net.minecraft.world.phys.Vec3;
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.Mth;
import net.minecraft.core.BlockPos;
import net.minecraft.Util;
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 dbb, final float float2, final ClientLevel dno, final int integer, final float float5) {
        final FluidState coj6 = dbb.getFluidInCamera();
        if (coj6.is(FluidTags.WATER)) {
            final long long7 = Util.getMillis();
            final int integer2 = dno.getBiome(new BlockPos(dbb.getPosition())).getWaterFogColor();
            if (FogRenderer.biomeChangedTime < 0L) {
                FogRenderer.targetBiomeFog = integer2;
                FogRenderer.previousBiomeFog = integer2;
                FogRenderer.biomeChangedTime = long7;
            }
            final int integer3 = FogRenderer.targetBiomeFog >> 16 & 0xFF;
            final int integer4 = FogRenderer.targetBiomeFog >> 8 & 0xFF;
            final int integer5 = FogRenderer.targetBiomeFog & 0xFF;
            final int integer6 = FogRenderer.previousBiomeFog >> 16 & 0xFF;
            final int integer7 = FogRenderer.previousBiomeFog >> 8 & 0xFF;
            final int integer8 = FogRenderer.previousBiomeFog & 0xFF;
            final float float6 = Mth.clamp((long7 - FogRenderer.biomeChangedTime) / 5000.0f, 0.0f, 1.0f);
            final float float7 = Mth.lerp(float6, (float)integer6, (float)integer3);
            final float float8 = Mth.lerp(float6, (float)integer7, (float)integer4);
            final float float9 = Mth.lerp(float6, (float)integer8, (float)integer5);
            FogRenderer.fogRed = float7 / 255.0f;
            FogRenderer.fogGreen = float8 / 255.0f;
            FogRenderer.fogBlue = float9 / 255.0f;
            if (FogRenderer.targetBiomeFog != integer2) {
                FogRenderer.targetBiomeFog = integer2;
                FogRenderer.previousBiomeFog = (Mth.floor(float7) << 16 | Mth.floor(float8) << 8 | Mth.floor(float9));
                FogRenderer.biomeChangedTime = long7;
            }
        }
        else if (coj6.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 cvl8 = dno.getSkyColor(dbb.getBlockPosition(), float2);
            final float float11 = (float)cvl8.x;
            final float float12 = (float)cvl8.y;
            final float float13 = (float)cvl8.z;
            final Vec3 cvl9 = dno.getFogColor(float2);
            FogRenderer.fogRed = (float)cvl9.x;
            FogRenderer.fogGreen = (float)cvl9.y;
            FogRenderer.fogBlue = (float)cvl9.z;
            if (integer >= 4) {
                final float float14 = (Mth.sin(dno.getSunAngle(float2)) > 0.0f) ? -1.0f : 1.0f;
                final Vector3f e14 = new Vector3f(float14, 0.0f, 0.0f);
                float float15 = dbb.getLookVector().dot(e14);
                if (float15 < 0.0f) {
                    float15 = 0.0f;
                }
                if (float15 > 0.0f) {
                    final float[] arr16 = dno.dimension.getSunriseColor(dno.getTimeOfDay(float2), float2);
                    if (arr16 != null) {
                        float15 *= arr16[3];
                        FogRenderer.fogRed = FogRenderer.fogRed * (1.0f - float15) + arr16[0] * float15;
                        FogRenderer.fogGreen = FogRenderer.fogGreen * (1.0f - float15) + arr16[1] * float15;
                        FogRenderer.fogBlue = FogRenderer.fogBlue * (1.0f - float15) + arr16[2] * float15;
                    }
                }
            }
            FogRenderer.fogRed += (float11 - FogRenderer.fogRed) * float10;
            FogRenderer.fogGreen += (float12 - FogRenderer.fogGreen) * float10;
            FogRenderer.fogBlue += (float13 - FogRenderer.fogBlue) * float10;
            final float float14 = dno.getRainLevel(float2);
            if (float14 > 0.0f) {
                final float float16 = 1.0f - float14 * 0.5f;
                final float float15 = 1.0f - float14 * 0.4f;
                FogRenderer.fogRed *= float16;
                FogRenderer.fogGreen *= float16;
                FogRenderer.fogBlue *= float15;
            }
            final float float16 = dno.getThunderLevel(float2);
            if (float16 > 0.0f) {
                final float float15 = 1.0f - float16 * 0.5f;
                FogRenderer.fogRed *= float15;
                FogRenderer.fogGreen *= float15;
                FogRenderer.fogBlue *= float15;
            }
            FogRenderer.biomeChangedTime = -1L;
        }
        double double7 = dbb.getPosition().y * dno.dimension.getClearColorScale();
        if (dbb.getEntity() instanceof LivingEntity && ((LivingEntity)dbb.getEntity()).hasEffect(MobEffects.BLINDNESS)) {
            final int integer2 = ((LivingEntity)dbb.getEntity()).getEffect(MobEffects.BLINDNESS).getDuration();
            if (integer2 < 20) {
                double7 *= 1.0f - integer2 / 20.0f;
            }
            else {
                double7 = 0.0;
            }
        }
        if (double7 < 1.0) {
            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 (coj6.is(FluidTags.WATER)) {
            float float11 = 0.0f;
            if (dbb.getEntity() instanceof LocalPlayer) {
                final LocalPlayer dqb10 = (LocalPlayer)dbb.getEntity();
                float11 = dqb10.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 (dbb.getEntity() instanceof LivingEntity && ((LivingEntity)dbb.getEntity()).hasEffect(MobEffects.NIGHT_VISION)) {
            final float float11 = GameRenderer.getNightVisionScale((LivingEntity)dbb.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 dbb, final FogMode a, final float float3, final boolean boolean4) {
        final FluidState coj5 = dbb.getFluidInCamera();
        final Entity akq6 = dbb.getEntity();
        final boolean boolean5 = coj5.getType() != Fluids.EMPTY;
        if (boolean5) {
            float float4 = 1.0f;
            if (coj5.is(FluidTags.WATER)) {
                float4 = 0.05f;
                if (akq6 instanceof LocalPlayer) {
                    final LocalPlayer dqb9 = (LocalPlayer)akq6;
                    float4 -= dqb9.getWaterVision() * dqb9.getWaterVision() * 0.03f;
                    final Biome bkt10 = dqb9.level.getBiome(new BlockPos(dqb9));
                    if (bkt10 == Biomes.SWAMP || bkt10 == Biomes.SWAMP_HILLS) {
                        float4 += 0.005f;
                    }
                }
            }
            else if (coj5.is(FluidTags.LAVA)) {
                float4 = 2.0f;
            }
            RenderSystem.fogDensity(float4);
            RenderSystem.fogMode(GlStateManager.FogMode.EXP2);
        }
        else {
            float float4;
            float float6;
            if (akq6 instanceof LivingEntity && ((LivingEntity)akq6).hasEffect(MobEffects.BLINDNESS)) {
                final int integer10 = ((LivingEntity)akq6).getEffect(MobEffects.BLINDNESS).getDuration();
                final float float5 = Mth.lerp(Math.min(1.0f, integer10 / 20.0f), float3, 5.0f);
                if (a == FogMode.FOG_SKY) {
                    float4 = 0.0f;
                    float6 = float5 * 0.8f;
                }
                else {
                    float4 = float5 * 0.25f;
                    float6 = float5;
                }
            }
            else if (boolean4) {
                float4 = float3 * 0.05f;
                float6 = Math.min(float3, 192.0f) * 0.5f;
            }
            else if (a == FogMode.FOG_SKY) {
                float4 = 0.0f;
                float6 = float3;
            }
            else {
                float4 = float3 * 0.75f;
                float6 = float3;
            }
            RenderSystem.fogStart(float4);
            RenderSystem.fogEnd(float6);
            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;
    }
}
