package com.softuni.earth.listeners;

import java.util.List;

import com.softuni.earth.GameWorldInitializer;
import com.softuni.earth.base.GameObject;
import com.softuni.earth.base.GameObjectManager;
import com.softuni.earth.base.objects.Player;

import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

/**
 * Listener that should be set to the scene in order to update the objects when
 * specific key events occur. For example the main character ( the player)
 * should move right when the user clicks the right arrow. This listener is
 * responsible for setting the position of the character which is then visually
 * updated by the scene itself.
 * 
 * @author marto
 *
 */
public class SceneKeyPressedListener implements EventHandler<KeyEvent> {

	private GameObject player;

	public SceneKeyPressedListener(GameWorldInitializer gameWorldInitializer) {
		GameObjectManager gameObjectManager = gameWorldInitializer
				.getGameObjectManager();
		List<GameObject> allObjects = gameObjectManager.getAllObjects();
		// This should be the player.
		player = allObjects.get(0);
	}

	public void handle(final KeyEvent event) {
		KeyCode code = event.getCode();

		if (code.equals(KeyCode.RIGHT)) {
			moveRight();
		} else if (code.equals(KeyCode.LEFT)) {
			moveLeft();
		} else if (code.equals(KeyCode.UP)) {
			moveUp();
		} else if (code.equals(KeyCode.DOWN)) {
			moveDown();
		}

		event.consume();
	}

	private void moveDown() {
		if (player instanceof Player) {
			Node playerNode = player.getNode();
			double translateY = playerNode.getTranslateY();

			playerNode.setTranslateY(translateY + 20f);
		}
	}

	private void moveUp() {
		if (player instanceof Player) {
			Node playerNode = player.getNode();
			double translateY = playerNode.getTranslateY();

			playerNode.setTranslateY(translateY - 20f);
		}
	}

	private void moveLeft() {
		if (player instanceof Player) {
			Node playerNode = player.getNode();
			double translateX = playerNode.getTranslateX();

			playerNode.setTranslateX(translateX - 20f);
		}
	}

	private void moveRight() {
		if (player instanceof Player) {
			Node playerNode = player.getNode();
			double translateX = playerNode.getTranslateX();

			playerNode.setTranslateX(translateX + 20f);
		}
	}
}

@@ -245,24 +245,70 @@
 .Player;%0D%0A%0D%0A
+import javafx.animation.TranslateTransition;%0D%0A
 import javaf
@@ -429,16 +429,46 @@
 yEvent;%0D
+%0Aimport javafx.util.Duration;%0D
 %0A%0D%0A/**%0D%0A
@@ -1699,32 +1699,34 @@
 yer.getNode();%0D%0A
+//
 %09%09%09double transl
@@ -1770,49 +1770,185 @@
 %0A%09%09%09
-playerNode.setTranslateY(translateY + 20f
+TranslateTransition smoothMove = new TranslateTransition(Duration.millis(200), playerNode);%0D%0A%09%09%09smoothMove.setByY(20f);%0D%0A%09%09%09smoothMove.setAutoReverse(true);%0D%0A%09%09%09smoothMove.play(
 );%0D%0A
@@ -2051,32 +2051,34 @@
 yer.getNode();%0D%0A
+//
 %09%09%09double transl
@@ -2122,49 +2122,186 @@
 %0A%09%09%09
-playerNode.setTranslateY(translateY - 20f
+TranslateTransition smoothMove = new TranslateTransition(Duration.millis(200), playerNode);%0D%0A%09%09%09smoothMove.setByY(-20f);%0D%0A%09%09%09smoothMove.setAutoReverse(true);%0D%0A%09%09%09smoothMove.play(
 );%0D%0A
@@ -2406,32 +2406,34 @@
 yer.getNode();%0D%0A
+//
 %09%09%09double transl
@@ -2477,49 +2477,186 @@
 %0A%09%09%09
-playerNode.setTranslateX(translateX - 20f
+TranslateTransition smoothMove = new TranslateTransition(Duration.millis(200), playerNode);%0D%0A%09%09%09smoothMove.setByX(-20f);%0D%0A%09%09%09smoothMove.setAutoReverse(true);%0D%0A%09%09%09smoothMove.play(
 );%0D%0A
@@ -2770,16 +2770,18 @@
 ode();%0D%0A
+//
 %09%09%09doubl
@@ -2829,16 +2829,18 @@
 ;%0D%0A%0D%0A%09%09%09
+//
 playerNo
@@ -2872,24 +2872,208 @@
 eX + 20f);%0D%0A
+%09%09%09TranslateTransition smoothMove = new TranslateTransition(Duration.millis(200), playerNode);%0D%0A%09%09%09smoothMove.setByX(20f);%0D%0A%09%09%09smoothMove.setAutoReverse(true);%0D%0A%09%09%09smoothMove.play();%0D%0A
 %09%09%7D%0D%0A%09%7D%0D%0A%7D%0D%0A

Add smooth move