package net.minecraft.client.renderer;

import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.world.level.dimension.DimensionType;
import net.minecraft.world.level.Level;
import com.mojang.math.Vector3f;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.effect.MobEffects;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
import com.mojang.blaze3d.platform.NativeImage;
import net.minecraft.client.renderer.texture.DynamicTexture;

public class LightTexture implements AutoCloseable {
    private final DynamicTexture lightTexture;
    private final NativeImage lightPixels;
    private final ResourceLocation lightTextureLocation;
    private boolean updateLightTexture;
    private float blockLightRedFlicker;
    private final GameRenderer renderer;
    private final Minecraft minecraft;
    
    public LightTexture(final GameRenderer dqn, final Minecraft dbn) {
        this.renderer = dqn;
        this.minecraft = dbn;
        this.lightTexture = new DynamicTexture(16, 16, false);
        this.lightTextureLocation = this.minecraft.getTextureManager().register("light_map", this.lightTexture);
        this.lightPixels = this.lightTexture.getPixels();
        for (int integer4 = 0; integer4 < 16; ++integer4) {
            for (int integer5 = 0; integer5 < 16; ++integer5) {
                this.lightPixels.setPixelRGBA(integer5, integer4, -1);
            }
        }
        this.lightTexture.upload();
    }
    
    @Override
    public void close() {
        this.lightTexture.close();
    }
    
    public void tick() {
        this.blockLightRedFlicker += (float)((Math.random() - Math.random()) * Math.random() * Math.random() * 0.1);
        this.blockLightRedFlicker *= (float)0.9;
        this.updateLightTexture = true;
    }
    
    public void turnOffLightLayer() {
        RenderSystem.activeTexture(33986);
        RenderSystem.disableTexture();
        RenderSystem.activeTexture(33984);
    }
    
    public void turnOnLightLayer() {
        RenderSystem.activeTexture(33986);
        RenderSystem.matrixMode(5890);
        RenderSystem.loadIdentity();
        final float float2 = 0.00390625f;
        RenderSystem.scalef(0.00390625f, 0.00390625f, 0.00390625f);
        RenderSystem.translatef(8.0f, 8.0f, 8.0f);
        RenderSystem.matrixMode(5888);
        this.minecraft.getTextureManager().bind(this.lightTextureLocation);
        RenderSystem.texParameter(3553, 10241, 9729);
        RenderSystem.texParameter(3553, 10240, 9729);
        RenderSystem.texParameter(3553, 10242, 10496);
        RenderSystem.texParameter(3553, 10243, 10496);
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        RenderSystem.enableTexture();
        RenderSystem.activeTexture(33984);
    }
    
    public void updateLightTexture(final float float1) {
        if (!this.updateLightTexture) {
            return;
        }
        this.updateLightTexture = false;
        this.minecraft.getProfiler().push("lightTex");
        final ClientLevel dno3 = this.minecraft.level;
        if (dno3 == null) {
            return;
        }
        final float float2 = dno3.getSkyDarken(1.0f);
        float float3;
        if (dno3.getSkyFlashTime() > 0) {
            float3 = 1.0f;
        }
        else {
            float3 = float2 * 0.95f + 0.05f;
        }
        final float float4 = this.minecraft.player.getWaterVision();
        float float5;
        if (this.minecraft.player.hasEffect(MobEffects.NIGHT_VISION)) {
            float5 = GameRenderer.getNightVisionScale(this.minecraft.player, float1);
        }
        else if (float4 > 0.0f && this.minecraft.player.hasEffect(MobEffects.CONDUIT_POWER)) {
            float5 = float4;
        }
        else {
            float5 = 0.0f;
        }
        final Vector3f e8 = new Vector3f(float2, float2, 1.0f);
        e8.lerp(new Vector3f(1.0f, 1.0f, 1.0f), 0.35f);
        final float float6 = this.blockLightRedFlicker + 1.5f;
        final Vector3f e9 = new Vector3f();
        for (int integer11 = 0; integer11 < 16; ++integer11) {
            for (int integer12 = 0; integer12 < 16; ++integer12) {
                final float float7 = this.getBrightness(dno3, integer11) * float3;
                final float float9;
                final float float8 = float9 = this.getBrightness(dno3, integer12) * float6;
                final float float10 = float8 * ((float8 * 0.6f + 0.4f) * 0.6f + 0.4f);
                final float float11 = float8 * (float8 * float8 * 0.6f + 0.4f);
                e9.set(float9, float10, float11);
                if (dno3.dimension.getType() == DimensionType.THE_END) {
                    e9.lerp(new Vector3f(0.99f, 1.12f, 1.0f), 0.25f);
                }
                else {
                    final Vector3f e10 = e8.copy();
                    e10.mul(float7);
                    e9.add(e10);
                    e9.lerp(new Vector3f(0.75f, 0.75f, 0.75f), 0.04f);
                    if (this.renderer.getDarkenWorldAmount(float1) > 0.0f) {
                        final float float12 = this.renderer.getDarkenWorldAmount(float1);
                        final Vector3f e11 = e9.copy();
                        e11.mul(0.7f, 0.6f, 0.6f);
                        e9.lerp(e11, float12);
                    }
                }
                e9.clamp(0.0f, 1.0f);
                if (float5 > 0.0f) {
                    final float float13 = Math.max(e9.x(), Math.max(e9.y(), e9.z()));
                    if (float13 < 1.0f) {
                        final float float12 = 1.0f / float13;
                        final Vector3f e11 = e9.copy();
                        e11.mul(float12);
                        e9.lerp(e11, float5);
                    }
                }
                final float float13 = (float)this.minecraft.options.gamma;
                final Vector3f e12 = e9.copy();
                e12.map(this::notGamma);
                e9.lerp(e12, float13);
                e9.lerp(new Vector3f(0.75f, 0.75f, 0.75f), 0.04f);
                e9.clamp(0.0f, 1.0f);
                e9.mul(255.0f);
                final int integer13 = 255;
                final int integer14 = (int)e9.x();
                final int integer15 = (int)e9.y();
                final int integer16 = (int)e9.z();
                this.lightPixels.setPixelRGBA(integer12, integer11, 0xFF000000 | integer16 << 16 | integer15 << 8 | integer14);
            }
        }
        this.lightTexture.upload();
        this.minecraft.getProfiler().pop();
    }
    
    private float notGamma(final float float1) {
        final float float2 = 1.0f - float1;
        return 1.0f - float2 * float2 * float2 * float2;
    }
    
    private float getBrightness(final Level bjw, final int integer) {
        return bjw.dimension.getBrightness(integer);
    }
    
    public static int pack(final int integer1, final int integer2) {
        return integer1 << 4 | integer2 << 20;
    }
    
    public static int block(final int integer) {
        return integer >> 4 & 0xFFFF;
    }
    
    public static int sky(final int integer) {
        return integer >> 20 & 0xFFFF;
    }
}
