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

public class Kick : MonoBehaviour
{

    public Vector3 kickOffset;
    public Transform cameraTransform;
    public float kickSpeed;

    public float kickThreshold;

    public BubbleManager bubbleManager;
    public CanvasAnimator canvasAnimator;

    bool waitingForKickAnimation;
    public float kickDelay;
    float timeWaitingForAnimation;

    bool chargingKick;
    public float kickChargeRate;
    public float kickCharge, minKickCharge, maxKickCharge;
    bool kickQueued;

    public Animator animator;

    public SkinnedMeshRenderer kickMeshRenderer;

    public float kickAnimationElapsedTime;
    public float kickDisappearTime;
    private void Update()
    {
        float kickInput = Input.GetKey(KeyCode.Q) ? 1 : 0;
        if (kickInput > 0 && !chargingKick && !waitingForKickAnimation)
        {
            chargingKick = true;
            kickQueued = true;

            kickCharge = minKickCharge;
        }
        else if (kickInput > 0 && chargingKick && !waitingForKickAnimation)
        {
            kickCharge += Time.deltaTime * kickChargeRate;

        }
        else if (kickInput == 0)
        {
            chargingKick = false;
            

        }

        if (kickQueued && !chargingKick)
        {
            
            kickCharge = Mathf.Clamp(kickCharge, 0, maxKickCharge);
            float chargedKickSpeed = kickCharge * kickSpeed;
            



            Vector3 kickDir = cameraTransform.right * kickOffset.x + cameraTransform.up * kickOffset.y + cameraTransform.forward * kickOffset.z;

            if (!waitingForKickAnimation)
            {
                waitingForKickAnimation = true;

                //canvasAnimator.kick();
                animator.SetTrigger("kick");
                animator.playbackTime = 0.5f;
                kickMeshRenderer.enabled = true;
                kickAnimationElapsedTime = 0;
            }
            

            if (waitingForKickAnimation)
            {
                timeWaitingForAnimation += Time.deltaTime;
            }

            if (timeWaitingForAnimation >= kickDelay)
            {
                Vector3 kickPos = cameraTransform.position + kickDir;

                Bubble[] bubblesFound = bubbleManager.bubblesAtPos(kickPos, kickThreshold);

                if (bubblesFound.Length > 0)
                {
                    foreach (Bubble bubble in bubblesFound)
                    {
                        bubble.velocity += kickDir * chargedKickSpeed;
                        Debug.Log("successful kick performed!");
                    }
                }

                
                waitingForKickAnimation = false;
                timeWaitingForAnimation = 0;
                kickQueued = false;
            }
        }


        kickAnimationElapsedTime += Time.deltaTime;
        if (kickAnimationElapsedTime >= kickDisappearTime)
        {
            
            kickMeshRenderer.enabled = false;
            
        }





    }
}
