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

namespace Quest_Environment.Q4
{
    public class Q4_PlaceFurnitureQuest : MonoBehaviour, IQuest
    {
        [System.Serializable]
        public class FurnitureTask
        {
            public string furnitureId;
            [Tooltip("The object that can be picked up.")]
            public GameObject pickupObject;
            [Tooltip("The zone where the object should be placed.")]
            public FurniturePlacementZone placementZone;
            [HideInInspector] public bool isPlaced;
        }

        [Header("Quest Settings")]
        [SerializeField] private string questName = "Tidy Up the Room";
        [SerializeField] private List<FurnitureTask> furnitureTasks;

        private string _heldFurnitureId = null;
        private int _placedCount = 0;

        public string QuestName => questName;
        public string Description { get; private set; }
        public int CurrentStep => _placedCount;
        public int TotalSteps => furnitureTasks.Count;
        public bool IsCompleted => _placedCount >= TotalSteps;

        public string HeldFurnitureId => _heldFurnitureId;

        public event System.Action<string> OnQuestStepChanged;

        public void StartQuest()
        {
            _placedCount = 0;
            _heldFurnitureId = null;
            foreach (var task in furnitureTasks)
            {
                task.isPlaced = false;
                if (task.placementZone != null) task.placementZone.ResetZone();
                if (task.pickupObject != null) task.pickupObject.SetActive(true);
            }

            PlayerHandsController.Instance?.HideHands();
            if (InteractionHandler.Instance != null)
            {
                InteractionHandler.Instance.onObjectInteracted += HandleInteraction;
            }
            UpdateQuestState();
        }

        void OnDestroy()
        {
            if (InteractionHandler.Instance != null)
            {
                InteractionHandler.Instance.onObjectInteracted -= HandleInteraction;
            }
        }

        private void HandleInteraction(GameObject interactedObject)
        {
            if (IsCompleted) return;

            if (_heldFurnitureId == null)
            {
                var task = furnitureTasks.FirstOrDefault(t => !t.isPlaced && t.pickupObject == interactedObject);
                if (task != null)
                {
                    _heldFurnitureId = task.furnitureId;
                    task.pickupObject.SetActive(false);
                    UpdateQuestState();
                    return;
                }
            }
            else
            {
                var task = furnitureTasks.FirstOrDefault(t => t.furnitureId == _heldFurnitureId && t.placementZone.gameObject == interactedObject);
                if (task != null)
                {
                    task.isPlaced = true;
                    task.placementZone.PlaceItem();
                    _heldFurnitureId = null;
                    _placedCount++;
                    QuestManager.Instance.StepCompleted();

                    if (IsCompleted)
                    {
                        FinishQuest();
                    }
                    else
                    {
                        UpdateQuestState();
                    }
                    return;
                }
            }
        }

        private void UpdateQuestState()
        {
            if (IsCompleted)
            {
                Description = "Everything is in its place!";
            }
            else if (_heldFurnitureId != null)
            {
                Description = $"Place the {_heldFurnitureId}.";
            }
            else
            {
                Description = $"Arrange the furniture: {_placedCount} / {TotalSteps}";
            }
            OnQuestStepChanged?.Invoke(Description);
        }

        private void FinishQuest()
        {
            UpdateQuestState();
            QuestManager.Instance.CompleteQuest();
        }

        public void AdvanceQuestStep() { }

        public void CompleteQuest()
        {
            if (InteractionHandler.Instance != null)
            {
                InteractionHandler.Instance.onObjectInteracted -= HandleInteraction;
            }
        }
    }
}
