using UnityEngine;

namespace MaskShop
{
    /// <summary>
    /// Manages player inventory and carrying mechanics
    /// </summary>
    [RequireComponent(typeof(CharacterController))]
    public class PlayerInventory : MonoBehaviour
    {
        [Header("Carrying Settings")]
        [SerializeField] private Transform handsTransform;
        [SerializeField, Range(0f, 1f)] private float speedReductionPercent = 0.3f;

        [Header("Debug")]
        [SerializeField] private bool showDebugInfo = true;

        // Components
        private CharacterController characterController;
        private PlayerMovement playerMovement; // Reference to your existing movement script

        // State
        private GameObject currentMold;
        private MaskMold currentMoldData;
        private float originalSpeed;
        private bool isCarrying = false;

        // Events
        public static event System.Action<MaskMold> OnMoldPickedUp;
        public static event System.Action OnMoldDropped;

        private void Awake()
        {
            characterController = GetComponent<CharacterController>();
            playerMovement = GetComponent<PlayerMovement>();

            if (handsTransform == null)
            {
                Debug.LogWarning("PlayerInventory: Hands transform not assigned! Creating one...");
                GameObject hands = new GameObject("Hands");
                hands.transform.SetParent(transform);
                hands.transform.localPosition = new Vector3(0, 1f, 0.5f);
                handsTransform = hands.transform;
            }
        }

        /// <summary>
        /// Picks up a mold and attaches it to player's hands
        /// </summary>
        public void PickupMold(GameObject moldInstance, MaskMold moldData)
        {
            if (isCarrying)
            {
                Debug.LogWarning("PlayerInventory: Already carrying a mold!");
                return;
            }

            currentMold = moldInstance;
            currentMoldData = moldData;

            // Attach to hands
            currentMold.transform.SetParent(handsTransform);
            currentMold.transform.localPosition = Vector3.zero;
            currentMold.transform.localRotation = Quaternion.identity;

            // Reduce player speed
            ApplySpeedReduction();

            isCarrying = true;

            // Fire event
            OnMoldPickedUp?.Invoke(moldData);

            if (showDebugInfo)
            {
                Debug.Log($"PlayerInventory: Picked up {moldData.MoldName}");
            }
        }

        /// <summary>
        /// Drops the current mold at the workbench
        /// </summary>
        public GameObject DropMoldAtWorkbench()
        {
            if (!isCarrying)
            {
                Debug.LogWarning("PlayerInventory: Not carrying any mold!");
                return null;
            }

            // Detach from hands
            currentMold.transform.SetParent(null);

            // Restore player speed
            RestoreSpeed();

            GameObject droppedMold = currentMold;
            MaskMold droppedData = currentMoldData;

            currentMold = null;
            //currentMoldData = null;
            isCarrying = false;

            // Fire event
            OnMoldDropped?.Invoke();

            if (showDebugInfo)
            {
                Debug.Log($"PlayerInventory: Dropped {droppedData.MoldName} at workbench");
            }

            return droppedMold;
        }

        /// <summary>
        /// Applies speed reduction when carrying
        /// </summary>
        private void ApplySpeedReduction()
        {
            if (playerMovement != null)
            {
                // Store original speed
                originalSpeed = playerMovement.moveSpeed;

                // Apply reduction
                float reducedSpeed = originalSpeed * (1f - speedReductionPercent);
                playerMovement.moveSpeed = reducedSpeed;

                if (showDebugInfo)
                {
                    Debug.Log($"PlayerInventory: Speed reduced from {originalSpeed} to {reducedSpeed}");
                }
            }
        }

        /// <summary>
        /// Restores original speed
        /// </summary>
        private void RestoreSpeed()
        {
            if (playerMovement != null && originalSpeed > 0)
            {
                playerMovement.moveSpeed = originalSpeed;

                if (showDebugInfo)
                {
                    Debug.Log($"PlayerInventory: Speed restored to {originalSpeed}");
                }
            }
        }

        // Public getters
        public bool IsCarrying() => isCarrying;
        public MaskMold GetCurrentMoldData() => currentMoldData;
        public GameObject GetCurrentMold() => currentMold;
    }
}