using UnityEngine;

public class BulletController : MonoBehaviour
{
    public GameObject explosionPrefab; // Reference to the Particle System Prefab

    private float speed;
    private int damage;
    private float despawnTime;
    private GameObject owner;

    public void Initialize(float speed, int damage, float despawnTime, GameObject owner)
    {
        this.speed = speed;
        this.damage = damage;
        this.despawnTime = despawnTime;
        this.owner = owner;
        // Additional initialization logic if needed
    }

    private void Start()
    {
        // Set a timer to despawn the bullet after a certain time
        Destroy(gameObject, despawnTime);
    }

    private void Update()
    {
        // Move the bullet forward
        transform.Translate(Vector3.up * speed * Time.deltaTime);
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        // Check if the collision is with an object tagged as an enemy and that the bullet owner is the player
        if (other.CompareTag("Enemy") && owner.CompareTag("Player"))
        {
            // Play sound effect
            AudioManager.instance.PlayBulletCollisionSound();

            // Get the Enemy component from the collided object
            Enemy enemy = other.GetComponent<Enemy>();

            // Check if the Enemy component is not null
            if (enemy != null)
            {
                // Call the TakeDamage function on the enemy, passing the bullet damage
                enemy.TakeDamage(damage);
            }

            // Instantiate the Particle System Prefab at the bullet's position
            Instantiate(explosionPrefab, transform.position, Quaternion.identity);

            // Destroy the bullet
            Destroy(gameObject);
        }
        else if (other.CompareTag("Obstacle"))
        {
            // Play sound effect
            AudioManager.instance.PlayBulletCollisionSound();

            // Instantiate the Particle System Prefab at the bullet's position
            Instantiate(explosionPrefab, transform.position, Quaternion.identity);

            // Destroy the bullet
            Destroy(gameObject);
        }

        // Check if the collision is with an object tagged as the player and that the bullet owner is the enemy
        if (other.CompareTag("Player") && owner.CompareTag("Enemy"))
        {
            // Play sound effect
            AudioManager.instance.PlayBulletCollisionSound();

            // Get the SpaceshipController component from the player
            PlayerController player = other.GetComponent<PlayerController>();

            // Check if the SpaceshipController component is not null
            if (player != null)
            {
                // Call the TakeDamage function on the player, passing the bullet damage
                player.TakeDamage(damage);
            }

            // Instantiate the Particle System Prefab at the bullet's position
            Instantiate(explosionPrefab, transform.position, Quaternion.identity);

            // Destroy the bullet
            Destroy(gameObject);
        }
    }
}
