using UnityEngine;
using UnityEngine.UI;

public class PlayerCollectables : MonoBehaviour {

    [Header("Collectables")]
    [SerializeField] private int currentApples;
    private Player player;

    [SerializeField] private Text currentAppleText;
    [SerializeField] private Text currentHealthText;

    void Start() {
        currentApples = 0;
        player = this.gameObject.GetComponent<Player>();
    }

    private void Update()
    {
        currentHealthText.text = player.maxHealth.ToString();
    }

    private void OnTriggerEnter2D(Collider2D collision) {

        if (collision.gameObject.tag == "Apple") {
            currentApples++;
            currentAppleText.text = currentApples.ToString();
            collision.gameObject.GetComponent<CircleCollider2D>().enabled = false;
            collision.gameObject.transform.GetChild(0).gameObject.GetComponent<Animator>().SetTrigger("Collected");
            Destroy(collision.gameObject, 1f);
        }

        if (collision.gameObject.tag == "Heart") { 
            
            if (player.maxHealth < 5) {
                player.maxHealth++;
                collision.gameObject.GetComponent<PolygonCollider2D>().enabled = false;
                collision.gameObject.transform.GetChild(0).gameObject.GetComponent<Animator>().SetTrigger("Collected");
                Destroy(collision.gameObject, 1f);
            }
            else {
              
                Debug.Log(" Don't collect the heart");
            }
        }
    }
}
