package org.cyclops.integrateddynamics.client.render.valuetype;

import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
import net.minecraft.util.EnumFacing;
import org.cyclops.cyclopscore.helper.Helpers;
import org.cyclops.integrateddynamics.api.client.render.valuetype.IValueTypeWorldRenderer;
import org.cyclops.integrateddynamics.api.evaluate.variable.IValue;
import org.cyclops.integrateddynamics.api.part.IPartContainer;
import org.cyclops.integrateddynamics.api.part.IPartType;

/**
 * A simple text-based value type world renderer.
 * @author rubensworks
 */
public class TextValueTypeWorldRenderer implements IValueTypeWorldRenderer {

    private static final float MAX = 12.5F;
    private static final float MARGIN_FACTOR = 1.1F;

    @Override
    public void renderValue(IPartContainer partContainer, double x, double y, double z, float partialTick,
                            int destroyStage, EnumFacing direction, IPartType partType, IValue value,
                            TileEntityRendererDispatcher rendererDispatcher, float distanceAlpha) {
        FontRenderer fontRenderer = rendererDispatcher.getFontRenderer();
        float maxWidth = 0;

        String[] lines = value.getType().toCompactString(value).split("\\\\n");
        for (String line : lines) {
            float width = fontRenderer.getStringWidth(line) - 1;
            maxWidth = Math.max(maxWidth, width);
        }

        float singleHeight = fontRenderer.FONT_HEIGHT;
        float totalHeight = singleHeight * lines.length;

        GlStateManager.pushMatrix();
        GlStateManager.enableRescaleNormal();

        float scaleX = MAX / (maxWidth * MARGIN_FACTOR);
        float scaleY = MAX / (totalHeight * MARGIN_FACTOR);
        float scale = Math.min(scaleX, scaleY); // Maintain aspect ratio
        float newWidth = maxWidth * scale;
        float newHeight = totalHeight * scale;
        GlStateManager.translate((MAX - newWidth) / 2, (MAX - newHeight) / 2, 0F);
        GlStateManager.scale(scale, scale, 1F);

        int offset = 0;
        for(String line : lines) {
            int color = Helpers.addAlphaToColor(value.getType().getDisplayColor(), distanceAlpha);
            rendererDispatcher.getFontRenderer().drawString(line, 0, offset, color);
            offset += singleHeight;
        }

        GlStateManager.disableRescaleNormal();
        GlStateManager.popMatrix();
    }
}

@@ -1355,16 +1355,28 @@
 .split(%22
+(?%3C=%5B%5E%5C%5C%5C%5C%5D)
 %5C%5C%5C%5Cn%22);
@@ -1470,13 +1470,25 @@
 dth(
+polishLine(
 line)
+)
  - 1
@@ -2377,20 +2377,32 @@
 wString(
+polishLine(
 line
+)
 , 0, off
@@ -2549,11 +2549,119 @@
 );%0A    %7D
+%0A%0A    protected String polishLine(String line) %7B%0A        return line.replaceAll(%22%5C%5C%5C%5C%5C%5C%5C%5Cn%22, %22%5C%5C%5C%5Cn%22);%0A    %7D
 %0A%7D%0A

Allow \n to be escaped in display panels, Closes #311