package com.lostcodestudios.gnoPortal.gameplay.entities.systems;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.math.Vector2;
import com.lostcode.javalib.entities.Entity;
import com.lostcode.javalib.entities.components.physical.Body;
import com.lostcode.javalib.entities.components.physical.Particle;
import com.lostcodestudios.gnoPortal.gameplay.PongWorld;
import com.lostcodestudios.gnoPortal.gameplay.entities.templates.BallTemplate;

public class InputSystem extends com.lostcode.javalib.entities.systems.InputSystem {

	Vector2 leftPaddleVelocity = new Vector2();
	Vector2 crosshairPosition = new Vector2();
	public Entity Ball;
	boolean ballfired =false;
	
	public InputSystem(InputMultiplexer input, Entity ball) {
		super(input);
		this.Ball = ball;
	}

	@Override
	protected void process(Entity e) {
		if (e.getTag().equals("paddleleft")) {
			leftPaddleVelocity.scl(PongWorld.PADDLE_SPEED);
			((Body) e.getComponent(Body.class)).setLinearVelocity(leftPaddleVelocity);
		}
		
		else if (e.getTag().equals("crosshair")) {
			((Particle) e.getComponent(Particle.class)).setPosition(crosshairPosition);
		}
		
		
		super.process(e);
	}

	@Override
	public boolean canProcess(Entity e) {
		return e.getTag().equals("paddleleft") || e.getTag().equals("crosshair");
	}

	@Override
	public boolean keyDown(int keycode) {
		if (keycode == Keys.F1){
			((PongWorld)world).enableDebug();
			return true;
		}
		
		if (keycode == Keys.W)
		{
			leftPaddleVelocity.y = 1;
			return true;
		}
		else if (keycode == Keys.S)
		{
			leftPaddleVelocity.y = -1;
			return true;
		}
		

		
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		if (keycode == Keys.W || keycode == Keys.S)
		{
			leftPaddleVelocity.y = 0;
			return true;
		}
		
		return super.keyUp(keycode);
	}

	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		if(!ballfired){
			Vector2 pos = world.toWorldCoordinates(new Vector2(screenX, screenY));
			
			Body bb = Ball.getComponent(Body.class);
			
			pos.sub(bb.getPosition());
			pos.nor();
			
			bb.setLinearVelocity(pos.cpy().scl(BallTemplate.VELOCITY));
			ballfired = true;
			return true;
		}
		else {
		

			Vector2 pos = world.toWorldCoordinates(new Vector2(screenX, screenY));
			
			if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)){
				// left click?
				
				Entity e = world.tryGetEntity("blue", "player", "portal");
				
				if (e != null)
					e.delete();
				
				world.createEntity("portal", "player", pos, "blue");
			}
			if(Gdx.input.isButtonPressed(Input.Buttons.RIGHT)){
				// right click?
				
				Entity e = world.tryGetEntity("orange", "player", "portal");
				
				if (e != null)
					e.delete();
				
				world.createEntity("portal", "player", pos, "orange");
			}
			
			return false;	
		
		}
	}
	@Override
	public boolean mouseMoved(int screenX, int screenY) {
		Vector2 pos = world.toWorldCoordinates(new Vector2(screenX, screenY));
		
		if (world.getBounds().contains(pos)) {
			crosshairPosition = pos.cpy();
		}
		
		return true;
	}


}

@@ -2436,37 +2436,32 @@
 %09// left click?%0A
-%09%09%09%09%0A
 %09%09%09%09Entity e = w
@@ -2450,32 +2450,62 @@
 ?%0A%09%09%09%09Entity e =
+ null;%0A%09%09%09%09%0A%09%09%09%09do %7B%0A%09%09%09%09%09 e =
  world.tryGetEnt
@@ -2538,32 +2538,33 @@
 tal%22);%0A%09%09%09%09%0A%09%09%09%09
+%09
 if (e != null)%0A%09
@@ -2554,32 +2554,33 @@
 %09if (e != null)%0A
+%09
 %09%09%09%09%09e.delete();
@@ -2572,32 +2572,107 @@
 %09%09%09%09e.delete();%0A
+%09%09%09%09%09else%0A%09%09%09%09%09%09System.out.println(%22oops%22);%0A%09%09%09%09%7D while (e != null);%09%0A%09%09%09%09%0A
 %09%09%09%09%0A%09%09%09%09world.c
@@ -2812,24 +2812,54 @@
 %09%09Entity e =
+ null;%0A%09%09%09%09%0A%09%09%09%09do %7B%0A%09%09%09%09%09 e =
  world.tryGe
@@ -2902,24 +2902,25 @@
 );%0A%09%09%09%09%0A%09%09%09%09
+%09
 if (e != nul
@@ -2923,24 +2923,25 @@
  null)%0A%09%09%09%09%09
+%09
 e.delete();%0A
@@ -2936,24 +2936,94 @@
 e.delete();%0A
+%09%09%09%09%09else%0A%09%09%09%09%09%09System.out.println(%22oops%22);%0A%09%09%09%09%7D while (e != null);%09%0A
 %09%09%09%09%0A%09%09%09%09wor

debug portal duplication deletion