package org.cyclops.integrateddynamics.core.network.diagnostics;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderGlobal;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraftforge.client.event.RenderWorldLastEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import org.cyclops.integrateddynamics.api.part.PartPos;
import org.cyclops.integrateddynamics.core.helper.PartHelpers;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * @author rubensworks
 */
public class NetworkDiagnosticsPartOverlayRenderer {

    private static final NetworkDiagnosticsPartOverlayRenderer _INSTANCE = new NetworkDiagnosticsPartOverlayRenderer();
    private final Set<PartPos> partPositions = Sets.newHashSet();

    private NetworkDiagnosticsPartOverlayRenderer() {

    }

    public static NetworkDiagnosticsPartOverlayRenderer getInstance() {
        return _INSTANCE;
    }

    public synchronized void addPos(PartPos pos) {
        partPositions.add(pos);
    }

    public synchronized void removePos(PartPos pos) {
        partPositions.remove(pos);
    }

    public synchronized void clearPositions() {
        partPositions.clear();
    }

    public synchronized boolean hasPartPos(PartPos pos) {
        return partPositions.contains(pos);
    }

    @SubscribeEvent
    public void onRender(RenderWorldLastEvent event) {
        if (!partPositions.isEmpty()) {
            EntityPlayer player = Minecraft.getMinecraft().player;
            float partialTicks = event.getPartialTicks();

            double offsetX = player.lastTickPosX + (player.posX - player.lastTickPosX) * (double) partialTicks;
            double offsetY = player.lastTickPosY + (player.posY - player.lastTickPosY) * (double) partialTicks;
            double offsetZ = player.lastTickPosZ + (player.posZ - player.lastTickPosZ) * (double) partialTicks;

            GlStateManager.enableBlend();
            GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO);
            GlStateManager.glLineWidth(6.0F);
            GlStateManager.disableTexture2D();
            GlStateManager.depthMask(false);

            List<PartPos> partList = Lists.newArrayList(partPositions);
            for (PartPos partPos : partList) {
                if (partPos.getPos().getDimensionId() == player.world.provider.getDimension() && partPos.getPos().getBlockPos().distanceSq(player.getPosition()) < 10000) {
                    PartHelpers.PartStateHolder<?, ?> partStateHolder = PartHelpers.getPart(partPos);
                    if (partStateHolder != null) {
                        final AxisAlignedBB globalRenderBB = partStateHolder.getPart().getPartRenderPosition().getBoundingBox(partPos.getSide())
                                .offset(partPos.getPos().getBlockPos())
                                .offset(-offsetX, -offsetY, -offsetZ)
                                .expand(0.05, 0.05, 0.05);
                        RenderGlobal.drawSelectionBoundingBox(globalRenderBB, 1.0F, 0.2F, 0.1F, 0.8F);
                    }
                }
            }

            GlStateManager.depthMask(true);
            GlStateManager.enableTexture2D();
            GlStateManager.disableBlend();
        }
    }

}

@@ -134,24 +134,58 @@
 llect.Sets;%0A
+import net.minecraft.block.Block;%0A
 import net.m
@@ -2898,24 +2898,77 @@
 t(partPos);%0A
+                    final AxisAlignedBB localPartBB;%0A
             
@@ -3034,40 +3034,17 @@
     
-final AxisAlignedBB g
 lo
-balRender
+calPart
 BB =
@@ -3127,21 +3127,200 @@
 tSide())
+;
 %0A    
+                %7D else %7B%0A                        localPartBB = Block.FULL_BLOCK_AABB;%0A                    %7D%0A%0A                    final AxisAlignedBB globalRenderBB = localPartBB%0A
         
@@ -3379,20 +3379,16 @@
 kPos())%0A
-    
         
@@ -3445,24 +3445,74 @@
 ffsetZ)%0A    
+                        .expand(0.05, 0.05, 0.05)%0A
             
@@ -3539,31 +3539,30 @@
 and(
+-
 0.05, 
+-
 0.05, 
+-
 0.05);%0A
-    
     
@@ -3656,38 +3656,16 @@
  0.8F);%0A
-                    %7D%0A
         

Fix the network diagnostic tool's box rendering