using UnityEngine;

public class BubbleMovement : MonoBehaviour
{
    private Vector3 velocity;
    private float speed;
    private float bounceFactor = 0.7f; // Reduced bounce factor to simulate slower movement
    private float driftFactor = 0.05f; // Controls the randomness of the drifting effect
    private float floatSpeed = 0.2f;   // Controls the floating speed (gentle upwards movement)

    public void SetSpeed(float bubbleSpeed)
    {
        speed = bubbleSpeed;
    }

    private void Start()
    {
        // Randomize the initial direction and speed for a gentle floating effect
        velocity = new Vector3(Random.Range(-1f, 1f), Random.Range(-1f, 1f), 0).normalized * speed;
    }

    private void Update()
    {
        // Move the bubble based on velocity
        transform.position += velocity * Time.deltaTime;

        // Apply gentle upward drifting movement to simulate bubble floating
        velocity.y += floatSpeed * Time.deltaTime;

        // Add randomness to movement for more natural drifting
        velocity.x += Random.Range(-driftFactor, driftFactor);
        velocity.y += Random.Range(-driftFactor, driftFactor);

        // Slow down the velocity over time to simulate air resistance
        velocity *= (1 - Time.deltaTime * 0.02f);

        // Check for bouncing off the screen edges
        CheckBounds();
    }

    private void CheckBounds()
    {
        Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);

        if (screenPos.x <= 0 || screenPos.x >= Screen.width)
        {
            velocity.x = -velocity.x * bounceFactor; // Bounce off left/right edges
        }

        if (screenPos.y <= 0 || screenPos.y >= Screen.height)
        {
            velocity.y = -velocity.y * bounceFactor; // Bounce off top/bottom edges
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        // If the bubble hits a bullet, pop it
        if (collision.gameObject.CompareTag("bullet_1"))
        {
            // Award point to player 1
            GameManager.Instance.UpdateP1Score();
            PopBubble(); // Pop the bubble
        }
        else if (collision.gameObject.CompareTag("bullet_2"))
        {
            // Award point to player 2
            GameManager.Instance.UpdateP2Score();
            PopBubble(); // Pop the bubble
        }
        // If the bubble hits any of the barriers, bounce away
        else if (collision.gameObject.CompareTag("top_bar") ||
                 collision.gameObject.CompareTag("bottom_bar") ||
                 collision.gameObject.CompareTag("middle_bar") ||
                 collision.gameObject.CompareTag("left_bar") ||
                 collision.gameObject.CompareTag("right_bar"))
        {
            BounceAway(collision.contacts[0].normal); // Bounce away using the collision normal
        }
    }

    private void BounceAway(Vector2 collisionNormal)
    {
        // Reflect the velocity based on the collision normal and apply the bounce factor
        velocity = Vector3.Reflect(velocity, collisionNormal) * bounceFactor;
    }

    private void PopBubble()
    {
        Destroy(gameObject);
    }
}
