using UnityEngine;
using System.Collections;

/// <summary>
/// Static utility class that provides common click blocking functionality
/// for UI components that implement IClickBlocker.
/// This avoids the need for inheritance and works with any MonoBehaviour.
/// </summary>
public static class ClickBlockingHelper
{
    /// <summary>
    /// Registers a click blocker with the ClickManager when enabled
    /// Call this from your OnEnable method
    /// </summary>
    public static void RegisterWithClickManager(IClickBlocker blocker)
    {
        ClickManager.Instance?.RegisterClickBlocker(blocker);
    }

    /// <summary>
    /// Unregisters a click blocker from the ClickManager when disabled
    /// Call this from your OnDisable method
    /// </summary>
    public static void UnregisterWithClickManager(IClickBlocker blocker)
    {
        ClickManager.Instance?.UnregisterClickBlocker(blocker);
    }

    /// <summary>
    /// Handles the common click flag logic for UI components.
    /// Returns true if a recent UI click should block map clicks.
    /// </summary>
    /// <param name="recentlyHandledClick">Reference to the component's click flag</param>
    /// <param name="componentName">Name of the component for debug logging</param>
    /// <param name="coroutineStarter">MonoBehaviour to start coroutines on</param>
    /// <returns>True if should block due to recent click</returns>
    public static bool HandleRecentClickCheck(ref bool recentlyHandledClick, string componentName, MonoBehaviour coroutineStarter)
    {
        if (recentlyHandledClick)
        {
            Debug.Log($"🚫 {componentName}: Blocking click due to recent UI interaction");
            return true;
        }
        return false;
    }

    /// <summary>
    /// Sets a temporary click blocking flag and starts the reset coroutine.
    /// Call this from UI event handlers (MouseDownEvent, ClickEvent, etc.)
    /// </summary>
    /// <param name="componentName">Name of the component for debug logging</param>
    /// <param name="coroutineStarter">MonoBehaviour to start coroutines on</param>
    /// <param name="flagSetter">Action to set the flag value</param>
    /// <returns>Coroutine that can be stored by the caller</returns>
    public static Coroutine SetClickFlag(string componentName, MonoBehaviour coroutineStarter, System.Action<bool> flagSetter)
    {
        flagSetter(true);
        Debug.Log($"🚫 {componentName}: Click flag SET - blocking map clicks");

        // Stop any existing reset coroutines to prevent premature reset
        coroutineStarter.StopAllCoroutines();

        // Start the reset coroutine
        return coroutineStarter.StartCoroutine(ResetClickFlagCoroutine(componentName, flagSetter));
    }

    /// <summary>
    /// Coroutine that resets the click blocking flag after a delay
    /// </summary>
    private static IEnumerator ResetClickFlagCoroutine(string componentName, System.Action<bool> flagSetter)
    {
        // Wait longer to ensure we block clicks that happen in the same frame
        yield return new WaitForEndOfFrame(); // Wait one frame
        yield return new WaitForEndOfFrame(); // Wait another frame
        yield return new WaitForSeconds(0.1f); // Wait additional time to be extra safe

        flagSetter(false);
        Debug.Log($"🔓 {componentName}: Click flag reset - map clicks allowed again");
    }

    /// <summary>
    /// Helper method to check if UI should skip blocking due to being invisible
    /// </summary>
    /// <param name="isUIVisible">Whether the UI is currently visible</param>
    /// <returns>True if should skip blocking (UI is invisible)</returns>
    public static bool ShouldSkipDueToInvisibleUI(bool isUIVisible)
    {
        return !isUIVisible; // Skip blocking if UI is not visible
    }
}
