package net.minecraft.client.renderer.debug;

import java.util.Iterator;
import com.mojang.blaze3d.vertex.VertexConsumer;
import java.util.Set;
import net.minecraft.client.renderer.LevelRenderer;
import net.minecraft.world.phys.AABB;
import net.minecraft.client.renderer.RenderType;
import com.google.common.collect.Sets;
import net.minecraft.client.renderer.MultiBufferSource;
import com.mojang.blaze3d.vertex.PoseStack;
import java.util.Comparator;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
import net.minecraft.core.BlockPos;
import java.util.Map;
import net.minecraft.client.Minecraft;

public class NeighborsUpdateRenderer implements DebugRenderer.SimpleDebugRenderer {
    private final Minecraft minecraft;
    private final Map<Long, Map<BlockPos, Integer>> lastUpdate;
    
    NeighborsUpdateRenderer(final Minecraft dbn) {
        this.lastUpdate = Maps.newTreeMap(Ordering.<Comparable>natural().reverse());
        this.minecraft = dbn;
    }
    
    public void addUpdate(final long long1, final BlockPos fk) {
        Map<BlockPos, Integer> map5 = this.lastUpdate.get(long1);
        if (map5 == null) {
            map5 = Maps.newHashMap();
            this.lastUpdate.put(long1, map5);
        }
        Integer integer6 = map5.get(fk);
        if (integer6 == null) {
            integer6 = 0;
        }
        map5.put(fk, integer6 + 1);
    }
    
    @Override
    public void render(final PoseStack cyl, final MultiBufferSource dqt, final double double3, final double double4, final double double5) {
        final long long10 = this.minecraft.level.getGameTime();
        final int integer12 = 200;
        final double double6 = 0.0025;
        final Set<BlockPos> set15 = Sets.newHashSet();
        final Map<BlockPos, Integer> map16 = Maps.newHashMap();
        final VertexConsumer cyo17 = dqt.getBuffer(RenderType.lines());
        final Iterator<Map.Entry<Long, Map<BlockPos, Integer>>> iterator18 = this.lastUpdate.entrySet().iterator();
        while (iterator18.hasNext()) {
            final Map.Entry<Long, Map<BlockPos, Integer>> entry19 = iterator18.next();
            final Long long11 = entry19.getKey();
            final Map<BlockPos, Integer> map17 = entry19.getValue();
            final long long12 = long10 - long11;
            if (long12 > 200L) {
                iterator18.remove();
            }
            else {
                for (final Map.Entry<BlockPos, Integer> entry20 : map17.entrySet()) {
                    final BlockPos fk26 = entry20.getKey();
                    final Integer integer13 = entry20.getValue();
                    if (set15.add(fk26)) {
                        final AABB cvf28 = new AABB(BlockPos.ZERO).inflate(0.002).deflate(0.0025 * long12).move(fk26.getX(), fk26.getY(), fk26.getZ()).move(-double3, -double4, -double5);
                        LevelRenderer.renderLineBox(cyo17, cvf28.minX, cvf28.minY, cvf28.minZ, cvf28.maxX, cvf28.maxY, cvf28.maxZ, 1.0f, 1.0f, 1.0f, 1.0f);
                        map16.put(fk26, integer13);
                    }
                }
            }
        }
        for (final Map.Entry<BlockPos, Integer> entry21 : map16.entrySet()) {
            final BlockPos fk27 = entry21.getKey();
            final Integer integer14 = entry21.getValue();
            DebugRenderer.renderFloatingText(String.valueOf(integer14), fk27.getX(), fk27.getY(), fk27.getZ(), -1);
        }
    }
}
