using Sirenix.Utilities;
using UnityEngine;
using VOID.Generic.Triggers;

namespace VOID.Generic.Player
{
    [DefaultExecutionOrder(-1)]
    public class CameraTarget : MonoBehaviour
    {
        public static CameraTarget InstantiateNew()
        {
            var gameObject = new GameObject("Camera Target");
            gameObject.layer = LayerManager.CameraIndex;
            gameObject.transform.parent = CameraManager.current.transform;

            // Collision
            var sphereCollider = gameObject.AddComponent<SphereCollider>();
            sphereCollider.isTrigger = true;
            
            // RigidBody
            var rigidBody = gameObject.AddComponent<Rigidbody>();
            rigidBody.useGravity = false;
            rigidBody.isKinematic = true;
            rigidBody.constraints = RigidbodyConstraints.FreezeAll;

            return gameObject.AddComponent<CameraTarget>();
        }

        private void OnTriggerEnter(Collider other)
        {
            other.GetComponents<ICameraTrigger>().ForEach(trigger => trigger.OnCameraEnter());
        }

        private void OnTriggerExit(Collider other)
        {
            other.GetComponents<ICameraTrigger>().ForEach(trigger => trigger.OnCameraExit());
        }
    }
}