using UnityEngine;

public class FruitBall : MonoBehaviour
{
    private Animator animator;

    public AudioClip spawnSound;

    public AudioClip bounceSound;

    public AudioClip hitSound;

    private AudioSource audioSource;

    private Vector2 speed;

    private float speedMultiplier = 1.0f;

    private float speedIncreaseTimer = 0.0f;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        audioSource = GetComponent<AudioSource>();
        animator = GetComponent<Animator>();
        speed = new Vector2(Mathf.Sign(Random.Range(-1f, 1f)) * 0.0001f,
                            Mathf.Sign(Random.Range(-1f, 1f)) * 0.0001f);

        audioSource.PlayOneShot(spawnSound);
    }

    // Update is called once per frame
    void Update()
    {
        AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);

        if (stateInfo.IsName("FruitBallMove")) {
            transform.Translate(speed * speedMultiplier);
        }

        if (speedIncreaseTimer > 0.0f)
        {
            speedIncreaseTimer -= Time.deltaTime;
            if (speedIncreaseTimer <= 0.0f)
            {
                speedMultiplier = 1.0f; // Reset the speed multiplier to 1x
            }
        }
    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Wall") || 
            collision.gameObject.CompareTag("BasicBall") || 
            collision.gameObject.CompareTag("FruitBall"))
        {
            ContactPoint2D contact = collision.GetContact(0);
            speed = Vector2.Reflect(speed, contact.normal);

            if (collision.gameObject.CompareTag("BasicBall") || 
                collision.gameObject.CompareTag("FruitBall"))
            {
                speedIncreaseTimer = 2.0f; // Reset the timer to 2 seconds
                speedMultiplier = 50.0f; // Set the speed multiplier to 7x
            }
        }
    }
}
