package speiger.src.coreengine.rendering.gui.renderer;

import speiger.src.coreengine.rendering.gui.components.TextComponent;
import speiger.src.coreengine.rendering.textures.base.ITexture;

public interface IFontRenderer
{
	public static final int BOLD = 1;
	public static final int SPECIAL = 2;
	
	public float getFontHeight();
	public float getBaseLine();
	public ITexture getTexture();
	public CharInstance getInstance(int codepoint, boolean bold);
	
	public void updateText(TextComponent component);
	
	public default String trimStringToWidth(String text, float limit){return trimStringToWidth(text, limit, false);}
	public String trimStringToWidth(String text, float limit, boolean reverse);
	
	public default float getCharLength(int codepoint){return getCharLength(codepoint, false);};
	public float getCharLength(int codepoint, boolean bold);
	
	public default float getTextLength(String text){return getTextLength(text, 0);};
	public float getTextLength(String text, int flags);
	public float[] getTextLengths(String text, int flags);
	
	public default float getTextHeight(String text){return getTextHeight(text, 0);};
	public float getTextHeight(String text, int flags);
	
	public String[] splitLines(String text, float maxWidth, int flags);
	
	public boolean isCharValid(int codepoint);
	public default String clearInvalidLetters(String text) 
	{
		if(text == null || text.isEmpty())
		{
			return "";
		}
		StringBuilder builder = new StringBuilder(text.length());
		for(int i = 0,m=text.length();i<m;)
		{
			int codePoint = text.codePointAt(i);
			if(isCharValid(codePoint)) builder.append(Character.toChars(codePoint));
			i += Character.charCount(codePoint);
		}
		return builder.toString();
	}
	
	
	public static final class CharInstance
	{
		int character;
		float width;
		float height;
		float minU;
		float minV;
		float maxU;
		float maxV;
		float xAdvance;
		boolean bold;
		
		public CharInstance(int character, boolean bold)
		{
			this.character = character;
			this.bold = bold;
		}
		
		public CharInstance(int character, int width, int height, float minU, float minV, float maxU, float maxV, int xAdvance, boolean bold)
		{
			this.character = character;
			this.width = width;
			this.height = height;
			this.minU = minU;
			this.minV = minV;
			this.maxU = maxU;
			this.maxV = maxV;
			this.xAdvance = xAdvance;
			this.bold = bold;
		}
		
		public void scale(float scale)
		{
			width *= scale;
			height *= scale;
			xAdvance *= scale;
		}
		
		public int getCharacter()
		{
			return character;
		}
		
		public float getWidth()
		{
			return width;
		}
		
		public float getHeight()
		{
			return height;
		}
		
		public float getMinU()
		{
			return minU;
		}
		
		public float getMinV()
		{
			return minV;
		}
		
		public float getMaxU()
		{
			return maxU;
		}
		
		public float getMaxV()
		{
			return maxV;
		}
		
		public float getXAdvance()
		{
			return xAdvance;
		}
		
		public boolean isBold()
		{
			return bold;
		}
	}
}
