using System;
using System.Collections.Generic;
using System.Text;

namespace Vortex.Drawing.Particles {
   
    ///<summary>Particle collection is container of particles. It contains their life and order</summary>
    public class ParticleCollection {
        List<BaseParticle> m_Particles = new List<BaseParticle>();
        List<BaseParticle> m_ShadowParticles = new List<BaseParticle>();

        public ParticleCollection() {
            ;
        }

        ///<summary>Add particle to collection</summary>
        public void Add(BaseParticle particle) {
            m_Particles.Add(particle);
        }

        ///<summary>Number of particles</summary>
        public int Count {
            get { return m_Particles.Count; }
        }

        ///<summary>Enumeration of particles</summary>
        public IEnumerable<BaseParticle> Particles {
            get { return m_Particles; }
        }

		/// <summary>
		/// Clears this collection.
		/// </summary>
		public void Clear() {
			m_Particles.Clear();
		}

        ///<summary>Update particles in collection: move, rotate, scale etc</summary>
        public void Update(float timeDelta) {
            //iterate for all particles
            foreach (BaseParticle particle in m_Particles) {
                if (particle.Update(timeDelta)) {
                    m_ShadowParticles.Add(particle);
                }
            }

            //swap lists
            List<BaseParticle> tempList = m_ShadowParticles;
            m_ShadowParticles = m_Particles;
            m_Particles = tempList;

            //clear everything
            m_ShadowParticles.Clear();
        }

        ///<summary>Draws collection of particles in direct or reverse order</summary>
        public void Draw(Canvas2D canvas, bool reverseOrder) {
            //draw all particles
            foreach (BaseParticle particle in m_Particles) {
                canvas.DrawSpriteEntity(particle);
            }
        }
    }
}
