package de.lessvoid.nifty.renderer.jogl.input;

import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.concurrent.ConcurrentLinkedQueue;

import de.lessvoid.nifty.NiftyInputConsumer;
import de.lessvoid.nifty.input.keyboard.KeyboardInputEvent;
import de.lessvoid.nifty.input.mouse.MouseInputEvent;
import de.lessvoid.nifty.spi.input.InputSystem;

/**
 * Copy of InputSystemAwtImpl
 */
public class JoglInputSystem implements InputSystem, MouseMotionListener, MouseListener,
        KeyListener {

    private ConcurrentLinkedQueue<MouseInputEvent> mouseEvents = new ConcurrentLinkedQueue<MouseInputEvent>();

    private ConcurrentLinkedQueue<KeyboardInputEvent> keyboardEvents = new ConcurrentLinkedQueue<KeyboardInputEvent>();

    @Override
    public void forwardEvents(final NiftyInputConsumer inputEventConsumer) {
        MouseInputEvent mouseEvent = mouseEvents.poll();
        while (mouseEvent != null) {
            inputEventConsumer.processMouseEvent(mouseEvent);
            mouseEvent = mouseEvents.poll();
        }

        KeyboardInputEvent keyEvent = keyboardEvents.poll();
        while (keyEvent != null) {
            inputEventConsumer.processKeyboardEvent(keyEvent);
            keyEvent = keyboardEvents.poll();
        }
    }

    @Override
    public void mouseDragged(MouseEvent mouseEvent) {
        // at the moment we only care about the BUTTON1
        if (mouseEvent.getButton() == MouseEvent.BUTTON1
                || mouseEvent.getModifiers() == InputEvent.BUTTON1_MASK) {
            mouseEvents.add(new MouseInputEvent(mouseEvent.getX(), mouseEvent.getY(), true));
        }
    }

    @Override
    public void mouseMoved(MouseEvent mouseEvent) {
        // at the moment we only care about the BUTTON1
        if (mouseEvent.getButton() == MouseEvent.BUTTON1) {
            mouseEvents.add(new MouseInputEvent(mouseEvent.getX(), mouseEvent.getY(), true));
        }
    }

    @Override
    public void mouseClicked(MouseEvent mouseEvent) {

    }

    @Override
    public void mouseEntered(MouseEvent mouseEvent) {

    }

    @Override
    public void mouseExited(MouseEvent mouseEvent) {

    }

    @Override
    public void mousePressed(MouseEvent mouseEvent) {
        // at the moment we only care about the BUTTON1
        if (mouseEvent.getButton() == MouseEvent.BUTTON1) {
            mouseEvents.add(new MouseInputEvent(mouseEvent.getX(), mouseEvent.getY(), true));
        }
    }

    @Override
    public void mouseReleased(MouseEvent mouseEvent) {
        // at the moment we only care about the BUTTON1
        if (mouseEvent.getButton() == MouseEvent.BUTTON1) {
            mouseEvents.add(new MouseInputEvent(mouseEvent.getX(), mouseEvent.getY(), false));
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {
        handleKeyEvent(e, true);
    }

    @Override
    public void keyReleased(KeyEvent e) {
        handleKeyEvent(e, false);
    }

    private void handleKeyEvent(KeyEvent e, boolean isKeyDown) {
        int newKeyCode = keyCodeConverter.convertToNiftyKeyCode(e.getKeyCode(), e.getKeyLocation());
        keyboardEvents.add(new KeyboardInputEvent(newKeyCode, e.getKeyChar(), isKeyDown, e
                .isShiftDown(), e.isControlDown()));
    }

    AwtToNiftyKeyCodeConverter keyCodeConverter = new AwtToNiftyKeyCodeConverter();

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void setMousePosition(int x, int y) {
        // TODO implement this method later
    }

}

@@ -417,62 +417,8 @@
 nt;%0A
-import de.lessvoid.nifty.input.mouse.MouseInputEvent;%0A
 impo
@@ -651,26 +651,25 @@
 ue%3CMouse
-Input
 Event
+Data
 %3E mouseE
@@ -707,26 +707,25 @@
 ue%3CMouse
-Input
 Event
+Data
 %3E();%0A%0A  
@@ -944,25 +944,22 @@
     
-  
 Mouse
-Input
 Event
+Data
  mou
@@ -1041,55 +1041,56 @@
     
-inputEventConsumer.processMouseEvent(mouseEvent
+mouseEvent.processMouseEvents(inputEventConsumer
 );%0A 
@@ -1655,34 +1655,33 @@
 dd(new Mouse
-Input
 Event
+Data
 (mouseEvent.
@@ -1703,32 +1703,35 @@
 ent.getY(), true
+, 0
 ));%0A        %7D%0A  
@@ -1950,34 +1950,33 @@
 dd(new Mouse
-Input
 Event
+Data
 (mouseEvent.
@@ -1998,32 +1998,35 @@
 ent.getY(), true
+, 0
 ));%0A        %7D%0A  
@@ -2474,34 +2474,33 @@
 dd(new Mouse
-Input
 Event
+Data
 (mouseEvent.
@@ -2530,16 +2530,19 @@
 (), true
+, 0
 ));%0A    
@@ -2776,26 +2776,25 @@
 ew Mouse
-Input
 Event
+Data
 (mouseEv
@@ -2829,16 +2829,19 @@
 ), false
+, 0
 ));%0A    
@@ -3622,10 +3622,664 @@
 %0A    %7D%0A%0A
+    private class MouseEventData %7B%0A      private int mouseX;%0A      private int mouseY;%0A      private int mouseWheel;%0A      private int button;%0A      private boolean buttonDown;%0A%0A      public MouseEventData(final int mouseX, final int mouseY, final boolean mouseDown, final int mouseButton) %7B%0A        this.mouseX = mouseX;%0A        this.mouseY = mouseY;%0A        this.buttonDown = mouseDown;%0A        this.button = mouseButton;%0A        this.mouseWheel = 0;%0A      %7D%0A%0A      public void processMouseEvents(final NiftyInputConsumer inputEventConsumer) %7B%0A        inputEventConsumer.processMouseEvent(mouseX, mouseY, mouseWheel, button, buttonDown);%0A      %7D%0A    %7D%0A
 %7D%0A

make that compatible with the latest Nifty mouse handling changes so that it at least compiles again - someone smarter with JOGL/AWT should add support for more mouse buttons (julien? :D)