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

public class BubbleBlower : MonoBehaviour
{
    public BubbleManager bubbleManager;
    public Vector3 bubbleCreationOffset;
    public float bubbleCooldown;
    float timeSinceLastBubble;
    public Transform playerCameraTransform;
    public Movement playerMovement;

    public float initialBubbleSpeed;

    bool waitingForBubbleAnimation;
    public float bubbleSpawnDelay;
    float elapsedTime;

    public CanvasAnimator canvasAnimator;

    public AudioSource audioSource;
    public Animator animator;
    private void Start()
    {
        timeSinceLastBubble = bubbleCooldown + 1; // ensures that a bubble can be created right away 
    }



    private void Update()
    {
        float m0 = Input.GetKey(KeyCode.Mouse0) ? 1 : 0;
        float m1 = Input.GetKey(KeyCode.Mouse1) ? 1 : 0;
        Vector2 input = new Vector2(m0, m1);




        if (m1 == 1 && timeSinceLastBubble >= bubbleCooldown) // if right mouse button pressed...
        {
            /*
            if (!canvasAnimator.isAnimationRunning())
                canvasAnimator.blowBubble();
            */
            animator.speed = 3;
            animator.SetTrigger("blow bubble");
            audioSource.Play();

            waitingForBubbleAnimation = true;
        }


        if (waitingForBubbleAnimation)
        {
            if (elapsedTime >= bubbleSpawnDelay)
            {
                Vector3 newBubblePos = transform.position + playerCameraTransform.forward * bubbleCreationOffset.z + playerCameraTransform.right * bubbleCreationOffset.x + playerCameraTransform.up * bubbleCreationOffset.y;

                bubbleManager.createBubble(newBubblePos, playerMovement.velocity + playerCameraTransform.forward * initialBubbleSpeed);

                timeSinceLastBubble = 0;

                elapsedTime = 0;
                waitingForBubbleAnimation = false;
            }
            else
            {
                elapsedTime += Time.deltaTime;
            }
        }
        else
        {
            timeSinceLastBubble += Time.deltaTime;
        }
        

        

        
    }
}
