package player;

import java.util.ConcurrentModificationException;

import engine.Camera;
import engine.Handler;
import utils.Point2f;
import utils.Utils;
import world.Boulder;
import world.DroppedItem;
import world.Flower;
import world.Sapling;
import world.TallGrass;
import world.Tree;

/*  ------------- Nihilne ------------
    * Created by Mitch Weaver - 2016 *
      ~~~~ All Rights Reserved ~~~~ */

final class PlayerCollision {

	private static float diffX, diffY;
			
	private static final boolean calcCollision(boolean stopPlayer, Point2f p, float r) {
		if (Utils.intersects(Player.center, Player.radius, p, r)) {
			diffX = Utils.abs(Player.center.x - p.x);
			diffY = Utils.abs(Player.center.y - p.y);
			 
			if (Player.center.x > p.x) {
				if (stopPlayer) {
					Player.pos.x += diffX/20;
					Player.velX = 0;
				}
			} else if (Player.center.x < p.x) {
				if (stopPlayer) {
					Player.pos.x -= diffX/20;
					Player.velX = 0;
				}
			}
			if (Player.center.y > p.y) {
				if (stopPlayer) {
					Player.pos.y += diffY/20;
					Player.velY = 0;
				}
			} else if (Player.center.y < p.y) {
				if (stopPlayer) {
					Player.pos.y -= diffY/20;
					Player.velY = 0;
				}
			}
			if(stopPlayer) Camera.tick();
			return true;
		}
		return false;
	}

	public static final boolean collision() {
		try {
			
		// PLAYER COLLIDED's
		for (final Flower i : Handler.flowers) if (i.alive && calcCollision(false, i.center, i.radius)){ i.playerCollided = Player.flowerCollided = true; break; } else i.playerCollided = Player.flowerCollided = false;
		for (final TallGrass i : Handler.grass) if (i.alive && calcCollision(false, i.center, i.radius)) { i.playerCollided = Player.grassCollided = true; break; } else i.playerCollided = Player.grassCollided = false;
		for (final Sapling i : Handler.saplings) if(calcCollision(false, i.center, i.radius)){ i.playerCollided = Player.saplingCollided = true; break;} else i.playerCollided = Player.saplingCollided = false;
		for (final DroppedItem i : Handler.droppedItems) if (calcCollision(false, i.center, i.radius)){ i.playerCollided = Player.droppedItemCollided = true; break;} else i.playerCollided = Player.droppedItemCollided = false;
		for (final Tree i : Handler.trees) if (i.alive && calcCollision(false, i.center, i.chopRadius)) { i.playerCollided = Player.treeCollided = true; break; } else i.playerCollided = Player.treeCollided = false;
		for (final Boulder i : Handler.boulders) if (i.alive && calcCollision(false, i.center, i.mineRadius)) { i.playerCollided = Player.boulderCollided = true; break; } else i.playerCollided = Player.boulderCollided = false;
		
		// SOLID OBJECTS
		for (final Tree i : Handler.trees) if (i.alive && calcCollision(true, i.center, i.radius)) return true;
		for (final Boulder i : Handler.boulders) if (i.alive && calcCollision(true, i.center, i.radius)) return true;
		
		} catch (ConcurrentModificationException cme ) {}
		
		return false; /* if no solid objects have been hit and returned true, return false */
	}
	
}
