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

public class WeaponGrab : MonoBehaviour
{
    public KeyCode grabKey = KeyCode.E;
    public float grabRadius = 1f;
    public Transform lowerArm;
    public LayerMask weaponLayer;
    public Vector3 weaponOffsetRotation = Vector3.zero; // Adjust this offset rotation as needed
    public Vector3 weaponOffsetPosition = Vector3.zero; // Adjust this offset position as needed

    private GameObject currentHeldWeapon;

    void Update()
    {
        if (Input.GetKeyDown(grabKey))
        {
            if (currentHeldWeapon != null)
            {
                ReleaseWeapon();
            }
            else
            {
                TryGrabWeapon();
            }
        }
    }

    private void TryGrabWeapon()
    {
        Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, grabRadius, weaponLayer);

        foreach (Collider2D col in colliders)
        {
            if (col.CompareTag("Weapon"))
            {
                currentHeldWeapon = col.gameObject;

                // Make the weapon a child of the lower arm
                currentHeldWeapon.transform.parent = lowerArm;

                // Set the position and rotation as the lower arm with the offsets
                currentHeldWeapon.transform.localPosition = weaponOffsetPosition;
                currentHeldWeapon.transform.localRotation = Quaternion.Euler(weaponOffsetRotation);

                // Disable the weapon's Rigidbody to prevent physics interactions while held
                Rigidbody2D rb = currentHeldWeapon.GetComponent<Rigidbody2D>();
                if (rb != null)
                {
                    rb.isKinematic = true;

                    // Add a FixedJoint2D component to the weapon
                    FixedJoint2D fixedJoint = currentHeldWeapon.AddComponent<FixedJoint2D>();
                    // Connect the joint to the lower arm
                    fixedJoint.connectedBody = lowerArm.GetComponent<Rigidbody2D>();
                    // Set the anchor and connectedAnchor positions to match the current position of the weapon in the lower arm's local space
                    fixedJoint.anchor = weaponOffsetPosition;
                    fixedJoint.connectedAnchor = Vector2.zero;

                    // Set kinematic to false immediately after adding the FixedJoint2D
                    rb.isKinematic = false;
                }

                break;
            }
        }
    }

    private void ReleaseWeapon()
    {
        if (currentHeldWeapon != null)
        {
            // Remove the weapon from the lower arm's children
            currentHeldWeapon.transform.parent = null;

            // Remove the FixedJoint2D component from the weapon
            FixedJoint2D joint = currentHeldWeapon.GetComponent<FixedJoint2D>();
            if (joint != null)
            {
                Destroy(joint);
            }

            currentHeldWeapon = null;
        }
    }

    private void OnDrawGizmosSelected()
    {
        // Display the grab radius in the Unity Editor for visualization
        Gizmos.color = Color.yellow;
        Gizmos.DrawWireSphere(transform.position, grabRadius);
    }
}