using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using matnesis.TeaTime;

public class UIManager : MonoBehaviour 
{
	public Text moneyText;
	public Text lvlText;

	public Sprite heartFull;
	public Sprite heartEmpty;
	public Sprite bulletFull;
	public Sprite bulletEmpty;

	public Image[] hearts;
	public Image[] bullets;
	private PlayerShoot shoot;
	void Start()
	{
		shoot = FindObjectOfType<PlayerShoot> ();
		ChangeBullets ();
		ChangeHearts ();
		this.tt ("ShowLevel").Add (() => 
			{
				lvlText.text = "Level 1 - " + GameManager.currentLevel.ToString();
			}).Add(2, () => 
				{ 
					lvlText.text = ""; 
				}).Immutable();
	}

	public void ChangeHearts ()
	{
		for(int i = 0; i < hearts.Length; i++)
		{
			if(i < GameManager.playerLifes)
			{
				hearts [i].sprite = heartFull;
			}
			else
			{
				hearts [i].sprite = heartEmpty;
			}
		}
	}

	public void ChangeBullets ()
	{
		for(int i = 0; i < hearts.Length; i++)
		{
			if(i < GameManager.bulletCap - shoot.spentBullets)
			{
				bullets [i].sprite = bulletFull;
			}
			else
			{
				bullets [i].sprite = bulletEmpty;
			}
		}
	}

	public void SetScore (Vector3 pos, int amount)
	{
		this.tt("PositionText").Add(() =>
			{
				Vector3 newPos = Camera.main.WorldToScreenPoint(pos);
				moneyText.GetComponent<RectTransform>().anchoredPosition = new Vector2(newPos.x, newPos.y);
				moneyText.text = "+" + amount;
			}).Add(2, () => { moneyText.text = ""; });
	}
}
