package client.renderer.tileentity;

import org.lwjgl.opengl.GL11;

import client.renderer.Drawing;
import client.renderer.GlState;
import common.block.tech.BlockStandingSign;
import common.block.tech.BlockWallSign;
import common.tileentity.TileEntitySign;
import common.world.State;


public class SignRenderer extends ElementRenderer<TileEntitySign>
{
    public void renderElements(TileEntitySign te, double x, double y, double z, float partialTicks)
    {
        GL11.glPushMatrix();
        float f = 0.6666667F;

        State state = te.getState();
        if (state.getBlock() instanceof BlockStandingSign)
        {
            GL11.glTranslatef((float)x + 0.5F, (float)y + 0.75F * f, (float)z + 0.5F);
            int r = state.getValue(BlockStandingSign.FACING).getIndex();
            float f1 = 0.0F;

            if (r == 2)
            {
                f1 = 180.0F;
            }

            if (r == 4)
            {
                f1 = 90.0F;
            }

            if (r == 5)
            {
                f1 = -90.0F;
            }
            GL11.glRotatef(-f1, 0.0F, 1.0F, 0.0F);
            GL11.glTranslatef(0.0F, f * -0.0625f * 2.0f, 0.0F);
        }
        else
        {
            int k = state.getBlock() instanceof BlockWallSign ? state.getValue(BlockWallSign.FACING).getIndex() : 0;
            float f2 = 0.0F;

            if (k == 2)
            {
                f2 = 180.0F;
            }

            if (k == 4)
            {
                f2 = 90.0F;
            }

            if (k == 5)
            {
                f2 = -90.0F;
            }

            GL11.glTranslatef((float)x + 0.5F, (float)y + 0.75F * f, (float)z + 0.5F);
            GL11.glRotatef(-f2, 0.0F, 1.0F, 0.0F);
            GL11.glTranslatef(0.0F, -0.3125F - f * 0.0625f * 0.5f, -0.4375F);
        }

        GlState.enableRescaleNormal();
        float f3 = 0.015625F * f;
        GL11.glTranslatef(0.0F, 0.5F * f, 0.1F * f);
        GL11.glScalef(f3, -f3, f3);
        GL11.glNormal3f(0.0F, 0.0F, -1.0F * f3);
        GlState.depthMask(false);

        for (int j = 0; j < te.text.length; ++j)
        {
            if (te.text[j] != null && !te.text[j].isEmpty())
            {
                String s = te.text[j].length() > 50 ? te.text[j].substring(0, 50) : te.text[j];
                GL11.glPushMatrix();
                GL11.glScalef(0.75f, 0.75f, 0.75f);
                Drawing.drawTextCenteredN(s, 0, j * (18 - 3) - 30, 0xff000000);
                GL11.glPopMatrix();
            }
        }

        GlState.depthMask(true);
        GlState.color(1.0F, 1.0F, 1.0F, 1.0F);
        GL11.glPopMatrix();
    }
}
