package org.cyclops.integrateddynamics.api.part;

import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.AxisAlignedBB;
import org.cyclops.cyclopscore.datastructure.EnumFacingMap;
import org.cyclops.cyclopscore.helper.MatrixHelpers;
import org.cyclops.integrateddynamics.client.model.CableModel;

import java.util.Arrays;

/**
 * Render position definition of a part.
 * @author rubensworks
 */
public class PartRenderPosition {

    public static final PartRenderPosition NONE = new PartRenderPosition(-1, -1, -1, -1);

    private final float depthFactor;
    private final float widthFactor;
    private final float heightFactor;
    private final EnumFacingMap<AxisAlignedBB> sidedCableCollisionBoxes;
    private final EnumFacingMap<AxisAlignedBB> collisionBoxes;

    public PartRenderPosition(float selectionDepthFactor, float depthFactor, float widthFactor, float heightFactor) {
        this.depthFactor = depthFactor;
        this.widthFactor = widthFactor;
        this.heightFactor = heightFactor;
        float[][] sidedCableCollisionBoxesRaw = new float[][]{
                {CableModel.MIN, selectionDepthFactor, CableModel.MIN, CableModel.MAX, CableModel.MIN, CableModel.MAX}, // DOWN
                {CableModel.MIN, CableModel.MAX, CableModel.MIN, CableModel.MAX, 1 - selectionDepthFactor, CableModel.MAX}, // UP
                {CableModel.MIN, CableModel.MIN, selectionDepthFactor, CableModel.MAX, CableModel.MAX, CableModel.MIN}, // NORTH
                {CableModel.MIN, CableModel.MAX, CableModel.MAX, CableModel.MAX, CableModel.MIN, 1 - selectionDepthFactor}, // SOUTH
                {selectionDepthFactor, CableModel.MIN, CableModel.MIN, CableModel.MIN, CableModel.MAX, CableModel.MAX}, // WEST
                {CableModel.MAX, CableModel.MIN, CableModel.MIN, 1 - selectionDepthFactor, CableModel.MAX, CableModel.MAX}, // EAST
        };

        sidedCableCollisionBoxes = EnumFacingMap.newMap();
        for (EnumFacing side : EnumFacing.VALUES) {
            float[] b = sidedCableCollisionBoxesRaw[side.ordinal()];
            sidedCableCollisionBoxes.put(side, new AxisAlignedBB(b[0], b[1], b[2], b[3], b[4], b[5]));
        }

        float[][] collisionBoxesRaw = new float[][]{
                {0.19F, 0.81F}, {0.005F, selectionDepthFactor}, {0.19F, 0.81F}
        };
        collisionBoxes = EnumFacingMap.newMap();
        for (EnumFacing side : EnumFacing.VALUES) {
            // Copy bounds
            float[][] bounds = new float[collisionBoxesRaw.length][collisionBoxesRaw[0].length];
            for (int i = 0; i < bounds.length; i++)
                bounds[i] = Arrays.copyOf(collisionBoxesRaw[i], collisionBoxesRaw[i].length);

            // Transform bounds
            MatrixHelpers.transform(bounds, side);
            collisionBoxes.put(side, new AxisAlignedBB(bounds[0][0], bounds[1][0], bounds[2][0], bounds[0][1], bounds[1][1], bounds[2][1]));
        }
    }

    public float getDepthFactor() {
        return depthFactor;
    }

    public float getWidthFactor() {
        return widthFactor;
    }

    public float getHeightFactor() {
        return heightFactor;
    }

    public AxisAlignedBB getSidedCableBoundingBox(EnumFacing side) {
        return sidedCableCollisionBoxes.get(side);
    }

    public AxisAlignedBB getBoundingBox(EnumFacing side) {
        return collisionBoxes.get(side);
    }

}

@@ -2162,24 +2162,144 @@
 %0A        %7D%0A%0A
+        float min = (1 - widthFactor) / 2 + 0.0025F;%0A        float max = (1 - widthFactor) / 2 + widthFactor - 0.0025F;%0A
         floa
@@ -2356,28 +2356,24 @@
        %7B
-0.19F, 0.81F
+min, max
 %7D, %7B0.00
@@ -2404,20 +2404,16 @@
 %7D, %7B
-0.19F, 0.81F
+min, max
 %7D%0A  

Fix collision boxes ignoring part render positions