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


namespace GuidewireSim
{
    public class CollisionDetectionPrimitive : MonoBehaviour
    {
        SimulationLoop simulationLoop; //!< The SimulationLoop component in the Simulation GameObject
        CollisionHandler collisionHandler; //!< The CollisionHandler component in the Simulation GameObject

        public int sphereID; /**< The unique ID of the sphere that this component is attached to. Matches the position in @p spheres in #SimulationLoop.
                              *   @note Should also match the position in @p spherePositions, @p sphereVelocities, @p sphereExternalForces,
                              *   @p spherePositionPredictions in #SimulationLoop.
                              */

        private void Awake()
        {
            simulationLoop = FindObjectOfType<SimulationLoop>();
            Assert.IsNotNull(simulationLoop);

            collisionHandler = FindObjectOfType<CollisionHandler>();
            Assert.IsNotNull(collisionHandler);
        }

        private void Start()
        {
            AssignSphereID();
        }

        private void AssignSphereID()
        {
            GameObject thisSphere = this.transform.gameObject;

            for (int sphereIndex = 0; sphereIndex < simulationLoop.SpheresCount; sphereIndex++)
            {
                if (thisSphere == simulationLoop.spheres[sphereIndex])
                {
                    sphereID = sphereIndex;
                    return;
                }
            }

            Debug.LogWarning("No sphereID could be assigned.");
        }

        private void OnCollisionEnter(Collision other)
        {
            Debug.Log("trigger " + transform.name);

            collisionHandler.RegisterCollision(this.transform, other, sphereID);
        }

        private void OnCollisionStay(Collision other)
        {
            Debug.Log("trigger " + transform.name);

            collisionHandler.RegisterCollision(this.transform, other, sphereID);
        }










        // private void OnTriggerEnter(Collider other)
        // {
        //     Debug.Log("trigger " + transform.name);
        //     if (!insideCollision)
        //     {

        //         collisionHandler.RegisterCollision(this.transform, other, sphereID);
        //         Debug.Log("pos" + this.transform.position);
        //         Debug.Log("colpos" + this.transform.GetComponent<SphereCollider>().center);
        //         Debug.Log("otherpos" + other.transform.position);
        //         Vector3 otherClosestPoint = other.ClosestPoint(this.transform.position);
        //         Debug.Log("otherclosestpoint" + otherClosestPoint.ToString("e4"));
        //         Debug.Log("thisclosestpoint" + this.transform.GetComponent<SphereCollider>().ClosestPoint(otherClosestPoint).ToString("e4"));
        //         Vector3 thisclosestpoint2 = this.transform.GetComponent<SphereCollider>().ClosestPoint(other.transform.position);
        //         Debug.Log("thisclosestpoint2" + thisclosestpoint2.ToString("e4"));
        //         Vector3 DiffCollisionPointToCenter = thisclosestpoint2 - (this.transform.position + this.transform.GetComponent<SphereCollider>().center);
        //         Debug.Log("DiffCollisionPointToCenter" + DiffCollisionPointToCenter.ToString("e4"));
        //     }
        // }

        // private void OnTriggerStay(Collider other)
        // {
        //     Debug.Log("trigger " + transform.name);
        //     if (!insideCollision)
        //     {

        //         collisionHandler.RegisterCollision(this.transform, other, sphereID);
        //     }
        // }

        // private void OnTriggerExit(Collider other)
        // {
        //     if (insideCollision)
        //     {
        //         Debug.Log("trigger " + transform.name);

        //         collisionHandler.RegisterCollision(this.transform, other, sphereID);                
        //     }
        // }

        // private void OnCollisionEnter(Collision other)
        // {
        //     Debug.Log("trigger " + transform.name);
        //     // needed to use ClosestPoint().
        //     // other.collider.GetComponent<MeshCollider>().convex = true;

        //     collisionHandler.RegisterCollision(this.transform, other, sphereID);
        //     // Debug.Log(other.GetContact(0).normal.ToString("e2"));


        //     // if (!insideCollision)
        //     // {
        //     //     collisionHandler.RegisterCollision(this.transform, other, sphereID);
        //     //     Debug.Log("pos" + this.transform.position);
        //     //     Debug.Log("colpos" + this.transform.GetComponent<SphereCollider>().center);
        //     //     Debug.Log("otherpos" + other.transform.position);

        //     //     other.collider.GetComponent<MeshCollider>().convex = true;
        //     //     Vector3 otherClosestPoint = other.collider.ClosestPoint(this.transform.position);

        //     //     Debug.Log("otherclosestpoint" + otherClosestPoint.ToString("e4"));
        //     //     Debug.Log("thisclosestpoint" + this.transform.GetComponent<SphereCollider>().ClosestPoint(otherClosestPoint).ToString("e4"));
        //     //     Vector3 thisclosestpoint2 = this.transform.GetComponent<SphereCollider>().ClosestPoint(other.transform.position);
        //     //     Debug.Log("thisclosestpoint2" + thisclosestpoint2.ToString("e4"));
        //     //     Vector3 DiffCollisionPointToCenter = thisclosestpoint2 - (this.transform.position + this.transform.GetComponent<SphereCollider>().center);
        //     //     Debug.Log("DiffCollisionPointToCenter" + DiffCollisionPointToCenter.ToString("e4"));
        //     // }

        // }
    }
}