import java.awt.Color;

import becker.robots.*;

public class Gods extends City {

	int TASKS = 3;
	Wall[] walls = new Wall[37];
	int[] hiddenWalls = { 3, 4, 16 };
	Color intersectionColour = new Color(0xD3D3D3);
	final Color GOLD = new Color(0xCEA008);
	Hero hercules;

	public Gods() {
		super();
		this.setFrameTitle("Three Labours of Hercules");
		int street = 3;
		int avenue = 3;
		// Labour One Walls
		walls[0] = new Wall(this, street, avenue, Direction.NORTH);
		walls[1] = new Wall(this, street, avenue, Direction.SOUTH);
		walls[2] = new Wall(this, street, avenue, Direction.WEST);
		walls[3] = new Wall(this, street, avenue, Direction.EAST);

		// Labour Two Walls
		walls[4] = new Wall(this, 3, 2, Direction.WEST);
		walls[5] = new Wall(this, 2, 2, Direction.WEST);
		walls[6] = new Wall(this, 2, 2, Direction.NORTH);
		walls[7] = new Wall(this, 2, 3, Direction.NORTH);
		walls[8] = new Wall(this, 2, 4, Direction.NORTH);
		walls[9] = new Wall(this, 2, 4, Direction.EAST);
		walls[10] = new Wall(this, 3, 4, Direction.EAST);
		walls[11] = new Wall(this, 4, 4, Direction.EAST);
		walls[12] = new Wall(this, 4, 4, Direction.SOUTH);
		walls[13] = new Wall(this, 4, 3, Direction.SOUTH);
		walls[14] = new Wall(this, 4, 2, Direction.SOUTH);
		walls[15] = new Wall(this, 4, 2, Direction.WEST);
		// Labour Three Walls
		walls[16] = new Wall(this, 1, 3, Direction.NORTH);
		walls[17] = new Wall(this, 1, 4, Direction.NORTH);
		walls[18] = new Wall(this, 1, 5, Direction.NORTH);
		walls[19] = new Wall(this, 1, 5, Direction.EAST);
		walls[20] = new Wall(this, 1, 5, Direction.EAST);
		walls[21] = new Wall(this, 2, 5, Direction.EAST);
		walls[22] = new Wall(this, 3, 5, Direction.EAST);
		walls[23] = new Wall(this, 4, 5, Direction.EAST);
		walls[24] = new Wall(this, 5, 5, Direction.EAST);
		walls[25] = new Wall(this, 5, 5, Direction.SOUTH);
		walls[26] = new Wall(this, 5, 4, Direction.SOUTH);
		walls[27] = new Wall(this, 5, 3, Direction.SOUTH);
		walls[28] = new Wall(this, 5, 2, Direction.SOUTH);
		walls[29] = new Wall(this, 5, 1, Direction.SOUTH);
		walls[30] = new Wall(this, 5, 1, Direction.WEST);
		walls[31] = new Wall(this, 4, 1, Direction.WEST);
		walls[32] = new Wall(this, 3, 1, Direction.WEST);
		walls[33] = new Wall(this, 2, 1, Direction.WEST);
		walls[34] = new Wall(this, 1, 1, Direction.WEST);
		walls[35] = new Wall(this, 1, 1, Direction.NORTH);
		walls[36] = new Wall(this, 1, 2, Direction.NORTH);

		hercules = new Hero(this, 3, 3, Direction.NORTH, 6);
	}

	public void commandHero() {
		hercules.performLabourOne();
		if (this.confirmLabourOne()) {
			this.modifyWalls(0, Direction.EAST);
			hercules.prepareForLabourTwo();
			hercules.performLabourTwo();
			if (this.confirmLabourTwo()) {
				this.modifyWalls(1, Direction.WEST);
				hercules.prepareForLabourThree();
				hercules.performLabourThree();
				if (this.confirmLabourThree()) {
					this.removeWall(hiddenWalls[2], Direction.NORTH);
					hercules.prepareForRedemption();
					if (hercules.getStreet()==0 && hercules.getAvenue()==3)
						hercules.setLabel("Redemption");

				}
			}
		}

	}

	private void modifyWalls(int which, Direction dir) {
		for (int i = 0; i < hiddenWalls[which + 1]; i++) {
			walls[i].setBlocksEntry(false, false, false, false);
			walls[i].setBlocksExit(false, false, false, false);
		}
		removeWall(hiddenWalls[which], dir);
	}

	private void removeWall(int which, Direction dir) {
		walls[which].setColor(intersectionColour);
		walls[which].setBlocksExit(dir, false);

	}

	protected Intersection getIntersection(int street, int avenue) {
		return super.getIntersection(street, avenue);
	}

	public boolean confirmLabourOne() {
		Intersection intersection = this.getIntersection(3, 3);
		return intersection.countThings() == 5;
	}

	public boolean confirmLabourTwo() {
		Intersection int1 = this.getIntersection(2, 4);
		Intersection int2 = this.getIntersection(2, 2);
		Intersection int3 = this.getIntersection(4, 2);
		Intersection int4 = this.getIntersection(4, 4);
		return int1.countThings() == 3 && int2.countThings() == 3
				&& int3.countThings() == 3 && int4.countThings() == 3;
	}

	public boolean confirmLabourThree() {
		Intersection intersection = this.getIntersection(5, 3);
		IIterate<Thing> stuff = intersection.examineThings();
		while (stuff.hasNext()) {
			Thing thing = stuff.next();
			if (!(thing instanceof Wall))
				return thing.getColor().equals(GOLD)&& thing.getIcon().getSize()==0.25;
		}
		return false;
	}

}
