package de.heatup.mapengine;

import java.awt.Point;
import java.util.ArrayList;

import de.heatup.logging.Logger;
import de.heatup.objects.Player;
import de.heatup.objects.PowerUp;
import de.heatup.objects.PowerUpHandler;

public class CollisionHandler {
	private static ArrayList<Player> movingObjects = new ArrayList<Player>();
	private static ArrayList<PowerUp> powerUpObjects = new ArrayList<PowerUp>();
	/**
	 * Prüfe ob eine Kollision vorliegt
	 * @param movingObject Kann alles sein, dass sich bewegt. Spieler oder KI.
	 * @param d Richtung in die sich bewegt werden soll. Möglichkeiten: l, r, u, d
	 * @return
	 */
	public static boolean checkCollision(Player movingObject, char d) {
		if (movingObject == (movingObjects.get(0))) {
			hardwareCollision(movingObject, d);
			return normalCheck(movingObject, d);
		} else {
			return normalCheck(movingObject, d);
		}
	}
	
	public static boolean checkCollision(Point startPosition, char d, int steps) {
		boolean isPath = true;
		for (int counter = 1; counter <= steps; counter++) {
			switch (d) {
				case 'l':
					isPath = MapGrid.getGridCell(startPosition.x-counter, startPosition.y).isPath();
					break;
				case 'r':
					isPath = MapGrid.getGridCell(startPosition.x+counter, startPosition.y).isPath();
					break;
				case 'd':
					isPath = MapGrid.getGridCell(startPosition.x, startPosition.y+counter).isPath();
					break;
				case 'u':
					isPath = MapGrid.getGridCell(startPosition.x, startPosition.y-counter).isPath();
					break;
			}
			if ( ! isPath ) {
				break;
			}
		}
		return isPath;
	}
	public static void tick() {
		for (int i=1; i<movingObjects.size();i++) {
			if (movingObjects.get(0).getGridLocation().x== movingObjects.get(i).getGridLocation().x && movingObjects.get(0).getGridLocation().y == movingObjects.get(i).getGridLocation().y) {
//				Logger.write("COLLISION WITH ENEMY");
			}
		}
	}
	private static void hardwareCollision(Player movingObject, char d) {
		switch (d) {
		case 'l':
			if (MapGrid.getGridCell(movingObject.getGridLocation().x-1, movingObject.getGridLocation().y).isPath() && MapGrid.getGridCell(movingObject.getGridLocation().x-1, movingObject.getGridLocation().y).isTrigger()) {
//				Logger.write("Collision with trigger of hardware ID: " + MapGrid.getGridCell(movingObject.getGridLocation().x-1, movingObject.getGridLocation().y).getCorrespondingHardwareToTrigger());
			}
		case 'r':
			if (MapGrid.getGridCell(movingObject.getGridLocation().x+1, movingObject.getGridLocation().y).isPath() && MapGrid.getGridCell(movingObject.getGridLocation().x+1, movingObject.getGridLocation().y).isTrigger()) {
//				Logger.write("Collision with trigger of hardware ID: " + MapGrid.getGridCell(movingObject.getGridLocation().x+1, movingObject.getGridLocation().y).getCorrespondingHardwareToTrigger());
			}
		case 'd':
			if (MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y+1).isPath() && MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y+1).isTrigger()) {
//				Logger.write("Collision with trigger of hardware ID: " + MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y+1).getCorrespondingHardwareToTrigger());
			}
		case 'u':
			if (MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y-1).isPath() && MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y-1).isTrigger()) {
//				Logger.write("Collision with trigger of hardware ID: " + MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y-1).getCorrespondingHardwareToTrigger());
			}
		default:
			
		}
	}
	private static boolean normalCheck(Player movingObject, char d) {
		switch (d) {
		case 'l':
			if (PowerUpHandler.getPowerUpGridLocation().x == movingObject.getGridLocation().x-1 && PowerUpHandler.getPowerUpGridLocation().y == movingObject.getGridLocation().y) {
				PowerUpHandler.getPowerUp().trigger(movingObject == (movingObjects.get(0)));
			}
			return MapGrid.getGridCell(movingObject.getGridLocation().x-1, movingObject.getGridLocation().y).isPath();
		case 'r':
			if (PowerUpHandler.getPowerUpGridLocation().x == movingObject.getGridLocation().x+1 && PowerUpHandler.getPowerUpGridLocation().y == movingObject.getGridLocation().y) {
				PowerUpHandler.getPowerUp().trigger(movingObject == (movingObjects.get(0)));
			}
			return MapGrid.getGridCell(movingObject.getGridLocation().x+1, movingObject.getGridLocation().y).isPath();
		case 'd':
			if (PowerUpHandler.getPowerUpGridLocation().x == movingObject.getGridLocation().x && PowerUpHandler.getPowerUpGridLocation().y == movingObject.getGridLocation().y+1) {
				PowerUpHandler.getPowerUp().trigger(movingObject == (movingObjects.get(0)));
			}
			return MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y+1).isPath();
		case 'u':
			if (PowerUpHandler.getPowerUpGridLocation().x == movingObject.getGridLocation().x && PowerUpHandler.getPowerUpGridLocation().y == movingObject.getGridLocation().y-1) {
				PowerUpHandler.getPowerUp().trigger(movingObject == (movingObjects.get(0)));
			}
			return MapGrid.getGridCell(movingObject.getGridLocation().x, movingObject.getGridLocation().y-1).isPath();
		default:
			return true; // should not happen
		}
	}
	public static void addMovingObject(Player movingObject) {
		movingObjects.add(movingObject);
	}
	public static void addPowerUpObject(PowerUp powerUp) {
		powerUpObjects.add(powerUp);
	}
}
