package mcjty.xnet.multiblock;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import mcjty.xnet.XNet;
import mcjty.xnet.api.XNetAPI;
import mcmultipart.multipart.IMultipart;
import mcmultipart.multipart.IMultipartContainer;
import mcmultipart.multipart.PartSlot;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.common.capabilities.ICapabilityProvider;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;

/**
 * Created by Elec332 on 6-3-2016.
 */
public class XNetGrid {

    public XNetGrid(XNetWorldGridRegistry registry){
        this.worldGridRegistry = registry;
        allLocations = Sets.newHashSet();
        allLocations_ = Collections.unmodifiableSet(allLocations);
        allConnectors = Lists.newArrayList();
    }

    private final Set<BlockPos> allLocations;
    private final Set<BlockPos> allLocations_;
    private final List<FacedPosition> allConnectors;
    private final XNetWorldGridRegistry worldGridRegistry;


    int counter = 10;

    public void tick(){
//        if (counter > 0) {
//            counter--;
//            return;
//        }
//        counter = 10;
//        System.out.println("XNetGrid.tick");
//        for (BlockPos pos : allLocations) {
//            System.out.println("    pos = " + pos);
//        }
//        for (FacedPosition connector : allConnectors) {
//            System.out.println("connector = " + connector);
//        }
    }

    public void invalidate(){
    }

    public Set<BlockPos> getAllLocations() {
        return allLocations_;
    }

    public List<FacedPosition> getAllConnectors() {
        return allConnectors;
    }

    protected void merge(XNetGrid grid){
        for (BlockPos pos : grid.getAllLocations()){
            XNetTileData tile = worldGridRegistry.getPowerTile(pos);
            if (tile != null) {
                addTile(tile);
            } else {
                XNet.logger.error("Null tile for pos: " + pos);
            }
            addTile(worldGridRegistry.getPowerTile(pos));
        }
        allConnectors.addAll(grid.allConnectors);
    }

    protected void addTile(XNetTileData tile){
        allLocations.add(tile.getPos());
        tile.setGrid(this);
    }

    protected void onRemoved(XNetTileData tile) {
    }

    void change(XNetTileData tile, @Nullable IMultipart multipart_){
        TileEntity tileEntity = tile.getTile();
        BlockPos pos = tile.getPos();
        FacedPosition faces = null;
        for (FacedPosition position : allConnectors){
            if (position.pos.equals(pos)){
                faces = position;
            }
        }
        if (faces == null){
            faces = new FacedPosition(pos);
            allConnectors.add(faces);
        }
        faces.sides.clear();
        if (tileEntity instanceof IMultipartContainer){
            for (EnumFacing facing : EnumFacing.VALUES) {
                IMultipart multipart = ((IMultipartContainer) tileEntity).getPartInSlot(PartSlot.getFaceSlot(facing));
                if (multipart instanceof ICapabilityProvider && ((ICapabilityProvider) multipart).hasCapability(XNetAPI.XNET_CAPABILITY, facing.getOpposite())) {
                    faces.sides.add(facing);
                }
            }
        }
        if (faces.sides.isEmpty()){
            allConnectors.remove(faces);
        }
    }

    public class FacedPosition {

        private FacedPosition(@Nonnull BlockPos pos){
            this.pos = pos;
            this.sides = EnumSet.noneOf(EnumFacing.class);
        }

        private final BlockPos pos;
        private final Set<EnumFacing> sides;

        @Override
        public boolean equals(Object obj) {
            return obj instanceof FacedPosition && pos.equals(((FacedPosition) obj).pos) && sides.equals(((FacedPosition) obj).sides);
        }

        @Override
        public String toString() {
            return "Pos: "+pos+" Sides: "+sides;
        }

        public BlockPos getPos() {
            return pos;
        }

        public Set<EnumFacing> getSides() {
            return sides;
        }
    }

}

@@ -1200,17 +1200,17 @@
 unter = 
-1
+2
 0;%0A%0A    
@@ -1229,18 +1229,16 @@
 tick()%7B%0A
-//
         
@@ -1252,26 +1252,24 @@
 nter %3E 0) %7B%0A
-//
             
@@ -1279,18 +1279,16 @@
 nter--;%0A
-//
         
@@ -1299,18 +1299,16 @@
 return;%0A
-//
         
@@ -1301,34 +1301,32 @@
 turn;%0A        %7D%0A
-//
         counter 
@@ -1331,14 +1331,12 @@
 r = 
-1
+2
 0;%0A
-//
     
@@ -1376,18 +1376,16 @@
 tick%22);%0A
-//
         
@@ -1416,26 +1416,24 @@
 ocations) %7B%0A
-//
             
@@ -1472,18 +1472,16 @@
 + pos);%0A
-//
         
@@ -1482,18 +1482,16 @@
       %7D%0A
-//
         
@@ -1538,18 +1538,16 @@
 tors) %7B%0A
-//
         
@@ -1602,10 +1602,8 @@
 r);%0A
-//
     
@@ -1600,39 +1600,40 @@
 tor);%0A        %7D%0A
+%0A%0A
     %7D%0A
-%0A
     public void 

Bring back debug info