package de.samdev.colorrunner.game.world.entities.gameentities;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;

import de.samdev.colorrunner.game.renderer.CRAssets;
import de.samdev.colorrunner.game.world.CRGameWorld;
import de.samdev.colorrunner.game.world.SwipeDirection;
import de.samdev.colorrunner.game.world.entities.CRGameEntity;
import de.samdev.colorrunner.game.world.entities.GravityEntity;
import de.samdev.colorrunner.game.world.entities.gameentities.floor.FloorTileEntity;
import de.samdev.colorrunner.screens.menu.MainMenu;

public class PlayerEntity extends GravityEntity {
	public final static float PLAYER_WIDTH = 32;
	public final static float PLAYER_HEIGHT = 32;

	public final static float PLAYER_JUMP_FORCE = 356f;
	public final static float PLAYER_FLY_FORCE = 250f;

	public final static float PLAYER_SPEED_FORCE = 166f;
	public final static float PLAYER_TERMINAL_SPEED = 320f;

	public final static float PLAYER_ROTATION_SPEED = -200f;

	private SwipeDirection phase = SwipeDirection.UP;

	private boolean doFly = false;

	public PlayerEntity(CRGameWorld _owner, float x, float y) {
		super(_owner, x, y, PLAYER_WIDTH, PLAYER_HEIGHT);
	}

	@Override
	public void update(float delta) {
		if (velocity.x < PLAYER_TERMINAL_SPEED)
			velocity.x += PLAYER_SPEED_FORCE * delta;

		if (doFly)
			velocity.y += PLAYER_FLY_FORCE * delta;
		doFly = false;

		updateRotation(delta);

		super.update(delta);
	}

	private void updateRotation(float delta) {
		if (!isTouching_BOTTOM() || visualRotation % 90 != 0) {
			if (! isTouching_BOTTOM()) {
				visualRotation = visualRotation + PLAYER_ROTATION_SPEED * delta;
				visualRotation = (visualRotation + 720f) % 360f;
				
				float p = (float) ((Math.max(Math.abs(Math.cos(Math.toRadians(visualRotation + 45))), Math.abs(Math.sin(Math.toRadians(visualRotation + 45)))) * Math.sqrt(PLAYER_WIDTH * PLAYER_WIDTH + PLAYER_HEIGHT * PLAYER_HEIGHT)));
				updateHitBox(p, p);
			} else {
				visualRotation = (visualRotation + 720f) % 90f;
				
				if (visualRotation < 45)
					visualRotation = visualRotation + PLAYER_ROTATION_SPEED * delta;
				else
					visualRotation = visualRotation - PLAYER_ROTATION_SPEED * delta;
				
				if (visualRotation >= 90f || visualRotation <= 0f)
					visualRotation = 0f;
				
				visualRotation = (visualRotation + 720f) % 90f;
				
				float p = (float) ((Math.max(Math.abs(Math.cos(Math.toRadians(visualRotation + 45))), Math.abs(Math.sin(Math.toRadians(visualRotation + 45)))) * Math.sqrt(PLAYER_WIDTH * PLAYER_WIDTH + PLAYER_HEIGHT * PLAYER_HEIGHT)));
				updateHitBox(p, p);
			}
		}
	}

	public void jump() {
		if (isTouching_BOTTOM())
			velocity.y = PLAYER_JUMP_FORCE;
	}

	public void switchPhase(SwipeDirection sd) {
		if (phase == sd) return;
		phase = sd;
		
		for (CRGameEntity ent : world.entities) {
			if (ent == this) continue;
			
			if (ent.bounds.overlaps(bounds) && ent.canCollide(false, this) && ent instanceof FloorTileEntity)
				ent.remove();
				
		}
	}

	public void fly() {
		if (!isTouching_BOTTOM())
			doFly = true;
	}

	@Override
	public Texture getTexture() {
		switch (phase) {
		case UP:
			return CRAssets.TEX_PLAYER_UP;
		case RIGHT:
			return CRAssets.TEX_PLAYER_RIGHT;
		case DOWN:
			return CRAssets.TEX_PLAYER_DOWN;
		case LEFT:
			return CRAssets.TEX_PLAYER_LEFT;

		default:
			return null;
		}
	}

	@Override
	public boolean canCollide(boolean actualCollision, CRGameEntity collider) {
		return collider.canCollide(actualCollision, this);
	}

	public SwipeDirection getPhase() {
		return phase;
	}
}
