using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;

namespace AdvancedXNAGamesWP
{
	public struct ControlState
	{
		public bool Left;
		public bool Right;
		public bool Up;
		public bool Down;
		public bool Start;
		public bool Quit;
		public bool Fire;
		public Vector2 FingerPosition;
	}

	public static class InputManager
	{
		private static KeyboardState _keyboardState, _lastKeyboard;
		private static GamePadState _gamePadState, _lastGamePad;
		private static ControlState _controlState;

		static InputManager()
		{
			TouchPanel.EnabledGestures = GestureType.Tap | GestureType.HorizontalDrag | GestureType.VerticalDrag;
		}

		public static void Update()
		{
			_keyboardState = Keyboard.GetState();
			_gamePadState = GamePad.GetState(PlayerIndex.One);

			_controlState.Quit	= (_gamePadState.Buttons.Back== ButtonState.Pressed || _keyboardState.IsKeyDown(Keys.Escape));
			_controlState.Start	= (_gamePadState.Buttons.B	== ButtonState.Pressed 
									|| _keyboardState.IsKeyDown(Keys.Enter) || _keyboardState.IsKeyDown(Keys.Space) || _gamePadState.IsButtonDown(Buttons.Start)); 
			_controlState.Left	= (_gamePadState.DPad.Left	== ButtonState.Pressed || _gamePadState.ThumbSticks.Left.X < -0.1f || _keyboardState.IsKeyDown(Keys.Left));
			_controlState.Right	= (_gamePadState.DPad.Right	== ButtonState.Pressed || _gamePadState.ThumbSticks.Left.X > 0.1f || _keyboardState.IsKeyDown(Keys.Right));
			_controlState.Up	= (_gamePadState.DPad.Up	== ButtonState.Pressed || _gamePadState.ThumbSticks.Left.Y < -0.1f || _keyboardState.IsKeyDown(Keys.Up));
			_controlState.Down	= (_gamePadState.DPad.Down	== ButtonState.Pressed || _gamePadState.ThumbSticks.Left.Y > 0.1f || _keyboardState.IsKeyDown(Keys.Down));

			_controlState.Fire	= ((_gamePadState.Buttons.B	== ButtonState.Pressed && _lastGamePad.Buttons.B == ButtonState.Released) 
									|| (_keyboardState.IsKeyDown(Keys.Space) && !_lastKeyboard.IsKeyDown(Keys.Space)));

			while(TouchPanel.IsGestureAvailable)
			{
				GestureSample gs = TouchPanel.ReadGesture();
				_controlState.Fire |= (gs.GestureType == GestureType.Tap);
				_controlState.Start |= (gs.GestureType == GestureType.Tap);
				if(gs.GestureType == GestureType.HorizontalDrag)
				{
					_controlState.Left = (gs.Delta.X < 0);
					_controlState.Right = (gs.Delta.X > 0);
					_controlState.FingerPosition = gs.Position;
				}

				if(gs.GestureType == GestureType.VerticalDrag)
				{
					_controlState.Down = (gs.Delta.Y < 0);
					_controlState.Up = (gs.Delta.Y > 0);
					_controlState.FingerPosition = gs.Position;
				}
			}

			_lastGamePad = _gamePadState;
			_lastKeyboard = _keyboardState;
		}

		public static ControlState ControlState
		{
			get { return _controlState; }
		}
	}
}
