using _PROJECT.NewHandPresence;
using System.Collections;
using System.Collections.Generic;
using Unity.XR.CoreUtils;
using UnityEngine;
using UnityEngine.XR.Content.Interaction;
using UnityEngine.XR.Interaction.Toolkit;

public class PassangerSeat : LocomotionProvider
{
    // Start is called before the first frame update
    public GameObject ExitSpot;
    public CarTeleportButton ExitButton;
    private XROrigin currentPassanger;
    private LocomotionSystem currentPlayerLocomotion;
    private bool wasSmoothLocomotionEnabled;
    private ContinuousMoveProviderBase moveProvider;

    void Start()
    {
    }
    public void activateButtonPush(XROrigin player, CarTeleportButton button)
    {
        if (button == ExitButton) ExitCar();
        else EnterCar(player);
    }
    public void EnterCar(XROrigin player)
    {
        if (player == null || currentPassanger != null) return;

        TutorialController cameraChild = player.GetComponentInChildren<TutorialController>();
        //TunnelingVignetteController vignetteController = player.GetComponentInChildren<TunnelingVignetteController>();
        //LocomotionVignetteProvider vignetteProvider = player.GetComponentInChildren<LocomotionVignetteProvider>();
        if (cameraChild == null) return;

        Transform cameraTransform = cameraChild.transform.parent.transform;

        Vector3 cameraShift = cameraTransform.localPosition;

        currentPassanger = player;
        player.transform.SetParent(this.transform);
        player.transform.localPosition = -cameraShift;
        player.transform.localRotation = Quaternion.identity;
        cameraTransform.localRotation = this.transform.rotation;
        BeginLocomotion();
        disablePlayerLocomotion(player);
    }
    private void disablePlayerLocomotion(XROrigin player)
    {
        Menu menu = player.GetComponentInChildren<Menu>();
        if (menu != null) menu.DeactivateMenu();

        currentPlayerLocomotion = player.GetComponentInChildren<LocomotionSystem>();
        moveProvider = player.GetComponentInChildren<ContinuousMoveProviderBase>();

        if (moveProvider != null)
            wasSmoothLocomotionEnabled = moveProvider.enabled;

        if (currentPlayerLocomotion != null)
        {
            Debug.Log("LocomotionSystem of: " + currentPlayerLocomotion + " disabled");
            currentPlayerLocomotion.gameObject.SetActive(false);
        }
    }


    public void ExitCar()
    {
        if (currentPassanger == null) return;
        EndLocomotion();
        enablePlayerLocomotion(currentPassanger);

        TutorialController cameraChild = currentPassanger.GetComponentInChildren<TutorialController>();
        if (cameraChild == null) return;

        Transform cameraTransform = cameraChild.transform.parent.transform;

        // Set the player�s parent to null (making it part of the scene hierarchy)
        currentPassanger.transform.SetParent(null);

        // Put the player outside;
        currentPassanger.transform.localPosition = ExitSpot.transform.position;
        cameraTransform.rotation = ExitSpot.transform.rotation; 
        currentPassanger = null;
    }

    private void enablePlayerLocomotion(XROrigin player)
    {
        Menu menu = player.GetComponentInChildren<Menu>();
        if (menu != null) menu.ReactivateMenu();

        if (currentPlayerLocomotion != null)
            currentPlayerLocomotion.gameObject.SetActive(true);

        if (moveProvider != null)
            moveProvider.enabled = wasSmoothLocomotionEnabled;
            moveProvider = null;
    }


    // Update is called once per frame
    void Update()
    {

    }
}