package ei.engine.effects;

import java.awt.Rectangle;

import org.lwjgl.opengl.GL11;

import ei.engine.scene.Entity;
import ei.engine.texture.Texture;
import ei.engine.texture.TextureLoader;

/**
 * Displays text on the screen by a bitmap
 * @author Ziver
 *
 */

public class BitmapText extends Entity{
    private Texture texture;
    
    // The Text to display
    private String text;
    // Base Display List For The Font Set
    private static int base = -1;
    private boolean bold = false;
	
	/**
	 * Create a Text object
	 * 
	 * @param name The name of this entity
	 */
    public BitmapText(String name) {
		super(name);
		texture = TextureLoader.getTextureLoaderInstance().getTexture("data/font.bmp");
		if(base < 0)
			buildFont();
	}
    
    /**
     * Set the text to be displayed
     * 
     * @param msg The text
     */
    public void setText(String msg){
    	text = msg;
    }
	
    public void setBold(boolean b){
    	bold = b;
    }
    
    /**
     * Render the text
     */
    public void render(){
    	if(text != null && !text.equals("")){
    		// store the current model matrix
    		GL11.glPushMatrix();
    		
    		//Sets the location
    		super.setTranslationGL();
    		// No rotation enabled
    		//super.setRotationGL(texture.getImageWidth()/2,texture.getImageHeight()/2);
    		//Sets the scale of the sprite
    		super.setScaleGL();
    		
    		texture.bindGL();
    		
            int baseOffset = base - 32;                // Choose The Font Set (0 or 1)
            if(bold) baseOffset+= 128;
            for(int i=0;i<text.length();i++) {
                GL11.glCallList(baseOffset + text.charAt(i));
                GL11.glTranslatef(3.0f, 0.0f, 0.0f);
            }
    		
    		// restore the model view matrix to prevent contamination
    		GL11.glPopMatrix();
    	}
    }
    
    /**
     * From NeHe
     * @author Mark Bernard
     *
     */
    private void buildFont() {                                  // Build Our Font Display List
        float   cx;                                             // Holds Our X Character Coord
        float   cy;                                             // Holds Our Y Character Coord

        base = GL11.glGenLists(256);                            // Creating 256 Display Lists
        texture.bindGL();     									// Select Our Font Texture
        for (int i=0;i<256;i++) {                               // Loop Through All 256 Lists
            cx = ((float)(i % 16)) / 16.0f;                     // X Position Of Current Character
            cy = ((float)(i / 16)) / 16.0f;                     // Y Position Of Current Character

            GL11.glNewList(base + i, GL11.GL_COMPILE);          // Start Building A List
            GL11.glBegin(GL11.GL_QUADS);                        // Use A Quad For Each Character
            GL11.glTexCoord2f(cx, 1 - cy - 0.0625f);            // Texture Coord (Bottom Left)
            GL11.glVertex2i(0, 0);                              // Vertex Coord (Bottom Left)
            GL11.glTexCoord2f(cx + 0.0625f, 1 - cy - 0.0625f);  // Texture Coord (Bottom Right)
            GL11.glVertex2i(16,0);                              // Vertex Coord (Bottom Right)
            GL11.glTexCoord2f(cx + 0.0625f, 1 - cy);            // Texture Coord (Top Right)
            GL11.glVertex2i(16,16);                             // Vertex Coord (Top Right)
            GL11.glTexCoord2f(cx, 1 - cy);                      // Texture Coord (Top Left)
            GL11.glVertex2i(0, 16);                             // Vertex Coord (Top Left)
            GL11.glEnd();                                       // Done Building Our Quad (Character)
            GL11.glTranslatef(10.0f, 0.0f, 0.0f);               // Move To The Right Of The Character
            GL11.glEndList();                                   // Done Building The Display List
        }                                                       // Loop Until All 256 Are Built
    }
    
	/**
	 * Returns the bound of this class
	 */
	public Rectangle getBound() {
		return new Rectangle(
				(int)getLocation().getX(),
				(int)getLocation().getY(),
				16*(text!=null?text.length():0),16);
	}

	@Override
	public Texture getTexture() {
		return texture;
	}
}
