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 TMP_Text shotsText;
    public TMP_Text hitsText;
    public TMP_Text speedText; // 🔹 Εμφάνιση αρχικής ταχύτητας
    public TMP_Text angleText; // 🔹 Εμφάνιση αρχικής γωνίας

    private int totalShots = 0;
    private int successfulHits = 0;

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

    public GameObject c1;
    private Rigidbody2D c1Rb;
    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>();
            c1Rb = c1.GetComponent<Rigidbody2D>();

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

        // 🔹 Ενημέρωση των UI τιμών στην αρχή
        UpdateUI();
    }

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

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

        // 🔹 Αλλαγή γωνίας
        if (Input.GetKeyDown(KeyCode.A))
        {
            initialSpeed = Mathf.Clamp(initialSpeed + 1f, 1f, 50f);
            UpdateUI();
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            initialSpeed = Mathf.Clamp(initialSpeed - 1f, 1f, 50f);
            UpdateUI();
        }

        // 🔹 Αλλαγή ταχύτητας
        if (Input.GetKeyDown(KeyCode.K))
        {
            launchAngle = Mathf.Clamp(launchAngle + 5f, 0f, 90f);
            UpdateUI();
        }
        if (Input.GetKeyDown(KeyCode.L))
        {
            launchAngle = Mathf.Clamp(launchAngle - 5f, 0f, 90f);
            UpdateUI();
        }
    }

    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;

        totalShots++;
        UpdateUI();
    }

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

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

        if (c1 != null)
        {
            float minX = 0.5f;
            float maxX = GetScreenBounds().x * 0.8f;
            float randomX = Random.Range(minX, maxX);
            c1.transform.position = new Vector2(randomX, c1.transform.position.y);

            if (c1Rb != null)
            {
                c1Rb.velocity = Vector2.zero;
                c1Rb.angularVelocity = 0f;
            }
        }
    }

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

            successfulHits++;
            UpdateUI();
        }
    }

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

    void UpdateUI()
    {
        if (shotsText != null)
        {
            shotsText.text = "Βολές: " + totalShots;
        }
        if (hitsText != null)
        {
            hitsText.text = "Επιτυχίες: " + successfulHits;
        }
        if (speedText != null)
        {
            speedText.text = "Ταχύτητα: " + initialSpeed.ToString("F1");
        }
        if (angleText != null)
        {
            angleText.text = "Γωνία: " + launchAngle.ToString("F1") + "°";
        }
    }

    Vector2 GetScreenBounds()
    {
        Vector3 screenTopRight = new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z);
        return Camera.main.ScreenToWorldPoint(screenTopRight);
    }
}
