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

[RequireComponent(typeof(Rigidbody2D))]
public class GrabbableObject : MonoBehaviour, IGrabbable
{
    public bool CanBeGrabbed { get; private set; } = true;

    private FixedJoint2D m_Joint;
    private Rigidbody2D m_RigidbodyComponent;

    private void Awake()
    {
        m_Joint = GetComponent<FixedJoint2D>();
        m_RigidbodyComponent = GetComponent<Rigidbody2D>();
    }

    public virtual void OnGrab(IGrabber grabber)
    {
        CanBeGrabbed = true;
        m_Joint.connectedBody = grabber.RigidbodyComponent;
        m_Joint.enabled = true;
        m_RigidbodyComponent.isKinematic = false;
    }

    public virtual void OnLeave(IGrabber grabber)
    {
        CanBeGrabbed = true;
        m_Joint.enabled = false;
        m_RigidbodyComponent.isKinematic = true;
        m_RigidbodyComponent.velocity = Vector2.zero;
    }
}
    
