using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class Pickable : Interactable
{
    [SerializeField]
    private Transform HandPosition;

    [SerializeField]
    protected Transform player;

    [SerializeField]
    protected Transform Camera;

    [SerializeField]
    private float force;
    private Rigidbody rb;

    public virtual void Start()
    {
        rb = gameObject.GetComponent<Rigidbody>();
    }

    public virtual void Update()
    {
        if (HandPosition == null)
        {
            HandPosition = GameObject.Find("hand position").transform;
        }
        if (player == null)
        {
            player = GameObject.Find("PlayerCapsule").transform;
        }
        if (Camera == null)
        {
            Camera = GameObject.Find("MainCamera").transform;
        }
    }

    public override void Interact()
    {
        rb.isKinematic = true;
        gameObject.GetComponent<BoxCollider>().isTrigger = true;
        transform.SetParent(HandPosition);
        transform.localRotation = Quaternion.Euler(Vector3.zero);
        transform.localPosition = Vector3.zero;
        transform.localScale = Vector3.one;

        Debug.Log("Picked up");
    }

    public virtual void Drop()
    {
        rb.isKinematic = false;
        gameObject.GetComponent<BoxCollider>().isTrigger = false;
        transform.SetParent(null);
        rb.velocity = player.GetComponent<Rigidbody>().velocity;
        rb.AddForce(Camera.forward * force, ForceMode.Impulse);
        float random = Random.Range(-1f, 1f);
        rb.AddTorque(new Vector3(random, random, random) * 10);
    }
}
