using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace DataDrivenExample
{
    class WorldElement
    {
        public WorldElement() { }

        public Vector2 Position { get; set; }
        public Texture2D Texture { get; set; }
        public string TextureName { get; set; }
        public virtual void Update (GameTime time) { }
    }


    class Platform : WorldElement
    {
        public bool IsMoving {get; set;}
        public List<Vector2> Path { get; set; }
        public bool ReverseOnPathEnd { get; set; }
        public float Speed { get; set; }


        protected int direction = 1;
        protected int targetIndex;


        protected void incrementTargetIndex()
        {
            targetIndex += direction;
            if (targetIndex >= Path.Count)
            {
                if (ReverseOnPathEnd)
                {
                    direction = -1;
                    targetIndex += direction + direction;
                }
                else
                    targetIndex = 0;
            }
            else if (targetIndex < 0)
            {
                targetIndex = 1;
                direction = 1;
            }
        }

        public override void Update(GameTime time)
        {
            if (IsMoving)
            {
                Vector2 target = Path[targetIndex];
                float moveDistance = Speed * (float)time.ElapsedGameTime.TotalSeconds;
                if (moveDistance == 0)
                {
                    return;
                }
                while ((target - Position).LengthSquared() < moveDistance * moveDistance)
                {
                    Position = target;
                    incrementTargetIndex();
                    target = Path[targetIndex];
                }
                Vector2 moveVector = (target - Position);
                moveVector.Normalize();
                moveVector = moveVector * moveDistance;
                Position = Position + moveVector;

            }
        }

    }
}