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

public class CustomHorizCollider : MonoBehaviour
{

    Vector3[] debugContactPoints;
    public Movement playerMovement;

    public Vector3 horizResolveVector;
    public Transform playerTransform;
    public float floorHeight;

    public float horizCollisionThreshold;

    private void OnCollisionEnter(Collision collision)
    {
        // horizontal collision resolving:
        //Debug.Log("collision entered");

        ContactPoint[] contactPoints = new ContactPoint[collision.contactCount];
        collision.GetContacts(contactPoints);

        debugContactPoints = new Vector3[contactPoints.Length];
        for (int i = 0; i < contactPoints.Length; ++i)
        {
            debugContactPoints[i] = contactPoints[i].point;
        }


        Vector3 averageContactPoint = Vector3.zero;
        Vector3 resolveVector = Vector3.zero;
        if (contactPoints.Length > 0)
        {
            for (int i = 0; i < contactPoints.Length; ++i)
            {
                averageContactPoint += contactPoints[i].point;
            }
            averageContactPoint /= contactPoints.Length;

            resolveVector = averageContactPoint - transform.position;
        }
        resolveVector.y = 0;

        horizResolveVector = resolveVector;

        //Debug.Log("horizResolveVector magnitude: " + horizResolveVector.magnitude);

    }

    private void OnCollisionStay(Collision collision)
    {
        // horizontal collision resolving:
        //Debug.Log("collision entered");

        ContactPoint[] contactPoints = new ContactPoint[collision.contactCount];
        collision.GetContacts(contactPoints);

        debugContactPoints = new Vector3[contactPoints.Length];
        for (int i = 0; i < contactPoints.Length; ++i)
        {
            debugContactPoints[i] = contactPoints[i].point;
        }


        Vector3 averageContactPoint = Vector3.zero;
        Vector3 resolveVector = Vector3.zero;
        if (contactPoints.Length > 0)
        {
            for (int i = 0; i < contactPoints.Length; ++i)
            {
                averageContactPoint += contactPoints[i].point;
            }
            averageContactPoint /= contactPoints.Length;

            resolveVector = averageContactPoint - transform.position;
        }
        resolveVector.y = 0;

        horizResolveVector = resolveVector;

        //Debug.Log("horizResolveVector magnitude: " + horizResolveVector.magnitude);

    }


    private void OnCollisionExit(Collision collision)
    {
        debugContactPoints = null;
        horizResolveVector = Vector3.zero;
    }


    private void OnDrawGizmos()
    {
        if (debugContactPoints == null || debugContactPoints.Length == 0)
            return;

        Gizmos.color = Color.blue;
        foreach (Vector3 point in debugContactPoints)
        {
            Gizmos.DrawSphere(point, 0.05f);
        }

        Gizmos.color = Color.red;
        Gizmos.DrawLine(playerTransform.position, playerTransform.position + horizResolveVector);
        Gizmos.color = Color.yellow;
        Gizmos.DrawLine(playerTransform.position, playerTransform.position + playerMovement.velocity);
   
    }
    

    public float extrapolateDistance;

    void Update()
    {
        //transform.position = playerTransform.position + playerMovement.velocity.normalized * extrapolateDistance;
    }
}
