using ActionGame.GameArtifacts.Models.Characters;
using ActionGame.GameArtifacts.Operators;
using Microsoft.Xna.Framework;
using System;


namespace ActionGame.GameArtifacts.Intelligence.NonPlayerCharacterMovement
{
    public class FastRandomMotion : INonPlayerCharacterMovement
    {
        private IMoveableObject moveableObject;
        private ObjectMover.Direction? lastDirection;
        private TimeSpan lastDirectionChange;
        private Int32 continuousCounter;


        public FastRandomMotion(IMoveableObject moveableObject)
        {
            this.moveableObject = moveableObject;

            continuousCounter = 999;
            lastDirection = null;
        }


        public void Act(GameTime gameTime)
        {
            ObjectMover.Direction? direction = ObjectMover.Direction.Left;

            if (lastDirectionChange == null)
            {
                lastDirectionChange = gameTime.TotalGameTime;
            }

            if ((gameTime.TotalGameTime - lastDirectionChange).TotalSeconds > 0.10)
            {
                if (continuousCounter > 10)
                {
                    Random rand = new Random();
                    Int32 val = 0;
                    val = rand.Next(0, 6);

                    if (val == 0) direction = ObjectMover.Direction.Down;
                    else if (val == 1) direction = ObjectMover.Direction.Up;
                    else if (val == 2) direction = ObjectMover.Direction.Right;
                    else if (val == 3) direction = ObjectMover.Direction.Left;
                    else direction = null;

                    continuousCounter = 0;
                }
                else
                {
                    direction = lastDirection;
                    continuousCounter++;
                }

                if (direction != null)
                {
                    ObjectMover mover = new ObjectMover(moveableObject);
                    mover.Move((ObjectMover.Direction)direction);
                }

                lastDirection = direction;

                lastDirectionChange = gameTime.TotalGameTime;
            }

        }


        public void PostMovementAct(GameTime gameTime)
        {

        }


    }
}
