using UnityEngine;

namespace Quest_Environment.Q4
{
    [RequireComponent(typeof(Collider))]
    public class FurniturePlacementZone : MonoBehaviour, IInteractable
    {
        [Tooltip("The ID of the furniture that belongs in this zone.")]
        [SerializeField] private string targetFurnitureId;
        [Tooltip("The furniture object to show when placed.")]
        [SerializeField] private GameObject placedFurniturePrefab;
        [Tooltip("The GameObject with an Outline component that acts as a visual indicator.")]
        [SerializeField] private GameObject zoneIndicator;

        private bool _isOccupied = false;
        private Outline _indicatorOutline; // *** НОВОЕ ПОЛЕ для хранения компонента

        void Start()
        {
            // *** ИЗМЕНЕНИЕ: Находим и кэшируем компонент Outline
            if (zoneIndicator != null)
            {
                _indicatorOutline = zoneIndicator.GetComponent<Outline>();
            }
            ResetZone();
        }

        public void Interact()
        {
            if (QuestManager.Instance?.currentQuest is Q4_PlaceFurnitureQuest furnitureQuest)
            {
                if (!_isOccupied && furnitureQuest.HeldFurnitureId == targetFurnitureId)
                {
                    InteractionHandler.Instance.NotifyInteraction(gameObject);
                }
            }
        }

        public string GetDescription()
        {
            if (QuestManager.Instance?.currentQuest is Q4_PlaceFurnitureQuest furnitureQuest)
            {
                if (!_isOccupied && furnitureQuest.HeldFurnitureId == targetFurnitureId)
                {
                    return $"Поставить {targetFurnitureId}";
                }
            }
            return "";
        }

        public void PlaceItem()
        {
            _isOccupied = true;
            if (placedFurniturePrefab != null) placedFurniturePrefab.SetActive(true);
            
            // *** ИЗМЕНЕНИЕ: Выключаем Outline через компонент
            if (_indicatorOutline != null)
            {
                _indicatorOutline.enabled = false;
            }
        }

        public void ResetZone()
        {
            _isOccupied = false;
            if (placedFurniturePrefab != null) placedFurniturePrefab.SetActive(false);
            
            // *** ИЗМЕНЕНИЕ: Выключаем Outline через компонент
            if (_indicatorOutline != null)
            {
                _indicatorOutline.enabled = false;
            }
        }

        void Update()
        {
            // Если у нас нет компонента подсветки или зона уже занята, ничего не делаем
            if (_indicatorOutline == null || _isOccupied)
            {
                return;
            }

            bool shouldBeActive = false;
            if (QuestManager.Instance?.currentQuest is Q4_PlaceFurnitureQuest furnitureQuest)
            {
                if (furnitureQuest.HeldFurnitureId == targetFurnitureId)
                {
                    shouldBeActive = true;
                }
            }

            // *** ИЗМЕНЕНИЕ: Включаем/выключаем сам компонент, а не весь GameObject
            if (_indicatorOutline.enabled != shouldBeActive)
            {
                _indicatorOutline.enabled = shouldBeActive;
            }
        }
    }
}
