using UnityEngine;
using UnityEngine.Events;


public class ContactAction : MonoBehaviour
{
    public bool isCollisionActive; // Ob man wirklich mit dem Ding physisch kollidiert (oder ob man durch kann)
    public UnityEvent onContact;
    private Collider2D colliderComponent;
    private GameObject thingThatCollided; // wird wohl meist der Spieler sein
    private TextNote textNote;


    private void Awake()
    {
        colliderComponent = this.GetComponent<Collider2D>();
    }

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        colliderComponent.isTrigger = !isCollisionActive;
    }

    public void Kill()
    {
        if (thingThatCollided.GetComponent<Player>() != null)
        {
            thingThatCollided.GetComponent<Player>().Kill();
        }
    }
    public void Yeet()
    {
        if (thingThatCollided.GetComponent<Rigidbody2D>() != null)
        {
            thingThatCollided.GetComponent<Rigidbody2D>().AddForce(Vector2.up * 10f, ForceMode2D.Impulse);
        }
    }
    public void ShowText()
    {
        if (this.gameObject.GetComponent<TextNote>() != null)
        {
            textNote = this.gameObject.GetComponent<TextNote>();
            GameManager.gameManager.ShowText(textNote.headline, textNote.content);
        }
        else
        {
            Debug.Log("keine Component TextNote vorhanden");
        }

    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (isCollisionActive)
        {
            thingThatCollided = collision.gameObject;
            onContact.Invoke();
        }
    }

    private void OnTriggerEnter2D(Collider2D collider)
    {
        if (!isCollisionActive)
        {
            thingThatCollided = collider.gameObject;
            onContact.Invoke();
        }
    }

}
