using System;

namespace Icarus.Logic.Physics
{
	/// <summary>
	/// Summary description for PhysicsShortLife.
	/// </summary>
	public class PhysicsShortLife : WorldObject
	{

        /// <summary>
        /// The number of game engine iterations this object should live
        /// </summary>
        private int iIterationsToLive;

        /// <summary>
        /// The number of iterations this obj flickers once it's life has ran out
        /// </summary>
        private int iIterationsToFlicker;

        /// <summary>
        /// The number of game egine iterations that this object has lived
        /// </summary>
        private int iIterationCount;

        /// <summary>
        /// The animation ID to use while flickering.  The visible portion of the flicker.
        /// </summary>
        private string strAnimationID;

        /// <summary>
        /// gets or sets the number of game engine iterations that this worldobject lives
        /// </summary>
        public int IterationsToLive
        {
            get { return this.iIterationsToLive; }
            set { this.iIterationsToLive = value; }
        }

        /// <summary>
        /// gets or sets the number of game engine iterations this object has lived
        /// </summary>
        public int IterationCount
        {
            get { return this.iIterationCount; }
            set { this.iIterationCount = value; }
        }

        /// <summary>
        /// gets or set the number of iterations this obj flickers once it's life is over
        /// </summary>
        public int FlickerTime
        {
            get { return this.iIterationsToFlicker; }
            set { this.iIterationsToFlicker = value; }
        }

        /// <summary>
        /// Gets or sets the animation that is used during the "on" portion of flickering
        /// </summary>
        public string FlickerAnimationId
        {
            get { return this.strAnimationID; }
            set { this.strAnimationID = value; }
        }

		public PhysicsShortLife()
		{
			//Setting defualt lifespan
            this.iCollisionsToLive = 4*45;//5 sec at normal engine speed
            //Setting defualt flicker time
            this.iIterationsToFlicker = 2*45;// 2 sec at normal engine speed
            
            //Default animation id
            this.strAnimationID = "@\null\null_fset.png";
		}

        /// <summary>
        /// Keeps track of age, then makes flicker, then removes.
        /// </summary>
        /// <returns>action to be taken by physicsengine</returns>
        public override int specificUpdates()
        {
            this.iIterationCount++;
            if(this.iIterationCount > this.iIterationsToLive)
            {
                //this.Velocity.i = 0;

                if(this.iIterationCount > this.iIterationsToLive + this.iIterationsToFlicker)
                {
                    return (int)PhysicsSettings.enumActions.Remove;
                }

                if((this.iIterationCount/5)%2 == 0)//Swap animation every other frame
                {
                    this.SetAnimation(@"\null\null_fset.png", Icarus.Graphics.Animation.AnimationType.LoopOnce, 1);
                }
                else
                {
                    this.SetAnimation(this.strAnimationID, Icarus.Graphics.Animation.AnimationType.LoopOnce, 1);
                }
            }

            return (int)PhysicsSettings.enumActions.DoNothing;
        }

        
	}
}
