package de.gurkenlabs.litiengine.input;

import java.awt.AWTException;
import java.util.logging.Level;
import java.util.logging.Logger;

import de.gurkenlabs.litiengine.Game;
import de.gurkenlabs.litiengine.GameListener;

/**
 * The static <code>Input</code> class is the LITIengine's access point to devices that capture physical player input.
 * It manages input from different devices, i.e. keyboard, mouse or gamepad, and provides a unified API to access this information.
 * 
 * @see #mouse()
 * @see #keyboard()
 * @see #gamepads()
 */
public final class Input {
  private static final Logger log = Logger.getLogger(Input.class.getName());

  private static GamepadManager gamePadManager;
  private static IKeyboard keyboard;
  private static IMouse mouse;

  private Input() {
    throw new UnsupportedOperationException();
  }

  public static GamepadManager gamepads() {
    if (!Game.config().input().isGamepadSupport()) {
      log.log(Level.SEVERE, "Cannot access gamepads because gamepad support is disabled in the configuration.");
    }

    return gamePadManager;
  }

  public static IKeyboard keyboard() {
    return keyboard;
  }

  public static IMouse mouse() {
    return mouse;
  }

  public static final class InputGameAdapter implements GameListener {
    @Override
    public void terminated() {
      if (gamePadManager != null) {
        gamePadManager.terminate();
      }
    }

    @Override
    public void initialized(String... args) {
      init();
    }

    @Override
    public void started() {
      if (gamePadManager != null) {
        gamePadManager.start();
      }
    }

    private static void init() {
      try {
        keyboard = new Keyboard();
        Mouse m = new Mouse();
        
        if (!Game.isInNoGUIMode()) {
          Game.window().getRenderComponent().addMouseListener(m);
          Game.window().getRenderComponent().addMouseMotionListener(m);
          Game.window().getRenderComponent().addMouseWheelListener(m);
        }
        
        mouse = m;
        if (Game.config().input().isGamepadSupport()) {
          gamePadManager = new GamepadManager();
        }
      } catch (AWTException e) {
        log.log(Level.SEVERE, e.getMessage(), e);
      }
    }
  }
}

@@ -863,330 +863,1146 @@
 %0D%0A  
-public static GamepadManager gamepads() %7B%0D%0A    if (!Game.config().input().isGamepadSupport()) %7B%0D%0A      log.log(Level.SEVERE, %22Cannot access gamepads because gamepad support is disabled in the configuration.%22);%0D%0A    %7D%0D%0A%0D%0A    return gamePadManager;%0D%0A  %7D%0D%0A%0D%0A  public static IKeyboard keyboard() %7B%0D%0A    return keyboard;%0D%0A  %7D%0D%0A
+/**%0D%0A   * Gets the manager for all gamepad input devices.%0D%0A   * %0D%0A   * %3Cp%3E%0D%0A   * The manager provides easy access to the default controller as well as access by gamepad index for mulitplayer games.%0D%0A   * Gamepads don't need to be added explicitly, the manager supports hot-plugging at runtime and will auto-detect any%0D%0A   * added/removed gamepads.%0D%0A   * %3C/p%3E%0D%0A   * %0D%0A   * %3Cp%3E%0D%0A   * %3Cb%3EThis returns null if %3Ccode%3EGame.config().input().isGamepadSupport()%3C/code%3E is set to false.%3C/b%3E%0D%0A   * %3C/p%3E%0D%0A   * %0D%0A   * @return The gamepad manager.%0D%0A   * %0D%0A   * @see GamepadManager#current()%0D%0A   * @see GamepadManager#get(int)%0D%0A   */%0D%0A  public static GamepadManager gamepads() %7B%0D%0A    if (!Game.config().input().isGamepadSupport()) %7B%0D%0A      log.log(Level.SEVERE, %22Cannot access gamepads because gamepad support is disabled in the configuration.%22);%0D%0A    %7D%0D%0A%0D%0A    return gamePadManager;%0D%0A  %7D%0D%0A%0D%0A  /**%0D%0A   * Gets the keyboard input device.%0D%0A   * %0D%0A   * @return The keyboard input device.%0D%0A   */%0D%0A  public static IKeyboard keyboard() %7B%0D%0A    return keyboard;%0D%0A  %7D%0D%0A%0D%0A  /**%0D%0A   * Gets the mouse input device.%0D%0A   * %0D%0A   * @return The mouse input device.%0D%0A   */
 %0D%0A  
@@ -2603,24 +2603,16 @@
 use();%0D%0A
-        
 %0D%0A      
@@ -2866,24 +2866,16 @@
      %7D%0D%0A
-        
 %0D%0A      

Add some javadoc.