package client.renderer.tileentity;

import org.lwjgl.opengl.GL11;

import client.renderer.Drawing;
import client.renderer.GlState;
import client.renderer.model.ModelSign;
import common.block.Block;
import common.block.tile.BlockStandingSign;
import common.block.tile.BlockWallSign;
import common.init.Blocks;
import common.tileentity.TileEntitySign;
import common.world.State;


public class TileEntitySignRenderer extends TileEntitySpecialRenderer<TileEntitySign>
{
    private static final String SIGN_TEXTURE = "textures/blocks/sign.png";

    /** The ModelSign instance for use in this renderer */
    private final ModelSign model = new ModelSign();

    public void renderTileEntityAt(TileEntitySign te, double x, double y, double z, float partialTicks, int destroyStage)
    {
        Block block = te.getBlockType();
        GL11.glPushMatrix();
        float f = 0.6666667F;

        State state = te.getBlockState();
        if (block == Blocks.sign)
        {
            GL11.glTranslatef((float)x + 0.5F, (float)y + 0.75F * f, (float)z + 0.5F);
            float f1 = state.getBlock() == Blocks.sign ? (float)(state.getValue(BlockStandingSign.ROTATION) * 360) / 16.0F : 0.0F;
            GL11.glRotatef(-f1, 0.0F, 1.0F, 0.0F);
            this.model.signStick.showModel = true;
        }
        else
        {
            int k = state.getBlock() == Blocks.wall_sign ? 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, -0.4375F);
            this.model.signStick.showModel = false;
        }

        if (destroyStage >= 0)
        {
            this.bindTexture(DESTROY_STAGES[destroyStage]);
            GL11.glMatrixMode(GL11.GL_TEXTURE);
            GL11.glPushMatrix();
            GL11.glScalef(4.0F, 2.0F, 1.0F);
            GL11.glTranslatef(0.0625F, 0.0625F, 0.0625F);
            GL11.glMatrixMode(GL11.GL_MODELVIEW);
        }
        else
        {
            this.bindTexture(SIGN_TEXTURE);
        }

        GlState.enableRescaleNormal();
        GL11.glPushMatrix();
        GL11.glScalef(f, -f, -f);
        this.model.renderSign();
        GL11.glPopMatrix();
//        FontRenderer fontrenderer = this.getFontRenderer();
        float f3 = 0.015625F * f;
        GL11.glTranslatef(0.0F, 0.5F * f, 0.07F * f);
        GL11.glScalef(f3, -f3, f3);
        GL11.glNormal3f(0.0F, 0.0F, -1.0F * f3);
        GlState.depthMask(false);
        int i = 0;

        if (destroyStage < 0)
        {
            for (int j = 0; j < te.signText.length; ++j)
            {
                if (te.signText[j] != null)
                {
//                    Text ichatcomponent = new Text(te.signText[j]);
//                    List<Text> list = ichatcomponent.split(90, false);
                    String s = te.signText[j].length() > 50 ? te.signText[j].substring(0, 50) : te.signText[j]; // list != null && list.size() > 0 ? ((Text)list.get(0)).getFormattedText() : "";

//                    if (j == te.lineBeingEdited)
//                    {
//                        s = "> " + s + " <";
//                        SKC.drawString(s, -SKC.getStringWidth(s) / 2, j * 10 - te.signText.length * 5, i);
//                    }
//                    else
//                    {
                    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();

        if (destroyStage >= 0)
        {
            GL11.glMatrixMode(GL11.GL_TEXTURE);
            GL11.glPopMatrix();
            GL11.glMatrixMode(GL11.GL_MODELVIEW);
        }
    }
	
	
//	private void drawdString(String text, int x, int y) {
//		Drawing.drawText(text, x, y, 0xff000000, false);
//	}
//	private int getdStringWidth(String text) {
//		Vec2i size = Drawing.getTextSize(text);
//		return size.xpos;
//	}
}
