using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BubbleManager : MonoBehaviour
{
    public const float defaultBubbleSize = 1.0f, defaultLevitationSpeed = 1.0f, defaultLifetime = 7.0f, defaultExpansionRate = 0.2f;

    public bool overwriteManagerDefaults;
    public List<Bubble> bubbles;

    public GameObject bubblePrefab;
    public void createBubble(Vector3 position, Vector3 velocity, float size = defaultBubbleSize, float levitationSpeed = defaultLevitationSpeed, float lifeTime = defaultLifetime, float expansionRate = defaultExpansionRate)
    {
        Bubble bubble = Instantiate(bubblePrefab).GetComponent<Bubble>();

        bubble.transform.position = position;
        bubble.velocity = velocity;
        if (!overwriteManagerDefaults)
        {
            
            
            bubble.size = size;
            bubble.levitateSpeed = levitationSpeed;
            bubble.lifeTime = lifeTime;
            bubble.expansionRate = expansionRate;

        }


        bubble.bubbleManager = this;


        if (bubbles == null)
        {
            bubbles = new List<Bubble>();
        }

        bubbles.Add(bubble);
    }



    public float isInBubble(Vector3 pos, float threshold = 0.0f) // negative bubble force multipler => not inside a bubble
    {
        // early return conditions for if there are no bubbles in the scene
        if (bubbles == null)
        {
            return -1.0f;
        }

        if (bubbles.Count <= 0)
        {
            return -1.0f;
        }


        foreach (Bubble bubble in bubbles) // can be a foreach loop as breaking loop as soon as one bubble is found
        {
            if ((pos - bubble.transform.position).magnitude < bubble.size / 2 + threshold)
            {
                bubble.Pop();
                bubbles.Remove(bubble);

                return bubble.getBubbleForceMultipler();
            }


        }

        return -1.0f;
    }

    public Bubble[] bubblesAtPos(Vector3 pos, float threshold = 0.0f)
    {
        List<Bubble> bubblesFound = new List<Bubble>();

        // early return conditions for if there are no bubbles in the scene
        if (bubbles == null || bubbles.Count <= 0)
        {
            return bubblesFound.ToArray();
        }

        foreach (Bubble bubble in bubbles)
        {
            if ((pos - bubble.transform.position).magnitude < bubble.size / 2 + threshold)
            {
                bubblesFound.Add(bubble);
            }

        }


        return bubblesFound.ToArray();
    }

}
