using UnityEngine;
using Player;

namespace Quest_Environment.Q3
{
    public class Q3_WipeSurfacesQuest : MonoBehaviour, IQuest
    {
        [Header("Quest Settings")]
        [SerializeField] private string questName = "Surface Wiping";
        [SerializeField] private int surfacesToWipeTotal = 3;
        [SerializeField] private ItemData ragItem;

        [Header("Target Tags")]
        [SerializeField] private string ragGiverTag = "RagLocation";
        [SerializeField] private string surfaceTag = "WipeableSurface";

        [Header("Audio Settings")]
        [SerializeField] private AudioClip pickupRagSound;
        [SerializeField] private AudioClip wipeSurfaceSound;

        private PlayerController _playerController;

        private enum Stage
        {
            NotStarted,
            FindRag,
            WipeSurfaces,
            Completed
        }

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

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

        public event System.Action<string> OnQuestStepChanged;

        public void StartQuest()
        {
            if (ragItem == null) Debug.LogError($"[Q3] Rag ItemData is missing!");

            currentStage = Stage.FindRag;
            wipedSurfacesCount = 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.FindRag:
                    if (obj.CompareTag(ragGiverTag))
                    {
                        currentStage = Stage.WipeSurfaces;
                        _playerController?.PlayOneShotSound(pickupRagSound);
                        PlayerHandsController.Instance?.ShowRag(true);
                        UpdateQuestState();
                    }
                    break;

                case Stage.WipeSurfaces:
                    if (obj.CompareTag(surfaceTag))
                    {
                        wipedSurfacesCount++;
                        _playerController?.PlayOneShotSound(wipeSurfaceSound);
                        QuestManager.Instance.StepCompleted();

                        if (wipedSurfacesCount >= surfacesToWipeTotal)
                        {
                            FinishQuest();
                        }
                        else
                        {
                            UpdateQuestState();
                        }
                    }
                    break;
            }
        }

        private void UpdateQuestState()
        {
            switch (currentStage)
            {
                case Stage.FindRag:
                    Description = "Find a rag.";
                    break;
                case Stage.WipeSurfaces:
                    Description = "Wipe the dirty surfaces.";
                    break;
                case Stage.Completed:
                    Description = "Everything is clean!";
                    break;
            }
            OnQuestStepChanged?.Invoke(Description);
        }

        private void FinishQuest()
        {
            currentStage = Stage.Completed;

            // Hide the rag and remove it from inventory
            PlayerHandsController.Instance?.ShowRag(false);
            if (ragItem != null)
            {
                InventorySystem.Instance.RemoveItem(ragItem);
            }

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

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