using System;

using Icarus.Logic.Physics;

namespace TuxHunter.Controllers
{
	/// <summary>
	/// The controller for the GNU-bot.
	/// </summary>
	public class BotController : RandomBehaviorController
	{
		public BotController()
		{
            this.AddSubController(MakeMoveController(), 0.4);
		}

        private AbstractSubController MakeMoveController()
        {
            SequenceController result = new SequenceController();

            result.AddSubController(new DoNothingController(15));

            result.AddSubController(new MoveLeftController(30));
            result.AddSubController(new BotAttackController(15));
            result.AddSubController(new DoNothingController(15));
            result.AddSubController(new BotAttackController(30));

            result.AddSubController(new MoveRightController(50));
            result.AddSubController(new BotAttackController(45));
            result.AddSubController(new MoveLeftController(30));
            result.AddSubController(new BotAttackController(45));
            result.AddSubController(new MoveRightController(10));

            result.AddSubController(new MoveLeftController(30));
            result.AddSubController(new BotAttackController(15));
            result.AddSubController(new MoveLeftController(10));
            result.AddSubController(new DoNothingController(15));
            result.AddSubController(new MoveRightController(10));
            result.AddSubController(new BotAttackController(30));
            result.AddSubController(new MoveRightController(30));

            result.AddSubController(new MoveRightController(30));
            result.AddSubController(new BotAttackController(15));
            result.AddSubController(new DoNothingController(15));
            result.AddSubController(new BotAttackController(30));

            result.AddSubController(new MoveLeftController(50));
            result.AddSubController(new BotAttackController(45));
            result.AddSubController(new MoveRightController(30));
            result.AddSubController(new BotAttackController(45));
            result.AddSubController(new MoveLeftController(10));
            
            return result;
        }

        private class BotAttackController: AbstractSubController
        {
            public BotAttackController(int steps) : base(steps) { }

            protected override void Update(Icarus.Logic.PhysicsObject obj)
            {
                Icarus.Logic.Physics.PhysicsGNURobot bot = obj as Icarus.Logic.Physics.PhysicsGNURobot;

                if (bot.ActionState != (int) PhysicsSettings.enumPlayerActionStates.WhippingLeft &&
                    bot.ActionState != (int) PhysicsSettings.enumPlayerActionStates.WhippingRight) 
                {
                    bot.attack();
                }
            }
        }
	}
}
