using UnityEngine;
using System.Collections.Generic;

public class bubble : MonoBehaviour
{
    public enum bubbleType
    {
        thought,
        speech,
        shout,
        squeak
    };


    Collider2D col;
    PlatformEffector2D affector;
    SpringJoint2D spring;

    [Header("settings")]
    public bubbleType type = new bubbleType();
    [SerializeField]
    float destroyTimeTouch = 2;
    [SerializeField]
    float destroyTimePassive = 60;
    [SerializeField]
    [Tooltip("how hard the bubble resists external forces")]
    float springElasticity = 5;
    [SerializeField]
    float pushForce = 10;

    [Header("internal variables")]
    public Vector3 forceDirection;
    Vector3 force;


    void Start()
    {
        //assigning component variables
        col = gameObject.GetComponent<Collider2D>();
        affector = gameObject.GetComponent<PlatformEffector2D>();
        spring = gameObject.GetComponent<SpringJoint2D>();


        if (spring != false)
        {
            spring.frequency = springElasticity;
        }
        //checking effector for warning
        if (affector == null && type != bubbleType.speech)
        {
            Debug.LogWarning(type + " bubble requires platform effector to function properly");
        }

        if (col != null)
        {
            if (type == bubbleType.thought)
            {

            }
            if (type == bubbleType.speech)
            {
                affector.enabled = false;
            }
            if (type == bubbleType.shout)
            {

            }
            if (type == bubbleType.squeak)
            {
                Debug.Log("passively destroying");
                Destroy(gameObject, destroyTimePassive);
            }
        }
        else
        {
            Debug.LogWarning("bubble has no collider");
        }
    }

    void FixedUpdate()
    {
        force = forceDirection * pushForce;
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (type == bubbleType.squeak)
        {
            Debug.Log("touched. destroying");
            Destroy(gameObject, destroyTimeTouch);
        }
    }

    public void ChildTriggerEnter(GameObject foundObject)
    {
        if (foundObject.GetComponent<Rigidbody2D>() != null)
        {
            foundObject.GetComponent<Rigidbody2D>().AddForce(forceDirection * pushForce, ForceMode2D.Impulse);
        }
    }
}