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

public class CharacterControl : MonoBehaviour
{
    [SerializeField]
    Transform playerCamera;

    [SerializeField]
    float seeDistance;
    Interactable curretInteractable;
    Pickable currentPickable;
    Gun CurrentGun=null;
    public RaycastHit hitInfo;
    public static bool handsFull;
  
    [SerializeField]
    private GameObject hand;

    void Update()
    {
        bool Raycast = Physics.Raycast(
            playerCamera.position,
            playerCamera.TransformDirection(Vector3.forward),
            out hitInfo,
            seeDistance
        );
        Debug.DrawRay(
            playerCamera.position,
            playerCamera.TransformDirection(Vector3.forward) * seeDistance,
            Color.green
        );
        if (Raycast)
        {
            Debug.Log(hitInfo.transform.name);
            if (hitInfo.transform.TryGetComponent<Interactable>(out curretInteractable)) { }
            else if (hitInfo.transform.TryGetComponent<Pickable>(out currentPickable))
            {
                if (hitInfo.transform.TryGetComponent<Gun>(out CurrentGun)) { }
            }
            else
            {
                curretInteractable = null;
                currentPickable = null;
            }
        }
        else
        {
            curretInteractable = null;
            currentPickable = null;
        }
    }

    void OnInteract()
    {
        if (curretInteractable != null)
        {
            curretInteractable.Interact();
        }
        if (currentPickable != null)
        {
            currentPickable.PickUp();
        }
    }

    void OnShoot()
    {
        Debug.Log("Shoot");
        if (CurrentGun != null)
        {
            
            CurrentGun.Shoot();
        }
    }

    void OnReload()
    {
        if (CurrentGun != null)
        {
            CurrentGun.Reload();
        }
    }

    void OnDrop()
    {
        CurrentGun.transform.GetComponent<Pickable>().Drop();
    }
}
