using System.Collections.ObjectModel;

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

namespace EVE {
	public delegate void UpdateMethod();
	public class ButtonList : Collection<ButtonState> { }
	public class KeyList : Collection<Keys> { }

	public partial class EVEInput : GameComponent {
		private Point lastMouseLocation;
		private Vector2 mouseMoved;
		private ButtonList mouseButtons;
		private KeyList keys;

		public Vector2 MouseMoved { get { return mouseMoved; } }
		public ButtonList MouseButtons { get { return mouseButtons; } }
		public KeyList Keys { get { return keys; } }
		public UpdateMethod OnUpdate;

		public EVEInput(Game game) : base(game) { }

		public override void Initialize() {
			mouseButtons = new ButtonList();
			keys = new KeyList();

			MouseState startState = Mouse.GetState();
			lastMouseLocation = new Point(startState.X, startState.Y);

			base.Initialize();
		}

		public override void Update(GameTime gameTime) {
			MouseState state = Mouse.GetState();

			mouseMoved = new Vector2(
				lastMouseLocation.X - state.X,
				lastMouseLocation.Y - state.Y
			);

			lastMouseLocation = new Point(state.X, state.Y);

			mouseButtons.Clear();
			mouseButtons.Add(state.LeftButton);
			mouseButtons.Add(state.MiddleButton);
			mouseButtons.Add(state.RightButton);
			mouseButtons.Add(state.XButton1);
			mouseButtons.Add(state.XButton2);

			keys.Clear();
			foreach(Keys k in Keyboard.GetState().GetPressedKeys()) {
				Keys.Add(k);
			}

			if(OnUpdate != null) OnUpdate();

			base.Update(gameTime);
		}
	}
}