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;
    public Color collisionColor = Color.red;
    private Color originalColor;

    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>();
        }

        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;
        }

        // 🔹 Τοποθέτηση της "c1" σε νέα τυχαία θέση στον άξονα Χ (δεξιά από το 0, εντός της οθόνης)
        if (c1 != null)
        {
            float minX = 0.5f; // Ελάχιστη θέση X (λίγο δεξιά από το 0)
            float maxX = GetScreenBounds().x * 0.8f; // Μέγιστη θέση X (εντός της οθόνης)
            float randomX = Random.Range(minX, maxX);
            c1.transform.position = new Vector2(randomX, c1.transform.position.y);
        }
    }

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

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

            if (c1Renderer != null)
            {
                c1Renderer.color = collisionColor;
            }

            if (messageText != null)
            {
                messageText.text = "Σύγκρουση! 💥";
                Invoke("ResetMessage", 2f);
            }

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

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

    // 🔹 **Νέα συνάρτηση για να υπολογίζει τα όρια της οθόνης**
    Vector2 GetScreenBounds()
    {
        Vector3 screenTopRight = new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z);
        Vector2 worldBounds = Camera.main.ScreenToWorldPoint(screenTopRight);
        return worldBounds;
    }
}
