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

namespace GuidewireSim
{
    public class CollisionHandler : MonoBehaviour
    {
        public List<CollisionPair> registeredCollisions; //!< All collisions that occured between the last and the current frame in OnTriggerEnter.

        public SphereCollider[] sphereColliders; /**< Each element stores a reference to the SpherCollider of the respective element in @p spheres
                                                      *   in SimulationLoop.
                                                      *   @exampletext The second element in this list is the SphereCollider corresponding to the
                                                      *   sphere GameObject that is referenced in the second element of @p spheres in SimulationLoop.
                                                      */

        float sphereRadius = 5f; //!< The radius of the sphere elements of the guidewire.

        private void Start()
        {
            registeredCollisions = new List<CollisionPair>();    
        }

        public void RegisterCollision(Transform sphere, Collision vesselCollider, int sphereID)
        {
            CollisionPair registeredCollision = new CollisionPair(sphere, vesselCollider, sphereID);
            registeredCollisions.Add(registeredCollision);
        }

        public void ResetRegisteredCollisions()
        {
            registeredCollisions.Clear();
        }

        public void SetCollidersToPredictions(int spheresCount, Vector3[] spherePositionPredictions, Vector3[] spherePositions)
        {
            for (int sphereIndex = 0; sphereIndex < spheresCount; sphereIndex++)
            {
                // Siehe Blatt 10 für die Berechnung von .center
                Vector3 centerPosition = (spherePositionPredictions[sphereIndex] - spherePositions[sphereIndex]) / (2 * sphereRadius);
                sphereColliders[sphereIndex].center = centerPosition;
            }
        }
    }
}