using System;
using System.Collections;

using Icarus.Logic.Controllers;

namespace TuxHunter.Controllers
{
	/// <summary>
	/// The controller for tux.
	/// </summary>
	public class TuxController: RandomBehaviorController
	{
		public TuxController()
		{
			this.AddSubController(MakeBehavior1(), 0.2);
			this.AddSubController(MakeBehavior2(), 0.2);
			this.AddSubController(MakeBehavior3(), 0.2);

			// The rest is left to doing nothing...
		}

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

			result.AddSubController(new MoveLeftController(20));
			result.AddSubController(MakeWhackController());
			result.AddSubController(MakeWhackController());
			result.AddSubController(new MoveRightController(20));

			return result;
		}

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

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

			return result;
		}

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

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

			return result;
		}

		private AbstractSubController MakeWhackController()
		{
			int steps = 20;

			SequenceController whackController = new SequenceController();
			whackController.AddSubController(new MoveBodyDown(steps, 155));
			whackController.AddSubController(new DoNothingController(steps));
			whackController.AddSubController(new MoveBodyUp(steps, 155));

			return whackController;
		}

		private class MoveBodyDown: AbstractSubController 
		{
			private int mPixelsPerMove = 0;

			public MoveBodyDown(int steps, int pixels) : base(steps)
			{
				mPixelsPerMove = pixels / steps;
			}

			protected override void Update(Icarus.Logic.PhysicsObject obj)
			{
				((Icarus.Logic.WorldObject) obj).GetChildAt(0).AnchorPoint.y += mPixelsPerMove;
			}
		}

		private class MoveBodyUp: AbstractSubController 
		{
			private int mPixelsPerMove = 0;

			public MoveBodyUp(int steps, int pixels) : base(steps)
			{
				mPixelsPerMove = pixels / steps;
			}

			protected override void Update(Icarus.Logic.PhysicsObject obj)
			{
				((Icarus.Logic.WorldObject) obj).GetChildAt(0).AnchorPoint.y -= mPixelsPerMove;
			}
		}
	}
}
