using UnityEngine;
using TMPro;

public class Projectile2D : MonoBehaviour
{
    public float initialSpeed = 10f;
    public float launchAngle = 45f;
    public Vector2 startPosition;
    public KeyCode fireKey = KeyCode.Space;
    public KeyCode resetKey = KeyCode.R;

    private Rigidbody2D rb;
    private bool isLaunched = false;

    public TMP_Text messageText;
    public AudioSource audioSource;
    public AudioClip launchSound;
    public AudioClip resetSound;
    public AudioClip collisionSound;

    public GameObject c1; // 🔹 Αναφορά στη μπάλα "c1"
    private SpriteRenderer c1Renderer; // 🔹 SpriteRenderer για αλλαγή χρώματος
    public Color collisionColor = Color.red; // 🔹 Χρώμα που θα πάρει μετά τη σύγκρουση
    private Color originalColor; // 🔹 Αρχικό χρώμα της μπάλας "c1"

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        startPosition = transform.position;
        rb.bodyType = RigidbodyType2D.Static;

        if (messageText != null)
        {
            messageText.gameObject.SetActive(true);
        }

        if (audioSource == null)
        {
            audioSource = GetComponent<AudioSource>();
        }

        // 🔹 Βρίσκουμε το SpriteRenderer της μπάλας "c1"
        if (c1 != null)
        {
            c1Renderer = c1.GetComponent<SpriteRenderer>();
            if (c1Renderer != null)
            {
                originalColor = c1Renderer.color; // Αποθήκευση αρχικού χρώματος
            }
        }
    }

    void Update()
    {
        if (Input.GetKeyDown(fireKey) && !isLaunched)
        {
            LaunchProjectile();
        }

        if (Input.GetKeyDown(resetKey))
        {
            ResetProjectile();
        }
    }

    void LaunchProjectile()
    {
        isLaunched = true;
        rb.bodyType = RigidbodyType2D.Dynamic;

        if (messageText != null)
        {
            messageText.gameObject.SetActive(false);
        }

        if (audioSource != null && launchSound != null)
        {
            audioSource.PlayOneShot(launchSound);
        }

        float angleRad = launchAngle * Mathf.Deg2Rad;
        Vector2 velocity = new Vector2(Mathf.Cos(angleRad), Mathf.Sin(angleRad)) * initialSpeed;
        rb.velocity = velocity;
    }

    void ResetProjectile()
    {
        transform.position = startPosition;
        rb.bodyType = RigidbodyType2D.Static;
        rb.velocity = Vector2.zero;
        rb.angularVelocity = 0f;
        isLaunched = false;

        if (messageText != null)
        {
            messageText.gameObject.SetActive(true);
        }

        if (audioSource != null && resetSound != null)
        {
            audioSource.PlayOneShot(resetSound);
        }

        // 🔹 Επαναφορά χρώματος στη μπάλα "c1"
        if (c1Renderer != null)
        {
            c1Renderer.color = originalColor;
        }
    }

    void OnBecameInvisible()
    {
        if (isLaunched)
        {
            ResetProjectile();
        }
    }

    // 🔹 **Νέο: Σύγκρουση με άλλη μπάλα**
    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.name == "c1") // Έλεγχος αν συγκρούστηκε με τη μπάλα "c1"
        {
            Debug.Log("Οι μπάλες συγκρούστηκαν!");

            // Αλλαγή χρώματος της μπάλας "c1"
            if (c1Renderer != null)
            {
                c1Renderer.color = collisionColor;
            }

            // Αν έχουμε UI Text, το εμφανίζουμε για λίγο
            if (messageText != null)
            {
                messageText.text = "Σύγκρουση! 💥";
                Invoke("ResetMessage", 2f);
            }

            // Παίζουμε ήχο σύγκρουσης
            if (audioSource != null && collisionSound != null)
            {
                audioSource.PlayOneShot(collisionSound);
            }
        }
    }

    void ResetMessage()
    {
        if (messageText != null)
        {
            messageText.text = "Πατήστε Space για νέα βολή!";
        }
    }
}
