using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ScreenNotification : MonoBehaviour
{
	public static ScreenNotification Instance;
	private Text Text;

	private void Awake()
	{
		Instance = this;
		Text = GetComponent<Text>();
		Text.CrossFadeColor(FadeOutColor, 0f, true, true);
	}

	public int Show(string message, float duration)
	{
		Text.text = message;
		FadeIn(duration);
		CurrentMessageId = ++LastGivenMessageId;
		return CurrentMessageId;
	}

	public void Hide()
	{
		FadeOut();
	}

	public void Hide(int messageId)
	{
		if (CurrentMessageId == messageId)
		{
			FadeOut();
		}
	}

	private int CurrentMessageId;
	private static int LastGivenMessageId = 100;

	public Color FadeInColor = new Color(1f, 1f, 1f, 1f);
	public Color FadeOutColor = new Color(0f, 0f, 0f, 0f);

	private void FadeIn(float duration)
	{
		CancelInvoke("FadeIn");
		CancelInvoke("FadeOut");

		Text.CrossFadeColor(FadeInColor, 0.5f, true, true);

		if (duration > 0)
		{
			Invoke("FadeOut", duration);
		}
	}

	private void FadeOut()
	{
		CancelInvoke("FadeIn");
		CancelInvoke("FadeOut");

		Text.CrossFadeColor(FadeOutColor, 0.7f, true, true);
	}
}
