using UnityEngine;
using Player;

namespace Quest_Environment.Q2
{
    public class Q2_SweepDirtQuest : MonoBehaviour, IQuest
    {
        [Header("Quest Settings")]
        [SerializeField] private string questName = "Room Cleaning";
        [SerializeField] private int dirtPilesTotal = 5;
        [SerializeField] private ItemData broomItem;

        [Header("Target Tags")]
        [SerializeField] private string broomGiverTag = "BroomLocation";
        [SerializeField] private string dirtTag = "Dirt";

        [Header("Audio Settings")]
        [SerializeField] private AudioClip pickupBroomSound;
        [SerializeField] private AudioClip cleanDirtSound;

        private PlayerController _playerController;

        private enum Stage
        {
            NotStarted,
            FindBroom,
            SweepDirt,
            Completed
        }

        private Stage currentStage = Stage.NotStarted;
        private int cleanedDirtCount = 0;

        public string QuestName => questName;
        public string Description { get; private set; }
        public int CurrentStep => cleanedDirtCount;
        public int TotalSteps => dirtPilesTotal;
        public bool IsCompleted => currentStage == Stage.Completed;

        public event System.Action<string> OnQuestStepChanged;

        public void StartQuest()
        {
            if (broomItem == null) Debug.LogError($"[Q2] Broom ItemData is missing!");

            currentStage = Stage.FindBroom;
            cleanedDirtCount = 0;
            PlayerHandsController.Instance?.HideHands();
            
            _playerController = FindObjectOfType<PlayerController>();
            
            if (InteractionHandler.Instance != null)
            {
                InteractionHandler.Instance.onObjectInteracted += HandleInteraction;
            }
            UpdateQuestState();
        }

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

        private void HandleInteraction(GameObject obj)
        {
            if (currentStage == Stage.Completed || obj == null) return;

            switch (currentStage)
            {
                case Stage.FindBroom:
                    if (obj.CompareTag(broomGiverTag))
                    {
                        currentStage = Stage.SweepDirt;
                        _playerController?.PlayOneShotSound(pickupBroomSound);
                        PlayerHandsController.Instance?.ShowBroom(true);
                        UpdateQuestState();
                    }
                    break;

                case Stage.SweepDirt:
                    if (obj.CompareTag(dirtTag))
                    {
                        cleanedDirtCount++;
                        _playerController?.PlayOneShotSound(cleanDirtSound);
                        QuestManager.Instance.StepCompleted();

                        if (cleanedDirtCount >= dirtPilesTotal)
                        {
                            FinishQuest();
                        }
                        else
                        {
                            UpdateQuestState();
                        }
                    }
                    break;
            }
        }

        private void UpdateQuestState()
        {
            switch (currentStage)
            {
                case Stage.FindBroom:
                    Description = "Find a broom.";
                    break;
                case Stage.SweepDirt:
                    Description = "Sweep the floors in the rooms.";
                    break;
                case Stage.Completed:
                    Description = "The floor is clean!";
                    break;
            }
            OnQuestStepChanged?.Invoke(Description);
        }

        private void FinishQuest()
        {
            currentStage = Stage.Completed;
            
            // Hide the broom and remove it from inventory
            PlayerHandsController.Instance?.ShowBroom(false);
            if (broomItem != null)
            {
                InventorySystem.Instance.RemoveItem(broomItem);
            }

            UpdateQuestState();
            QuestManager.Instance.CompleteQuest();
        }

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