using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class BitmapFontDemo : MonoBehaviour 
{
	public SpriteManager spriteManager;	
	public TextAsset fontXML1;
	public TextAsset fontXML2;
	
	BitmapFont bmf1;
	BitmapFont bmf2;

	List<Sprite> charSprites = new List<Sprite>();
	int charIndex = 0;

	void Start () 
	{
		bmf1 = new BitmapFont(fontXML1.text, 0, 0);
		bmf2 = new BitmapFont(fontXML2.text, 256, 0);

		AddText(bmf1, "abcdefghijklmnopqrstuvxyz", 32, 40);
		AddText(bmf1, "ABCDEFGHIJKLMNOPQRSTUVXYZ", 32, 70);
				
		AddText(bmf2, "The quick brown fox jumps over the lazy dog", 52, 120, true);
		AddText(bmf1, "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.\nNam cursus. Morbi ut mi. Nullam enim leo, egestas id, \ncondimentum at, laoreet mattis, massa. Sed eleifend \nnonummy diam.", 32, 160, true);
		
		AddText(bmf2, "abcdefghijklmnopqrstuvxyz", 64, 280);
		AddText(bmf2, "ABCDEFGHIJKLMNOPQRSTUVXYZ", 32, 320);
	}
	
	void AddText(BitmapFont bmf, string text, int x, int y, bool colorize = false)
	{
		List<BitmapFont.Sprite> sprites = bmf.GetSprites(text, 0, 0);
		
		foreach(BitmapFont.Sprite s in sprites)
		{
			GameObject go = new GameObject("Glyph \"" + s.c + "\"");
			go.transform.position = new Vector3(s.x + x + s.texCoords.width/2, Screen.height - y - s.y + s.texCoords.height/2, 0.0f);
			
			if(!colorize)
			{
				spriteManager.AddSprite(go, s.texCoords.width, s.texCoords.height, (int)s.texCoords.x, (int)s.texCoords.y + (int)s.texCoords.height, (int)s.texCoords.width, (int)s.texCoords.height, false);
			}
			else
			{	
				Sprite sp = spriteManager.AddSprite(go, s.texCoords.width, s.texCoords.height, (int)s.texCoords.x, (int)s.texCoords.y + (int)s.texCoords.height, (int)s.texCoords.width, (int)s.texCoords.height, false);
				sp.SetColor(new Color(Random.Range(0.2f, 1.0f), Random.Range(0.2f, 1.0f), Random.Range(0.2f, 1.0f)));
			}
		}
	}
	
	void WriteText(BitmapFont bmf, string text, int x, int y)
	{
		List<BitmapFont.Sprite> sprites = bmf.GetSprites(text, 0, 0);
		
		foreach(BitmapFont.Sprite s in sprites)
		{
			// Check if already have created a sprite, if so update it
			if(charIndex < charSprites.Count)
			{
				Sprite sp = charSprites[charIndex];
				
				GameObject go = sp.client;
				go.transform.position = new Vector3(s.x + x + s.texCoords.width/2, Screen.height - y - s.y + s.texCoords.height/2, 0.0f);
				go.name = "glyph \"" + s.c + "\"";
				
				sp.SetSizeXY(s.texCoords.width, s.texCoords.height);				
				sp.lowerLeftUV = spriteManager.PixelCoordToUVCoord((int)s.texCoords.x, (int)s.texCoords.y + (int)s.texCoords.height);
				sp.uvDimensions = spriteManager.PixelSpaceToUVSpace((int)s.texCoords.width, (int)s.texCoords.height);
				sp.hidden = false;
				sp.Transform();
			}
			else
			{
				// Create new sprite
				GameObject go = new GameObject("Glyph \"" + s.c + "\"");
				go.transform.position = new Vector3(s.x + x + s.texCoords.width/2, Screen.height - y - s.y + s.texCoords.height/2, 0.0f);
				charSprites.Add( spriteManager.AddSprite(go, s.texCoords.width, s.texCoords.height, (int)s.texCoords.x, (int)s.texCoords.y + (int)s.texCoords.height, (int)s.texCoords.width, (int)s.texCoords.height, false));
			}
			
			charIndex++;
		}
	}
	
	void WriteTextBegin()
	{
		charIndex = 0;
	}
	
	void WriteTextEnd()
	{
		// Hide all unused charSprites
		for(int i = charIndex; i < charSprites.Count; ++i)
		{
			charSprites[i].hidden = true;	
		}
	}
	
	void Update()
	{
		WriteTextBegin();
		
		WriteText(bmf1, "Timer: " + (int)(Time.time * 10.0f), 32, 400);
		WriteText(bmf2, "Countdown: " + (10 - (int)Time.time), 32, 440);		
		
		WriteTextEnd();
	}
}
