using UnityEngine;
using System.Collections;

public class TextEffect : MonoBehaviour {

	MonotypeText label;
	public float showTime = 0;
	const float SHOW_TIME = 0.4f;
	float miniTimer = 0;
	const float MINI_TIME = 0.1f;
	
	static TextEffect[] effects;
	
	public static void Init(Transform transform, int num){
		effects = new TextEffect[num];
		for (int i = 0; i < effects.Length; i++){
			effects[i] = Utils.Create<TextEffect>(transform, Vector3.zero);
		}
	}
	
	public static void ShowMessage(string message, Vector3 position, float time = SHOW_TIME){
		for (int i = 0; i < effects.Length; i++){
			if (effects[i].showTime <= 0){
				effects[i].SetMessage(message, position, time);
				break;
			}
		}
	}
	
	void Start () {
		label = gameObject.AddComponent<MonotypeText>();
		label.SetMaterial(Resources.Load("Material/Font") as Material);
		label.visible = false;
	}
	
	public void SetMessage(string message, Vector3 position, float time = SHOW_TIME){
		label.text = message;
		transform.localPosition = position;
		showTime = time;
		label.visible = true;
	}
	
	void Update () {
		if (showTime > 0) {
			showTime -= Time.deltaTime;
			label.SetColor(new Color(1, 1, 1, showTime / SHOW_TIME));
			miniTimer -= Time.deltaTime;
			if (miniTimer < 0){
				transform.position += new Vector3(0, Tile.TILE_SIZE / 16f, 0);
				miniTimer = MINI_TIME;
			}
			if (showTime <= 0){
				showTime = 0;
				label.visible = false;
			}				
		}
	}
}
