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

public class PopupManager : MonoBehaviour
{
		//public List<UITweener> popups = new List<UITweener> ();
	
		public static PopupManager instance;

		
		public delegate void PopupInHandler ();
		public static event PopupInHandler onPopupIn;
	
		public delegate void PopupOutHandler ();
		public static event PopupOutHandler onPopupOut;
		
		public string currentPopup;
	
		PopupBackground popupBackground;
		
		void Start ()
		{
				instance = this;
				popupBackground = GameObject.FindWithTag ("PopupBackground").GetComponent<PopupBackground> ();
				popupBackground.Hide ();
		}
		
		public void PopupTweenIn (string popupName)
		{		
				Time.timeScale = 0;
				if (currentPopup != "") {
						PopupTweenOut (currentPopup, true);
				}
				currentPopup = popupName;
				Popup popup = transform.Find (popupName).GetComponent<Popup> ();
				popup.Show ();
				
				popupBackground.Show ();
				
				if (onPopupIn != null)
						onPopupIn ();
		}
		
		public void PopupTweenOutCurrent ()
		{
				PopupTweenOut (currentPopup);
		}
		
		public void PopupTweenOut (string popupName, bool forceHide = false)
		{
				Time.timeScale = 1;
				if (currentPopup == "") {
						return;
				}
				Time.timeScale = 1;
				Popup popup = transform.Find (popupName).GetComponent<Popup> ();
				
				if (popup.hideable || forceHide) {
						if (popupName == currentPopup)
								currentPopup = "";
						popup.Hide ();
						popupBackground.Hide ();
						if (onPopupOut != null)
								onPopupOut ();
				}
		}
		

	
}
