using System.Collections.Generic;
using Zenject;

namespace FlappyBird.Gameplay.Pipes
{
    /// <summary>
    /// Pure C# object pool for PipeView prefab instances.
    /// Pre-warming is deferred to Initialize() (called by Zenject after the
    /// container is fully built) to avoid a circular dependency:
    ///   PipePool → Factory → PipeView → IPipePool → PipePool
    /// </summary>
    public sealed class PipePool : IPipePool, IInitializable
    {
        private readonly Stack<PipeView>  _inactive = new Stack<PipeView>();
        private readonly PipeSettings     _settings;
        private readonly PipeView.Factory _factory;

        /// <summary>Constructor injection — no instantiation here.</summary>
        public PipePool(PipeSettings settings, PipeView.Factory factory)
        {
            _settings = settings;
            _factory  = factory;
        }

        // ── IInitializable ─────────────────────────────────────────────────

        /// <summary>
        /// Called by Zenject after all bindings are resolved.
        /// Safe to call Factory.Create() here because IPipePool is already registered.
        /// </summary>
        public void Initialize()
        {
            Prewarm(_settings.InitialPoolSize);
        }

        // ── IPipePool ──────────────────────────────────────────────────────

        /// <inheritdoc/>
        public PipeView Get()
        {
            PipeView pipe = _inactive.Count > 0
                ? _inactive.Pop()
                : _factory.Create();

            pipe.OnSpawn();
            return pipe;
        }

        /// <inheritdoc/>
        public void Return(PipeView pipe)
        {
            pipe.OnDespawn();
            _inactive.Push(pipe);
        }

        // ── Private helpers ────────────────────────────────────────────────

        private void Prewarm(int count)
        {
            for (int i = 0; i < count; i++)
            {
                PipeView pipe = _factory.Create();
                pipe.OnDespawn();
                _inactive.Push(pipe);
            }
        }
    }
}