package de.fe1k.game9.components;

import de.fe1k.game9.entities.Entity;
import de.fe1k.game9.events.*;
import de.fe1k.game9.utils.Direction;
import de.nerogar.noise.input.InputHandler;
import de.nerogar.noise.input.KeyboardKeyEvent;
import de.nerogar.noise.util.Logger;
import org.lwjgl.glfw.GLFW;

@Depends(components = { ComponentMoving.class })
public class ComponentControllable extends Component {

	private InputHandler inputs;
	private float   jumpPower  = 0;
	private boolean wasKeyDown = false;

	private int   moveDirection       = 1;
	private float targetMoveDirection = moveDirection;

	private EventListener<EventUpdate>    eventUpdate;
	private EventListener<EventCollision> eventCollision;

	public ComponentControllable(InputHandler inputHandler) {
		this.inputs = inputHandler;
		eventUpdate = this::update;
		eventCollision = this::collision;
		Event.register(EventUpdate.class, eventUpdate);
		Event.register(EventCollision.class, eventCollision);
	}

	private void update(EventUpdate event) {
		ComponentMoving moving = getOwner().getComponent(ComponentMoving.class);
		boolean isKeyDown = inputs.isKeyDown(' ');
		boolean onGround = moving.touching[Direction.DOWN.val];

		if (onGround) {
			jumpPower = 1;
		}

		if (isKeyDown && !(onGround && wasKeyDown)) {
			moving.velocity.add(moving.gravity.multiplied(-0.09f * jumpPower));
			jumpPower *= 1 - (10 * event.deltaTime);
		}

		moving.velocity.setX(10f * moveDirection);
		wasKeyDown = isKeyDown;

		for (KeyboardKeyEvent keyboardKeyEvent : inputs.getKeyboardKeyEvents()) {
			if (keyboardKeyEvent.action == GLFW.GLFW_PRESS && keyboardKeyEvent.key == GLFW.GLFW_KEY_ENTER) {
				moveDirection *= -1;
			}
		}
		if (targetMoveDirection < moveDirection) targetMoveDirection += 10 * event.deltaTime;
		if (targetMoveDirection > moveDirection) targetMoveDirection -= 10 * event.deltaTime;
		getOwner().getScale().setX(targetMoveDirection);

	}

	private void collision(EventCollision event) {
		if (!event.movingComponent.getOwner().equals(getOwner())) {
			return;
		}
		if (event.collisionDirection.isHorizontal()) {
			// death
			Event.trigger(new EventEntityDestroyed(getOwner()));
			resetPosition();
		}
	}

	public void resetPosition() {
		ComponentStartMarker start = Entity.getFirstComponent(ComponentStartMarker.class);
		if (start == null) {
			Logger.getErrorStream().printf("No start marker found, can't teleport to start!");
			return;
		}
		getOwner().teleport(start.getOwner().getPosition());
	}

	@Override
	public void destroy() {
		Event.unregister(EventUpdate.class, eventUpdate);
		Event.unregister(EventCollision.class, eventCollision);
	}

}

@@ -514,17 +514,23 @@
 e int   
-m
+targetM
 oveDirec
@@ -534,22 +534,16 @@
 rection 
-      
 = 1;%0A%09pr
@@ -554,23 +554,17 @@
 e float 
-targetM
+m
 oveDirec
@@ -568,19 +568,31 @@
 rection 
-= m
+      = targetM
 oveDirec
@@ -1653,17 +1653,23 @@
 ) %7B%0A%09%09%09%09
-m
+targetM
 oveDirec
@@ -1691,31 +1691,25 @@
 %7D%0A%09%09%7D%0A%09%09if (
-targetM
+m
 oveDirection
@@ -1711,17 +1711,23 @@
 ction %3C 
-m
+targetM
 oveDirec
@@ -1724,39 +1724,33 @@
 tMoveDirection) 
-targetM
+m
 oveDirection += 
@@ -1777,23 +1777,17 @@
 ;%0A%09%09if (
-targetM
+m
 oveDirec
@@ -1793,17 +1793,23 @@
 ction %3E 
-m
+targetM
 oveDirec
@@ -1814,23 +1814,17 @@
 ection) 
-targetM
+m
 oveDirec
@@ -1882,23 +1882,17 @@
 ().setX(
-targetM
+m
 oveDirec

fix moveDirection and targetMoveDirection being switched around