using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR.InteractionSystem;

/**
 * <summary>Enables the Throwable and Interactible components once the player comes close enough.</summary>
 */
public class InteractibleEnabler : MonoBehaviour {
    public float Distance;
    [Tooltip("True after the player has picked this thing up. Used to avoid glitches related to disabled Interactables when they're in use.")]
    public bool PickedUp;

    private Transform Player;
    private Interactable Interactable;
    private Throwable Throwable;
    private float SqrDistance;

    private void Awake() {
        Player = GameObject.FindGameObjectWithTag("Player").transform;
        Throwable = GetComponent<Throwable>();
        Interactable = GetComponent<Interactable>();
        Update();
    }

    private void Update() {
        if (PickedUp) {
            Throwable.enabled = true;
            Interactable.enabled = true;
        } else {
            bool InRange = (Player.position - transform.position).magnitude < Distance;
            Throwable.enabled = InRange;
            Interactable.enabled = InRange;
        }
    }

    public void Pickup() {
        PickedUp = true;
    }
}