package com.ibm.sk.engine;

import static com.ibm.sk.Main.getTurn;
import static com.ibm.sk.engine.World.getWorldObjects;

import java.awt.Point;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;

import com.ibm.sk.WorldConstans;
import com.ibm.sk.dto.AbstractAnt;
import com.ibm.sk.dto.Hill;
import com.ibm.sk.dto.IAnt;
import com.ibm.sk.dto.Step;
import com.ibm.sk.dto.Vision;
import com.ibm.sk.dto.enums.Direction;
import com.ibm.sk.engine.exceptions.MoveException;
import com.ibm.sk.ff.gui.common.objects.operations.CreateGameData;

public final class ProcessExecutor {

	private ProcessExecutor() {}
	private static List steps = new ArrayList();
	private static final GuiConnector guiConnector = new GuiConnector();


	public static void execute(final Hill hill) {

		for (final IAnt ant : hill.getAnts()) {
			singleStep(ant);
		}
		
		steps.add(new Step(getTurn(), getWorldObjects()));
		guiConnector.placeGuiObjects(getWorldObjects());
	}

	public static void initGame(final Hill team1, final Hill team2) {
		CreateGameData gameData = new CreateGameData();
		gameData.setWidth(WorldConstans.X_BOUNDRY);
		gameData.setHeight(WorldConstans.Y_BOUNDRY);
		gameData.setTeams(new String[]{"King of ants","Queen of pants"});
		guiConnector.initGame(gameData);
		guiConnector.placeGuiObject(team1);
		if (team2 != null) {
			guiConnector.placeGuiObject(team2);
		}
	}
	
	public static List getSteps() {
		return steps;
	}

	private static void singleStep(final IAnt ant) {
		System.out.println("Ant " + ant.getId() + " said:");
		final Vision vision = new Vision(createVisionGrid(ant.getPosition()));
		final Direction direction = ant.move(vision);
		final MovementHandler movementHandler = new MovementHandler();

		if (Direction.NO_MOVE.equals(direction)) {
			System.out.println("I'm not moving. I like this place!");
		} else {
			try {
				boolean hadFood = false;
				
				if (ant instanceof AbstractAnt) {
					hadFood = ((IAnt) ant).hasFood();
				}
				
				movementHandler.makeMove(ant, direction);
				
				if (ant instanceof AbstractAnt) {
					if (!hadFood && ((IAnt) ant).hasFood()) {
						guiConnector.removeGuiObject(((IAnt) ant).getFood());
					}
				}
			} catch (final MoveException e) {
				System.out.println("I cannot move to " + direction.name() + "! That would hurt me!");
			}
		}
	}

	private static Map<Direction, Object> createVisionGrid(final Point visionPosition) {
		final Map<Direction, Object> visionGrid = new EnumMap<>(Direction.class);

		for (final Direction visionDirection : Direction.values()) {
			visionGrid.put(visionDirection, checkField(visionDirection, new Point(visionPosition)));
		}

		return visionGrid;
	}

	private static Object checkField(final Direction direction, final Point point) {
		point.translate(direction.getPositionChange().x, direction.getPositionChange().y);
		final Object foundObject = World.getWorldObject(point);
		System.out.println("I see on position [" + point.x + ", " + point.y + "] "
				+ (foundObject == null ? "nothing" : foundObject));
		return foundObject;
	}
}

@@ -912,20 +912,18 @@
 ;%0D%0A%09%09%7D%0D%0A
-%09%09
 %0D%0A
+
 %09%09steps.
@@ -1096,16 +1096,22 @@
 2) %7B%0D%0A%09%09
+final 
 CreateGa
@@ -1282,40 +1282,44 @@
 ng%5B%5D
-%7B%22King of ants%22,%22Queen of pants%22
+ %7B team1.getName(), team2.getName() 
 %7D);%0D
@@ -1467,19 +1467,18 @@
 %09%7D%0D%0A%09%7D%0D%0A
-%09
 %0D%0A
+
 %09public 
@@ -1984,22 +1984,18 @@
 false;%0D%0A
-%09%09%09%09
 %0D%0A
+
 %09%09%09%09if (
@@ -2039,29 +2039,20 @@
 adFood =
- ((IAnt)
  ant
-)
 .hasFood
@@ -2063,22 +2063,18 @@
 %0A%09%09%09%09%7D%0D%0A
-%09%09%09%09
 %0D%0A
+
 %09%09%09%09move
@@ -2116,14 +2116,10 @@
 );%0D%0A
-%09%09%09%09
 %0D%0A
+
 %09%09%09%09
@@ -2177,21 +2177,12 @@
 d &&
- ((IAnt)
  ant
-)
 .has
@@ -2231,20 +2231,11 @@
 ect(
-((IAnt) 
 ant
-)
 .get

Fix team names initialization.