// Archontia Anna Tsapanidou, NTU - MSc Computer Games Systems 2008-2009

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Input;

namespace TankAttack
{
	#region InputControl Class
	public abstract class InputControl
	{
		public enum Actions { NONE, UP, DOWN, LEFT, RIGHT, FIRE, MENU };
        public enum InputType { NONE, AUTO, KEYBOARD, JOYPAD }

		public virtual List<InputControl.Actions> GetAction()
		{
			return new List<InputControl.Actions>();
		}
		public new virtual InputType GetType()
		{
			return InputType.NONE;
		}
	}
	#endregion

	#region KeyboardControl Class (derived from InputControl)
	public class KeyboardControl : InputControl
	{
		public Keys Up;
		public Keys Down;
		public Keys Left;
		public Keys Right;
		public Keys Fire;
		public Keys Menu;

		public KeyboardControl()
		{
			Up = Keys.W;
			Down = Keys.S;
			Left = Keys.A;
			Right = Keys.D;
			Fire = Keys.Space;
			Menu = Keys.Escape;
		}

		public override List<InputControl.Actions> GetAction()
		{
			List<InputControl.Actions> tempActions = new List<InputControl.Actions>();
			if (Keyboard.GetState().IsKeyDown(Menu) )
			{
				tempActions.Add(Actions.MENU);
			}
			else
			{
				if (Keyboard.GetState().IsKeyDown(Up))
				{
					tempActions.Add(Actions.UP);
				}
				else if (Keyboard.GetState().IsKeyDown(Down))
				{
					tempActions.Add(Actions.DOWN);
				}
				else if (Keyboard.GetState().IsKeyDown(Left))
				{
					tempActions.Add(Actions.LEFT);
				}
				else if (Keyboard.GetState().IsKeyDown(Right))
				{
					tempActions.Add(Actions.RIGHT);
				}
				if (Keyboard.GetState().IsKeyDown(Fire))
				{
					tempActions.Add(Actions.FIRE);
				}
			}
			return tempActions;
		}
		public override InputType GetType()
		{
			return InputType.KEYBOARD;
		}
	}
	#endregion

	#region GamePadControl Class (derived from InputControl)
	class GamePadControl : InputControl
	{
		public Buttons Up	= Buttons.DPadUp;
		public Buttons Down = Buttons.DPadDown;
		public Buttons Left	= Buttons.DPadLeft;
		public Buttons Right = Buttons.DPadRight;
		public Buttons Fire = Buttons.X;
		public Buttons Menu = Buttons.Back;
		public PlayerIndex playerIndex;
		public GamePadControl()
		{
			Up		= Buttons.DPadUp;
			Down	= Buttons.DPadDown;
			Left	= Buttons.DPadLeft;
			Right	= Buttons.DPadRight;
			Fire	= Buttons.X;
			Menu	= Buttons.Back;
			playerIndex = PlayerIndex.One;
		}

		public override List<InputControl.Actions> GetAction()
		{
			List<InputControl.Actions> tempActions = new List<InputControl.Actions>();
			if (GamePad.GetState(PlayerIndex.One).IsButtonDown(Menu))
			{
				tempActions.Add(Actions.MENU);
			}
			else
			{
				if (GamePad.GetState(playerIndex).IsButtonDown(Up))
				{
					tempActions.Add(Actions.UP);
				}
				else if (GamePad.GetState(playerIndex).IsButtonDown(Down))
				{
					tempActions.Add(Actions.DOWN);
				}
				else if (GamePad.GetState(playerIndex).IsButtonDown(Left))
				{
					tempActions.Add(Actions.LEFT);
				}
				else if (GamePad.GetState(playerIndex).IsButtonDown(Right))
				{
					tempActions.Add(Actions.RIGHT);
				}
				if (GamePad.GetState(playerIndex).IsButtonDown(Fire))
				{
					tempActions.Add(Actions.FIRE);
				}
			}
			return tempActions;
		}
		public override InputType GetType()
		{
			return InputType.JOYPAD;
		}
	}
	#endregion
		
	#region DummyControl Class (derived from InputControl)
	class DummyControl : InputControl
	{
		public override List<InputControl.Actions> GetAction()
		{
			return new List<InputControl.Actions>();
		}
		public override InputType GetType()
		{
			return InputType.AUTO;
		}
	}
	#endregion

    #region AIControl class (derived from InputControl)
    public class AIControl : InputControl
	{
		public enum MoveState { NORTH, SOUTH, EAST, WEST, STILL };
		public MoveState moveState;
		public bool fire;

		public override List<InputControl.Actions> GetAction()
		{
			List<InputControl.Actions> tempActions = new List<InputControl.Actions>();

			switch (moveState)
			{
				case MoveState.NORTH:
					tempActions.Add(Actions.UP);
					break;
				case MoveState.SOUTH:
					tempActions.Add(Actions.DOWN);
					break;
				case MoveState.EAST:
					tempActions.Add(Actions.RIGHT);
					break;
				case MoveState.WEST:
					tempActions.Add(Actions.LEFT);
					break;
			}

			if (fire == true)
			{
				tempActions.Add(Actions.FIRE);
				fire = false;
			}
			return tempActions;
		}

		public override InputType GetType()
		{
			return InputType.AUTO;
		}
	} 
    #endregion
}
   