package de.samdev.colorrunner.input;

import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.input.GestureDetector.GestureListener;
import com.badlogic.gdx.math.Vector2;

import de.samdev.colorrunner.CRGame;
import de.samdev.colorrunner.game.world.SwipeDirection;

public class CRGameInputProcessor implements InputProcessor, GestureListener {
	private GameInputListener owner;

	public CRGameInputProcessor(GameInputListener gsc) {
		this.owner = gsc;
	}

	public void update() {

	}
	
	@Override
	public boolean keyDown(int keycode) {
		if (keycode == Input.Keys.UP) {
			owner.switchColor(SwipeDirection.UP);
			return true;
		}
		
		if (keycode == Input.Keys.RIGHT) {
			owner.switchColor(SwipeDirection.RIGHT);
			return true;
		}
		
		if (keycode == Input.Keys.DOWN) {
			owner.switchColor(SwipeDirection.DOWN);
			return true;
		}
		
		if (keycode == Input.Keys.LEFT) {
			owner.switchColor(SwipeDirection.LEFT);
			return true;
		}
		
		if (keycode == Input.Keys.SPACE) {
			owner.doJump();
			return true;
		}

		
		if (keycode == Input.Keys.R && CRGame.DEBUG) {
			owner.reset();
			return true;
		}
		
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		return false;
	}

	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		owner.doJump();
		return false;
	}

	@Override
	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
		return false;
	}

	@Override
	public boolean touchDragged(int screenX, int screenY, int pointer) {
		return false;
	}

	@Override
	public boolean mouseMoved(int screenX, int screenY) {
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		return false;
	}

	@Override
	public boolean touchDown(float x, float y, int pointer, int button) {
		return false;
	}

	@Override
	public boolean tap(float x, float y, int count, int button) {
		return false;
	}

	@Override
	public boolean longPress(float x, float y) {
		return false;
	}

	@Override
	public boolean fling(float velocityX, float velocityY, int button) {
		double angle = Math.atan2(velocityX, velocityY) + Math.PI;

		if (angle < Math.PI * 1/4.0) {
			owner.switchColor(SwipeDirection.UP);
			return true;
		} else if (angle < Math.PI * 3/4.0) {
			owner.switchColor(SwipeDirection.LEFT);
			return true;
		} else if (angle < Math.PI * 5/4.0) {
			owner.switchColor(SwipeDirection.DOWN);
			return true;
		} else if (angle < Math.PI * 7/4.0) {
			owner.switchColor(SwipeDirection.RIGHT);
			return true;
		} else {
			owner.switchColor(SwipeDirection.UP);
			return true;
		}
	}

	@Override
	public boolean pan(float x, float y, float deltaX, float deltaY) {
		// NOP
		return false;
	}

	@Override
	public boolean panStop(float x, float y, int pointer, int button) {
		// NOP
		return false;
	}

	@Override
	public boolean zoom(float initialDistance, float distance) {
		// NOP
		return false;
	}

	@Override
	public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
		// NOP
		return false;
	}

	@Override
	public void pinchStop() {
		// NOP
	}

}
