package edu.stuy.util;

import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.buttons.Button;
import edu.wpi.first.wpilibj.buttons.JoystickButton;

/**
 * Class for both the Logitech Dual Action 2 Gamepad and the Logitech Gamepad
 * F310. The Logitech Gamepad F310 must have the switch on the back set to "D"
 * for this class to work. This class probably also works with the Logitech
 * Wireless Gamepad F710 (untested, but it has the exact same layout as the
 * F310).
 * @author kevin
 */
public class Gamepad extends Joystick {

    public Gamepad(int port) {
        super(port);
    }

    /**
     * The left analog stick x-axis.
     * @return value of left analog x-axis
     */
    public double getLeftX() {
        return getRawAxis(0);
    }

    /**
     * The left analog stick y-axis.
     * @return value of left analog y-axis
     */
    public double getLeftY() {
        return getRawAxis(1);
    }

    /**
     * The right analog stick x-axis.
     * @return value of right analog x-axis
     */
    public double getRightX() {
        return getRawAxis(2);
    }

    /**
     * The right analog stick y-axis.
     * @return value of right analog y-axis
     */
    public double getRightY() {
        return getRawAxis(3);
    }

    /**
     * The directional pad of the gamepad.
     * @return value of the left/right d-pad buttons
     */
    public double getDPadX() {
        return getRawAxis(4);
    }

    /**
     * The direction pad of the gamepad.
     * @return value of the up/down d-pad buttons
     */
    public double getDPadY() {
        return getRawAxis(5);
    }

    /**
     * The upper d-pad button.
     * @return if upper d-pad button is pressed
     */
    public boolean getRawDPadUp() {
        return getDPadY() < -.5;
    }

    public DPadButton getDPadUp() {
        return new DPadButton(this, DPadButton.Direction.UP);
    }

    /**
     * The lower d-pad button.
     * @return if the lower d-pad button is pressed
     */
    public boolean getRawDPadDown() {
        return getDPadY() > .5;
    }

    public DPadButton getDPadDown() {
        return new DPadButton(this, DPadButton.Direction.DOWN);
    }

    /**
     * The left d-pad button.
     * @return if the left d-pad button is pressed
     */
    public boolean getRawDPadLeft() {
        return getDPadX() < -.5;
    }

    public DPadButton getDPadLeft() {
        return new DPadButton(this, DPadButton.Direction.LEFT);
    }

    /**
     * The right d-pad button.
     * @return if the right d-pad button is pressed
     */
    public boolean getRawDPadRight() {
        return getDPadX() > .5;
    }

    public DPadButton getDPadRight() {
        return new DPadButton(this, DPadButton.Direction.RIGHT);
    }

    /**
     * The left bumper.
     * @return if the left bumper is pressed
     */
    public boolean getRawLeftBumper() {
        return getRawButton(5);
    }

    public JoystickButton getLeftBumper() {
        return new JoystickButton(this, 5);
    }

    /**
     * The right bumper.
     * @return if the right bumper is pressed
     */
    public boolean getRawRightBumper() {
        return getRawButton(6);
    }

    public JoystickButton getRightBumper() {
        return new JoystickButton(this, 6);
    }

    /**
     * The left trigger.
     * @return if the left trigger is pressed
     */
    public boolean getRawLeftTrigger() {
        return getRawButton(7);
    }

    public JoystickButton getLeftTrigger() {
        return new JoystickButton(this, 7);
    }

    /**
     * The right trigger.
     * @return if the right trigger is pressed
     */
    public boolean getRawRightTrigger() {
        return getRawButton(8);
    }

    public JoystickButton getRightTrigger() {
        return new JoystickButton(this, 8);
    }

    /**
     * The left button of the button group. On some gamepads this is X.
     * @return if the left button is pressed
     */
    public boolean getRawLeftButton() {
        return getRawButton(1);
    }

    public JoystickButton getLeftButton() {
        return new JoystickButton(this, 1);
    }

    /**
     * The left button of the button group. On some gamepads this is A.
     * @return if the bottom button is pressed
     */
    public boolean getRawBottomButton() {
        return getRawButton(2);
    }

    public JoystickButton getBottomButton() {
        return new JoystickButton(this, 2);
    }

    /**
     * The left button of the button group. On some gamepads this is B.
     * @return if the right button is pressed
     */
    public boolean getRawRightButton() {
        return getRawButton(3);
    }

    public JoystickButton getRightButton() {
        return new JoystickButton(this, 3);
    }

    /**
     * The top button of the button group. On some gamepads this is Y.
     * @return if the top button is pressed
     */
    public boolean getRawTopButton() {
        return getRawButton(4);
    }

    public JoystickButton getTopButton() {
        return new JoystickButton(this, 4);
    }

    /**
     * The central left button. On some gamepads this is the select button.
     * @return if the back button is pressed
     */
    public boolean getRawBackButton() {
        return getRawButton(9);
    }

    public JoystickButton getBackButton() {
        return new JoystickButton(this, 9);
    }

    /**
     * The central right button. On some gamepads this is the start button.
     * @return if the start button is pressed
     */
    public boolean getRawStartButton() {
        return getRawButton(10);
    }

    public JoystickButton getStartButton() {
        return new JoystickButton(this, 10);
    }

    /**
     * The click-function of the left analog stick.
     * @return if the left analog stick is being clicked down
     */
    public boolean getRawLeftAnalogButton() {
        return getRawButton(11);
    }

    public JoystickButton getLeftAnalogButton() {
        return new JoystickButton(this, 11);
    }

    /**
     * The click-function of the right analog stick.
     * @return if the right analog stick is being clicked down
     */
    public boolean getRawRightAnalogButton() {
        return getRawButton(12);
    }

    public JoystickButton getRightAnalogButton() {
        return new JoystickButton(this, 12);
    }

    public static class DPadButton extends Button {
        public static enum Direction {
            UP, DOWN, LEFT, RIGHT
        }

        private Gamepad gamepad;
        private Direction direction;

        public DPadButton(Gamepad gamepad, Direction direction) {
            this.gamepad = gamepad;
            this.direction = direction;
        }

        public boolean get() {
            switch (direction) {
            case UP:
                return gamepad.getRawDPadUp();
            case DOWN:
                return gamepad.getRawDPadDown();
            case LEFT:
                return gamepad.getRawDPadLeft();
            case RIGHT:
                return gamepad.getRawDPadRight();
            default: // Never reached
                return false;
            }
        }
    }
}

@@ -1265,363 +1265,8 @@
  %7D%0A%0A
-    /**%0A     * The directional pad of the gamepad.%0A     * @return value of the left/right d-pad buttons%0A     */%0A    public double getDPadX() %7B%0A        return getRawAxis(4);%0A    %7D%0A%0A    /**%0A     * The direction pad of the gamepad.%0A     * @return value of the up/down d-pad buttons%0A     */%0A    public double getDPadY() %7B%0A        return getRawAxis(5);%0A    %7D%0A%0A
     
@@ -1292,32 +1292,32 @@
 r d-pad button.%0A
+
      * @return i
@@ -1414,21 +1414,18 @@
  get
-DPadY() %3C -.5
+POV() == 0
 ;%0A  
@@ -1693,20 +1693,20 @@
  get
-DPadY() %3E .5
+POV() == 180
 ;%0A  
@@ -1976,21 +1976,20 @@
  get
-DPadX() %3C -.5
+POV() == 270
 ;%0A  
@@ -2262,20 +2262,19 @@
  get
-DPadX() %3E .5
+POV() == 90
 ;%0A  

Refactor gamepad d-pad to match new Joystick API.