using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bubbleshooter : MonoBehaviour
{
    public GameObject bubblePrefab; // Prefab of the bubble to shoot
    [SerializeField] private GameObject tentacle;
    private float bubbleSpeed = 20f;  // Speed of the bubble
    private float bubbleLifetime = 8f; // Time before the bubble disappears
    public string platformLayerName = "Platform"; // Name of the platform layer
    public string bubbleLayerName = "Bubble"; // Name of the bubble layer
    

    IEnumerator Start()
    {
        // Ignore collisions between the Bubble layer and Platform layer
        int platformLayer = LayerMask.NameToLayer(platformLayerName);
        int bubbleLayer = LayerMask.NameToLayer(bubbleLayerName);

        if (platformLayer != -1 && bubbleLayer != -1)
        {
            Physics2D.IgnoreLayerCollision(bubbleLayer, platformLayer);
        }
        else
        {
            Debug.LogError("Platform or Bubble layer not set up correctly in Unity!");
        }

        // Shoot one bubble when the shooter spawns
        GameObject spawnedTentacle = Instantiate(tentacle, transform.position, Quaternion.identity);
        yield return new WaitForSeconds(1f);
        ShootBubble();
        Destroy(spawnedTentacle);
    }



    void ShootBubble()
    {
        if (bubblePrefab == null)
        {
            Debug.LogError("Bubble Prefab is not assigned!");
            return;
        }

        // Instantiate the bubble at the current GameObject's position
        GameObject bubble = Instantiate(bubblePrefab, transform.position, Quaternion.identity);

        // Set the bubble's layer to the "Bubble" layer
        bubble.layer = LayerMask.NameToLayer(bubbleLayerName);

        // Generate a random direction
        Vector2 randomDirection = Random.insideUnitCircle.normalized;

        // Set the bubble's velocity to move in the random direction
        Rigidbody2D rb = bubble.GetComponent<Rigidbody2D>();
        if (rb != null)
        {
            rb.velocity = randomDirection * bubbleSpeed;
        }
        else
        {
            Debug.LogError("No Rigidbody2D found on the bubble prefab!");
        }

        // Destroy the bubble after its lifetime
        var _audio = bubble.GetComponent<EnemyAudioHandler>().getAudioClip();
        AudioSource.PlayClipAtPoint(_audio, bubble.transform.position);
        Destroy(bubble, bubbleLifetime);
    }
}