using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;

public class HitDetectorScript : MonoBehaviour
{
    private bool _IsDead = false;

    [SerializeField] private GameObject _Menu;
    [SerializeField] private GameObject _ForwardSource;
    [SerializeField] private ActionBasedContinuousMoveProvider move;

    [Range(0f, 1f)]
    [SerializeField] private float _Intensity;
    [SerializeField] private float _Duration;

    [SerializeField] XRBaseController _LeftController;
    [SerializeField] XRBaseController _RightController;

    private void OnTriggerEnter(Collider other)
    {
        //Debug.Log(other.gameObject.name);
        if (other.gameObject.CompareTag("Car") && !_IsDead)
        {
            _IsDead = true;

            Vector3 pos = _ForwardSource.transform.position;
            Vector3 forward = _ForwardSource.transform.forward;

            // Определите расстояние перед объектом
            float distance = 1.0f;

            // Вычислите новую позицию с учетом расстояния
            Vector3 newPos = pos + forward.normalized * distance;

            // Меню смотрит в том же направлении, что и источник, но фиксируется по вертикали
            Quaternion newRot = Quaternion.LookRotation(new Vector3(forward.x, 0, forward.z));

            move.enabled = false;

            // Создание объекта
            Instantiate(_Menu, newPos, newRot);

            TriggerHaptic(_LeftController);
            TriggerHaptic (_RightController);

            Debug.Log("Player was hitted car");
            //SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().name);
        }
    }

    public void TriggerHaptic(XRBaseController controller)
    {
        if (_Intensity > 0)
        {
            controller.SendHapticImpulse(_Intensity, _Duration);
        }
    }
    //private void OnCollisionEnter(Collision collision)
    //{
    //    Debug.Log(collision.gameObject.name);
    //}

    //private void OnControllerColliderHit(ControllerColliderHit hit)
    //{
    //    Debug.Log(hit.gameObject.name);
    //    if (hit.gameObject.CompareTag("Car"))
    //    {
    //        Debug.Log("Car hitted");
    //    }
    //}
}
