using System;
using System.Collections;

using Icarus.Logic;
using Icarus.Logic.Controllers;
using Icarus.Logic.Physics;
using Icarus.Logic.GameEngine;
using Icarus.Renderer;
using Icarus.Input;
using Icarus.Input.ButtonEvents;
using Icarus.Input.MovementEvents;

using TuxHunter.GameStates;

namespace TuxHunter.Controllers
{
	/// <summary>
	/// This controller reads in user input to control the character.
	/// </summary>
	public class UserInputController: IController
	{
		public void UpdateObject(PhysicsObject obj)
		{
			IList events = this.ReadInput();
			PhysicsPlayer pp = (PhysicsPlayer) obj;

            foreach (object evt in events)
            {
                if (evt is ButtonPressEvent)
                {
                    ButtonPressEvent button = evt as ButtonPressEvent;
                    ProcessButton(button.Name, button.Pressed, button.Down, button.Released, pp);
                }
            }
		}

		/// <summary>
		/// Returns a list of input events from the user.
		/// </summary>
		/// <returns></returns>
		private IList ReadInput()
		{
			ArrayList events = new ArrayList();
			events.AddRange(InputEngine.Instance.KeyboardEventQueue);
			events.AddRange(InputEngine.Instance.MouseEventQueue);
			events.AddRange(InputEngine.Instance.JoystickEventQueue);

			return events;
		}

		private void ProcessButton(string action, bool pressed, bool down, bool released, PhysicsPlayer player)
		{
			if (action == null) return;

			Icarus.Logic.Physics.Point p;
			Stage s = ((GameplayState) GameEngine.Instance.CurrentState).Level.CurrStage;

			switch (action) 
			{
//                This case is for throwing cd's
//                case "LeftMenu": //Left Alt
//                    if(pressed)
//                    {
//                        if(player is PhysicsMerc)
//                        {
//                            ((PhysicsMerc)player).throwCD();
//                        }
//                    }
//                    break;
                case "LeftControl":
                    if(pressed)
                    {
                        if(player is PhysicsMerc)
                        {
                            ((PhysicsMerc)player).whip();
                        }
                    }
                    break;
				case "LeftArrow":
					if(released)
					{
						if(player.ActionState != (int)PhysicsSettings.enumPlayerActionStates.WalkingRight)
						{
							player.walkStop();
						}
					}
					else //if(pressed)
					{
						player.walkLeft();
					}
					break;
				case "Right":
					if(released)
					{
						if(player.ActionState != (int)PhysicsSettings.enumPlayerActionStates.WalkingLeft)
						{
						    player.walkStop();
						}
					}
					else //if(pressed)
					{
						player.walkRight();
					}

					break;
				case "Space": case "Button: 0":
					if(pressed)
					{
						player.jump();
					}
					break;

				case "Escape":
					if (released)
						GameEngine.Instance.PopState();
					break;

					//Manual scrolling and zooming:
				case "NumPad7":
					s.ZoomFactor /= 1.02f;
					break;

				case "NumPad9":
					s.ZoomFactor *= 1.02f;
					break;

				case "NumPad8":
					p = s.ViewCenter;
					p.y -= 10;
					s.ViewCenter = p;
					break;

				case "NumPad2":
					p = s.ViewCenter;
					p.y += 10;
					s.ViewCenter = p;
					break;

				case "NumPad4":
					p = s.ViewCenter;
					p.x -= 10;
					s.ViewCenter = p;
					break;

				case "NumPad6":
					p = s.ViewCenter;
					p.x += 10;
					s.ViewCenter = p;
					break;
			}
		}
	}
}
