using UnityEngine;
using System.Collections;

public class FlameGUI : MonoBehaviour {

	public Transform flameGuiBar;
	public Transform flameBarParticlePoint;
	public Transform fireParticlesTransform;
	public ParticleSystem fireParticles;
	public Animation messageGUI;
	public TextMesh messageText;


	static float barValueTarget;
	float barValue;
	static float barMax;

	static string message;
	static float messageTimer;
	public static bool showMessage;
	bool isShowingMessage;

	float barScale;
    private static float messageTime = 5f;




	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		//limites maximos y minimos para que la barra no se salga
		if (barValueTarget > barMax)barValueTarget = barMax;
		if (barValueTarget < 0) barValueTarget = 0;

		//formmula para que la barra se llene de forma smooth
		barValue = barValue + (barValueTarget - barValue) * (2f * Time.deltaTime);

		//escala la barra y posiciona las particulas en el lugar que deberia de ser
		flameGuiBar.transform.localScale = new Vector3 (1,(barValue/barMax), 1)*0.026828f;
		fireParticlesTransform.position = flameBarParticlePoint.position;

		//prende o apaga las particulas cuando esta a mas o menos de la mitad
		if (barValueTarget > barMax * .5f) {
			fireParticles.enableEmission = true;
		} else {
			fireParticles.enableEmission = false;
		}


		////////////////
		if (isShowingMessage) {
			messageTimer += Time.deltaTime;
            if (messageTimer > messageTime)
            {
				isShowingMessage = false;
				showMessage = false;
				messageGUI.PlayQueued("MessageOut",QueueMode.PlayNow);
			}
		} else {
			messageText.text =  message;
			if(showMessage){
				isShowingMessage = true;
				messageGUI.PlayQueued("MessageIn",QueueMode.PlayNow);
			}	
		}
		
	}

	static public void SetBarValue(float newValue){
		barValueTarget = newValue;
	}

	static public void SetBarMax(float newMaxValue){
		barMax = newMaxValue;
	}

	static public void PlayMessage(string newMessage){
		message = newMessage;
		messageTimer = 0;
		showMessage = true;
        messageTime = 5f;
	}

    static public void PlayMessage(string newMessage, float duration)
    {
        message = newMessage;
        messageTimer = 0;
        showMessage = true;
        messageTime = duration;
    }

}
