using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Hover
{
    static class Input
    {
        const float deadZone = 0.1f;

        public static KeyboardState kb = new KeyboardState(), kbLast = new KeyboardState();
        public static MouseState ms = new MouseState(), msLast = new MouseState();
        public static GamePadState ps = new GamePadState(), psLast = new GamePadState();

        public static bool JumpHold { get; private set; }
        public static bool Jump { get; private set; }
        public static bool Left { get; private set; }
        public static bool Right { get; private set; }
        public static bool Run { get; private set; }
        public static bool Bomb { get; private set; }

        public static void Update()
        {
            kbLast = kb;
            kb = Keyboard.GetState();
            msLast = ms;
            ms = Mouse.GetState();
            psLast = ps;
            ps = GamePad.GetState(PlayerIndex.One);

            Jump = KeyPressed(Keys.Space) ||
                   KeyPressed(Keys.Up) ||
                   KeyPressed(Keys.W) ||
                   (ps.Buttons.A == ButtonState.Pressed && psLast.Buttons.A == ButtonState.Released);

            JumpHold = KeyDown(Keys.Space) ||
                       KeyDown(Keys.Up) ||
                       KeyDown(Keys.W) ||
                       ps.Buttons.A == ButtonState.Pressed;

            Bomb = KeyPressed(Keys.LeftControl) ||
                   KeyPressed(Keys.Z) ||
                   KeyPressed(Keys.Tab) ||
                   (ps.Buttons.B == ButtonState.Pressed && psLast.Buttons.B == ButtonState.Released);

            Run = KeyDown(Keys.LeftShift) ||
                  KeyDown(Keys.RightShift) ||
                  KeyDown(Keys.NumPad0) ||
                  ps.Triggers.Right > deadZone;

            Left = KeyDown(Keys.Left) ||
                   KeyDown(Keys.A) ||
                   ps.DPad.Left == ButtonState.Pressed ||
                   ps.ThumbSticks.Left.X < -deadZone;

            Right = KeyDown(Keys.Right) ||
                    KeyDown(Keys.D) ||
                    ps.DPad.Right == ButtonState.Pressed ||
                    ps.ThumbSticks.Left.X > deadZone;
        }

        public static bool KeyDown(Keys key)
        {
            return kb.IsKeyDown(key);
        }

        public static bool KeyPressed(Keys key)
        {
            return kb.IsKeyDown(key) && kbLast.IsKeyUp(key);
        }
    }
}
