using UdonSharp;
using UnityEngine;

public class LocalObjectPool : UdonSharpBehaviour
{
    [Header("Pool Configuration")]
    public GameObject[] Pool;
    public int denomination;

    [Header("Debug")]
    public int availableCount;
    public int spawnedCount;

    private bool[] available;

    void Start()
    {
        InitializePool();
    }

    private void InitializePool()
    {
        if (Pool == null || Pool.Length == 0)
        {
            Debug.LogWarning("LocalObjectPool: Pool array is empty or null!");
            return;
        }

        // Initialize availability array
        available = new bool[Pool.Length];

        // Disable all objects and mark as available
        for (int i = 0; i < Pool.Length; i++)
        {
            if (Pool[i] != null)
            {
                Pool[i].SetActive(false);
                available[i] = true;
            }
            else
            {
                Debug.LogWarning($"LocalObjectPool: Pool[{i}] is null!");
                available[i] = false;
            }
        }

        UpdateDebugCounts();
    }

    /// <summary>
    /// Try to spawn an object from the pool.
    /// Returns the spawned GameObject, or null if no objects are available.
    /// </summary>
    public GameObject TryToSpawn()
    {
        if (available == null)
        {
            Debug.LogError("LocalObjectPool: Pool not initialized!");
            return null;
        }

        // Find first available object
        for (int i = 0; i < available.Length; i++)
        {
            if (available[i] && Pool[i] != null)
            {
                available[i] = false;
                Pool[i].SetActive(true);

                UpdateDebugCounts();
                return Pool[i];
            }
        }

        // No available objects
        return null;
    }

    /// <summary>
    /// Return an object to the pool.
    /// The object will be disabled and moved back to the pool's transform position.
    /// </summary>
    public void Return(GameObject go)
    {
        if (go == null)
        {
            Debug.LogWarning("LocalObjectPool: Attempted to return null object!");
            return;
        }

        // Find the object in the pool
        for (int i = 0; i < Pool.Length; i++)
        {
            if (Pool[i] == go)
            {
                // Reset transform to pool origin
                go.transform.SetParent(transform);
                go.transform.localPosition = Vector3.zero;
                go.transform.localRotation = Quaternion.identity;

                // Disable and mark as available
                go.SetActive(false);
                available[i] = true;
                UpdateDebugCounts();
                return;
            }
        }
    }

    /// <summary>
    /// Check whether the given object belongs to this pool.
    /// </summary>
    public bool OwnsObject(GameObject go)
    {
        if (go == null || Pool == null) return false;

        for (int i = 0; i < Pool.Length; i++)
        {
            if (Pool[i] == go) return true;
        }
        return false;
    }

    private void UpdateDebugCounts()
    {
        if (available == null) return;

        availableCount = 0;
        spawnedCount = 0;

        for (int i = 0; i < available.Length; i++)
        {
            if (available[i])
                availableCount++;
            else
                spawnedCount++;
        }
    }
}