using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using NaughtyAttributes;
using TMPro;


/// <summary>
/// Script to control player input and oars
/// </summary>
public class OarController : MonoBehaviour
{

    #region Inspector Fields

    [SerializeField,BoxGroup("References")]
    private HandController m_leftHand;

    [SerializeField, BoxGroup("References")]
    private HandController m_rightHand;

    [SerializeField, BoxGroup("References")]
    private Rigidbody m_oarRigidbody;

    [SerializeField, BoxGroup("References")]
    private Transform m_body;

    [SerializeField]
    private BoxCollider m_boxCollider;

    [SerializeField]
    private List<Collider> m_forbiddenColliders;

    #endregion Inspector Fields

    #region Private Fields

    float m_distanceFromRightHand;
    bool undoDoneThisFrame = false;

    #endregion Private Fields

    #region Getters

    public new Transform transform => m_oarRigidbody.transform;


    #endregion Getters



    #region MonoBehaviour Functions

    private void Awake()
    {
        m_boxCollider.enabled= false;
        m_distanceFromRightHand = Vector3.Distance(m_rightHand.transform.position, transform.position);
        m_oarRigidbody = GetComponent<Rigidbody>();
    }

    /// <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 FixedUpdate()
    {
        //undoDoneThisFrame = false;
        //UpdateTransform();
    }

    #endregion MonoBehaviour Functions

    #region Class Functionality
    
    public void UpdateTransform()
    {

        Vector3 direction =(m_rightHand.transform.position - m_leftHand.transform.position).normalized;
        Vector3 forward = Vector3.Cross(direction, m_body.forward);
        Quaternion rotation = Quaternion.LookRotation(forward, direction);


        transform.rotation = rotation;
        transform.position = m_rightHand.transform.position - direction * m_distanceFromRightHand;
        //MoveOarToPosition(m_rightHand.transform.position - direction * m_distanceFromRightHand);
        m_oarRigidbody.MoveRotation(rotation);
        m_oarRigidbody.MovePosition(m_rightHand.transform.position - direction * m_distanceFromRightHand);
        //MoveOarToRotation(rotation);


    }


    private void MoveOarToPosition(Vector3 position)
    {
        m_oarRigidbody.velocity = (position - transform.position) * (1 / Time.fixedDeltaTime);
    }

    private void MoveOarToRotation(Quaternion rotation)
    {
        Quaternion difference = rotation * Quaternion.Inverse(transform.rotation); 
        m_oarRigidbody.angularVelocity = (rotation.eulerAngles * Time.fixedDeltaTime);
    }


    public bool isColliding()
    {
        Vector3 direction = (m_rightHand.transform.position - m_leftHand.transform.position).normalized;
        Vector3 forward = Vector3.Cross(direction, m_body.forward);
        Quaternion rotation = Quaternion.LookRotation(forward, direction);

        Collider[] col = Physics.OverlapBox(transform.position + m_boxCollider.center, m_boxCollider.size / 2, rotation);
        if (col.Intersect(m_forbiddenColliders).Any())
        {
 
            return true;
        }
        return false;


    }

/*
    void OnCollisionEnter(Collision collision)
    {
        
        if (m_forbiddenColliders.Contains(collision.collider) && !undoDoneThisFrame)
        {
            Debug.Log($"Forbidden Collision: {collision.collider.gameObject}");
            undoDoneThisFrame= true;
            m_leftHand.UndoLastMovement();
            m_rightHand.UndoLastMovement();
        }
        else
        {
            Debug.Log($"Ignored Collision: {collision.collider.gameObject}");
        }



    }*/

    #endregion Class Functionality

    #region Editor Functions
    /// <summary>
    /// Called when the Component is created or Reset from the Inspector
    /// </summary>
    private void Reset()
    {
        m_oarRigidbody = GetComponentInChildren<Rigidbody>();
    } 
    #endregion Editor Functions

}