package com.kangacult.octogone;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.controllers.PovDirection;
import com.badlogic.gdx.controllers.mappings.Xbox;

public class InputPoller {
	public int cursorX = 0;
	public int cursorY = 0;
	public int cursorXPrev = 0;
	public int cursorYPrev = 0;

	public float deltaX = 0;
	public float deltaY = 0;

	public float movementAxis = 0f;
	public float strafingAxis = 0f;

	public boolean fire = false;
	public boolean draw = false;
	public boolean jump = false;
	public boolean isJumping = false;

	//Controller Input
	Gamepad gpad;
	public boolean leftTrigger;
	public boolean rightTrigger;

	public void init() {

		if (GamePrefs.instance.gamePadEnable) {
			gpad = new Gamepad();
			gpad.init();
			leftTrigger = false;
			rightTrigger = false;
		} else {
			gpad = null;
		}

	}

	public void poll() {
		cursorXPrev = cursorX;
		cursorYPrev = cursorY;
		cursorX = Gdx.input.getX() - Gdx.graphics.getWidth() / 2;
		cursorY = Gdx.graphics.getHeight() - Gdx.input.getY() - Gdx.graphics.getHeight() / 2;

		deltaX = (float) cursorX - (float) cursorXPrev;
		deltaY = (float) cursorY - (float) cursorYPrev;

		movementAxis = 0f;
		strafingAxis = 0f;
		if (Gdx.input.isKeyPressed(Keys.W))
			movementAxis += 1f;
		if (Gdx.input.isKeyPressed(Keys.A))
			strafingAxis -= 1f;
		if (Gdx.input.isKeyPressed(Keys.S))
			movementAxis -= 1f;
		if (Gdx.input.isKeyPressed(Keys.D))
			strafingAxis += 1f;

		fire = Gdx.input.isButtonPressed(Buttons.LEFT);
		draw = Gdx.input.isButtonPressed(Buttons.RIGHT);
		jump = Gdx.input.isKeyJustPressed(Keys.SPACE);

		if (gpad != null && gpad.pad != null) {
			pollXbox();
		}

		if (jump) {
			//TODO: Jump Code
			//System.out.println("jump");
		}
	}

	public void pollXbox() {
		//Left ThumbStick

		//LEFT/RIGHT
		float xAxis = gpad.pad.getAxis(Xbox.L_STICK_HORIZONTAL_AXIS);
		if (xAxis <= -0.45f) {
			strafingAxis -= 1f;
		} else if (xAxis >= 0.45f) {
			strafingAxis += 1f;
		}

		//UP/DOWN
		float yAxis = gpad.pad.getAxis(Xbox.L_STICK_VERTICAL_AXIS);
		if (yAxis <= -0.45f) {
			movementAxis += 1f;
		} else if (yAxis >= 0.45f) {
			movementAxis -= 1f;
		}

		//Right ThumbStick
		float yAxisRStick = gpad.pad.getAxis(Xbox.R_STICK_HORIZONTAL_AXIS);

		if (yAxisRStick > 0.05f || yAxisRStick < -0.05f) {
			deltaX = yAxisRStick * 37;
		}

		//Triggers
		if (!leftTrigger) {
			if (gpad.pad.getAxis(Xbox.L_TRIGGER) > 0.99f) {
				leftTrigger = true;
				//System.out.println("draw!");
				draw = true;
			}
		}
		if (gpad.pad.getAxis(Xbox.L_TRIGGER) <= 0.99f) {
			leftTrigger = false;
		}

		if (!rightTrigger) {
			if (gpad.pad.getAxis(Xbox.R_TRIGGER) > 0.99f) {
				rightTrigger = true;
				//System.out.println("fire!");
				fire = true;
			}
		}
		if (gpad.pad.getAxis(Xbox.R_TRIGGER) <= 0.99f) {
			rightTrigger = false;
		}

		//Buttons
		if (gpad.pad.getButton(Xbox.A)) {
			// Do something
			if (!isJumping) {
				jump = true;
				isJumping = true;
			}
		} else {
			isJumping = false;
		}

		if (gpad.pad.getButton(Xbox.B)) {
			// Do something
		}
		if (gpad.pad.getButton(Xbox.X)) {
			// Do something
		}
		if (gpad.pad.getButton(Xbox.Y)) {
			// Do something
		}

		//Start Etc
		if (gpad.pad.getButton(Xbox.START)) {
			// Do something
		}
		if (gpad.pad.getButton(Xbox.BACK)) {
			// Do something
		}

		// SHOULDER BUTTONS
		if (gpad.pad.getButton(Xbox.R_BUMPER)) {
			// Do something
		}
		if (gpad.pad.getButton(Xbox.L_BUMPER)) {
			// Do something
		}

		// Dpad
		if (gpad.pad.getPov(0) == PovDirection.center) {
			// Do something
		}
		if (gpad.pad.getPov(0) == PovDirection.north) {
			// Do something
		}
		if (gpad.pad.getPov(0) == PovDirection.south) {
			// Do something
		}
		if (gpad.pad.getPov(0) == PovDirection.west) {
			// Do something
		}
		if (gpad.pad.getPov(0) == PovDirection.east) {
			// Do something
		}
		if (gpad.pad.getPov(0) == PovDirection.northEast) {
			// Do something
		}
		if (gpad.pad.getPov(0) == PovDirection.southEast) {
			// Do something
		}
		if (gpad.pad.getPov(0) == PovDirection.northWest) {
			// Do something
		}
		if (gpad.pad.getPov(0) == PovDirection.southWest) {
			// Do something
		}
	}

	public boolean keypressed() {
		// Controller
		if (gpad != null && gpad.pad != null) {
			// Buttons
			if ((gpad.pad.getButton(Xbox.A))
					|| (gpad.pad.getButton(Xbox.B)) || (gpad.pad.getButton(Xbox.X))
					|| (gpad.pad.getButton(Xbox.Y)) || (gpad.pad.getButton(Xbox.START))) {
				return true;
			}
		}
		// Keyboard
		if (Gdx.input.isKeyJustPressed(Keys.ANY_KEY)) {
			return true;
		}
		return false;
	}

	public boolean reset() {
		// Controller
		if (gpad != null && gpad.pad != null) {
			// Buttons
			if ((gpad.pad.getButton(Xbox.BACK))) {
				return true;
			}
		}
		// Keyboard
		if (Gdx.input.isKeyJustPressed(Keys.R)) {
			return true;
		}
		return false;
	}
}
