using System.Collections;
using UnityEngine;

namespace Quest_Environment.Q3
{
    public class WipeableSurface : MonoBehaviour, IInteractable
    {
        [Header("Settings")]
        public ItemData requiredRagItem;
        [SerializeField] private float wipeDuration = 1.0f;

        [Header("Object References")]
        [Tooltip("The clean version of the surface to be activated.")]
        [SerializeField] private GameObject cleanSurfaceObject;
        [Tooltip("The dirty version of the surface to be deactivated.")]
        [SerializeField] private GameObject dirtySurfaceObject;

        private bool isWiping = false;

        private void Start()
        {
            // Ensure the clean version is hidden at the start
            if (cleanSurfaceObject != null) cleanSurfaceObject.SetActive(false);
        }

        public void Interact()
        {
            if (isWiping) return;

            bool hasRag = InventorySystem.Instance.HasItem(requiredRagItem);

            if (hasRag)
            {
                if (QuestManager.Instance != null && QuestManager.Instance.currentQuest is Q3_WipeSurfacesQuest)
                {
                    StartCoroutine(WipeRoutine());
                }
                else
                {
                    Debug.Log("This is dirty, but I have no reason to clean it right now.");
                }
            }
            else
            {
                Debug.Log("A rag is needed to clean this!");
            }
        }

        private IEnumerator WipeRoutine()
        {
            isWiping = true;

            PlayerHandsController.Instance?.PlayWipeAnimation();
            InteractionHandler.Instance.NotifyInteraction(gameObject);

            // Wait for the animation to finish
            yield return new WaitForSeconds(wipeDuration);

            if (dirtySurfaceObject != null) dirtySurfaceObject.SetActive(false);
            if (cleanSurfaceObject != null) cleanSurfaceObject.SetActive(true);

            Debug.Log("Surface wiped clean!");

            // We don't destroy the parent object, just disable this script
            // to prevent further interaction.
            this.enabled = false; 
        }

        public string GetDescription()
        {
            if (isWiping || !this.enabled) return "";
            return "Wipe surface";
        }
    }
}