using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NaughtyAttributes;
using Unity.VisualScripting;
using UnityEngine.Events;

/// <summary>
/// 
/// </summary>
public class BoatRowController : MonoBehaviour
{

    #region Inspector Fields
    public Transform m_oarTip;
    public UnityEvent<float> m_OnRow;
    public UnityEvent m_OnBrake;
    public int velocityOverFrames = 8;
    #endregion Inspector Fields

    #region Private Fields
    private Vector3 m_lastKnownOarPosition;

    private Queue<float> m_velocityQueue = new Queue<float>();
    #endregion Private Fields

	#region Getters
	
    #endregion Getters



    #region MonoBehaviour Functions
    /// <summary>
	/// OnEnable is called when the object becomes enabled and active.
    /// </summary>
    private void OnEnable()
    {
    
    }

    /// <summary>
	/// OnDisable is called when the behaviour becomes disabled.
    /// </summary>
    private void OnDisable()
    {
    
    }

    /// <summary>
    /// Update is called once per frame
    /// </summary>
    private void Update()
    {
    
    }

    #endregion MonoBehaviour Functions

    #region Class Functionality

    private void OnTriggerEnter(Collider other)
    {
        OarController oar = other.GetComponentInParent<OarController>();
        if (oar != null)
        {
            m_lastKnownOarPosition = transform.InverseTransformPoint(m_oarTip.position);
            m_velocityQueue = new Queue<float>();
        }
    }

    void OnTriggerStay(Collider other)
    {
        OarController oar = other.GetComponentInParent<OarController>();
        if(oar != null)
        {
            Vector3 localOarTipPosition = transform.InverseTransformPoint(m_oarTip.position);

            Vector3 direction = m_lastKnownOarPosition - localOarTipPosition;

            float directionality = Vector3.Dot(direction,Vector3.forward);

            m_velocityQueue.Enqueue(directionality * Time.deltaTime);

            if(m_velocityQueue.Count > velocityOverFrames)
            {
                m_velocityQueue.Dequeue();
            }

            float averageVelocity = 0;
            if (m_velocityQueue.Count > 0)
            {
                var v_arr = m_velocityQueue.ToArray();
                
                for(int i = 0; i < v_arr.Length; i++)
                {
                    averageVelocity += v_arr[i];
                }
                averageVelocity /= v_arr.Length;
            }

            if (averageVelocity > 0)
            {
                if(averageVelocity < 0.000001)
                {
                    m_OnBrake.Invoke();
                    //Debug.Log($"Brake! ({averageVelocity})");
                }
                else
                {
                    //Debug.Log($"Row! ({averageVelocity})");
                    m_OnRow.Invoke(averageVelocity);
                }
            }
            else
            {
                //Debug.Log($"No Row: {averageVelocity}");
                m_OnBrake.Invoke();
            }
            m_lastKnownOarPosition = localOarTipPosition;
        }
        else
        {
            Debug.Log($"Not Oar: {other.gameObject}");
        }
        
    }



    #endregion Class Functionality

    #region Editor Functions
    /// <summary>
    /// Called when the Component is created or Reset from the Inspector
    /// </summary>
    private void Reset()
    {
        //useful for finding components on creation
    } 
    #endregion Editor Functions

}