using UnityEngine;
using System.Collections.Generic;

namespace Northbound
{
    /// <summary>
    /// 플레이어의 장비 소켓 관리 (손, 등, 허리 등)
    /// </summary>
    public class EquipmentSocket : MonoBehaviour
    {
        [System.Serializable]
        public class Socket
        {
            public string socketName; // "RightHand", "LeftHand", "Back" 등
            public Transform socketTransform; // 실제 본 Transform
            [HideInInspector] public GameObject currentEquipment; // 현재 장착된 장비
        }

        [Header("Available Sockets")]
        public List<Socket> sockets = new List<Socket>();

        private Dictionary<string, Socket> _socketDict = new Dictionary<string, Socket>();

        private void Awake()
        {
            // 빠른 검색을 위한 딕셔너리 생성
            foreach (var socket in sockets)
            {
                if (!string.IsNullOrEmpty(socket.socketName))
                {
                    _socketDict[socket.socketName] = socket;
                }
            }
        }

        /// <summary>
        /// 소켓에 장비 부착
        /// </summary>
        public GameObject AttachToSocket(string socketName, GameObject equipmentPrefab)
        {
            if (!_socketDict.TryGetValue(socketName, out Socket socket))
            {
                Debug.LogWarning($"소켓을 찾을 수 없습니다: {socketName}");
                return null;
            }

            if (socket.socketTransform == null)
            {
                Debug.LogWarning($"소켓 Transform이 없습니다: {socketName}");
                return null;
            }

            // 기존 장비 제거
            DetachFromSocket(socketName);

            // 새 장비 생성
            if (equipmentPrefab != null)
            {
                GameObject equipment = Instantiate(equipmentPrefab, socket.socketTransform);
                equipment.transform.localPosition = Vector3.zero;
                equipment.transform.localRotation = Quaternion.identity;
                socket.currentEquipment = equipment;

                return equipment;
            }

            return null;
        }

        /// <summary>
        /// 소켓에서 장비 제거
        /// </summary>
        public void DetachFromSocket(string socketName)
        {
            if (!_socketDict.TryGetValue(socketName, out Socket socket))
                return;

            if (socket.currentEquipment != null)
            {
                Destroy(socket.currentEquipment);
                socket.currentEquipment = null;
            }
        }

        /// <summary>
        /// 모든 소켓에서 장비 제거
        /// </summary>
        public void DetachAll()
        {
            foreach (var socket in sockets)
            {
                if (socket.currentEquipment != null)
                {
                    Destroy(socket.currentEquipment);
                    socket.currentEquipment = null;
                }
            }
        }

        /// <summary>
        /// 특정 소켓에 장비가 있는지 확인
        /// </summary>
        public bool HasEquipment(string socketName)
        {
            if (_socketDict.TryGetValue(socketName, out Socket socket))
            {
                return socket.currentEquipment != null;
            }
            return false;
        }
    }
}