// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) braces deadcode

package net.minecraft.src;

import java.util.List;

// Referenced classes of package net.minecraft.src:
// Item, ItemStack, World, Block,
// BlockSapling, BlockCrops, BlockGrass, BlockTallGrass,
// BlockFlower, EntitySheep, BlockCloth, EntityPlayer,
// EntityLiving

public class PCmo_ItemActivator extends Item {
	public PCmo_ItemActivator(int i) {
		super(i);
		setMaxDamage(20);
		setMaxStackSize(1);
	}

	@Override
	public boolean onItemUse(ItemStack itemstack, EntityPlayer entityplayer, World world, int i, int j, int k, int l) {

		if (world.getBlockId(i, j, k) == Block.mobSpawner.blockID) {

			ModLoader.openGUI(entityplayer, new PCmo_GuiSpawnerEditor((TileEntityMobSpawner) world.getBlockTileEntity(i, j, k)));

			return true;
		}

		int steel = Block.blockSteel.blockID;
		int chest = Block.chest.blockID;

		String eMinerStructure = "Not a valid Miner's structure!";
		String eMinerCrystals = "Put some Power Crystals into the chest!";

		miner:
		for (int y = j; y >= j - 1; y--) {
			for (int x = i - 1; x <= i + 1; x++) {
				for (int z = k - 1; z <= k + 1; z++) {
					if (world.getBlockId(x, y, z) == steel && world.getBlockId(x + 1, y, z) == steel
							&& world.getBlockId(x, y, z + 1) == steel && world.getBlockId(x + 1, y, z + 1) == steel) {
						// valid bottom layer
						// find direction.
						if (world.getBlockId(x, y + 1, z) == steel && world.getBlockId(x + 1, y + 1, z) == chest
								&& world.getBlockId(x, y + 1, z + 1) == steel && world.getBlockId(x + 1, y + 1, z + 1) == chest) {
							if (spawnMinerAt(world, x, y, z, 0)) {
								itemstack.damageItem(1, entityplayer);
							} else {
								PC_Utils.chatMsg(eMinerCrystals, false);
							}
							return true;
						}
						if (world.getBlockId(x, y + 1, z) == chest && world.getBlockId(x + 1, y + 1, z) == chest
								&& world.getBlockId(x, y + 1, z + 1) == steel && world.getBlockId(x + 1, y + 1, z + 1) == steel) {
							if (spawnMinerAt(world, x, y, z, 3)) {
								itemstack.damageItem(1, entityplayer);
							} else {
								PC_Utils.chatMsg(eMinerCrystals, false);
							}
							return true;
						}
						if (world.getBlockId(x, y + 1, z) == chest && world.getBlockId(x + 1, y + 1, z) == steel
								&& world.getBlockId(x, y + 1, z + 1) == chest && world.getBlockId(x + 1, y + 1, z + 1) == steel) {
							if (spawnMinerAt(world, x, y, z, 2)) {
								itemstack.damageItem(1, entityplayer);
							} else {
								PC_Utils.chatMsg(eMinerCrystals, false);
							}
							return true;
						}
						if (world.getBlockId(x, y + 1, z) == steel && world.getBlockId(x + 1, y + 1, z) == steel
								&& world.getBlockId(x, y + 1, z + 1) == chest && world.getBlockId(x + 1, y + 1, z + 1) == chest) {
							if (spawnMinerAt(world, x, y, z, 1)) {
								itemstack.damageItem(1, entityplayer);
							} else {
								PC_Utils.chatMsg(eMinerCrystals, false);
							}
							return true;
						}

						PC_Utils.chatMsg(eMinerStructure, false);

						break miner;
					}
				}
			}
		}

		if (checkForFishingMachineNearby(world, i, j, k)) {
			spawnFishingMachine(world);
			itemstack.damageItem(1, entityplayer);
		}
		return false;
	}

	double transferX = 0, transferY = 0, transferZ = 0;
	int transferX_blocks = 0, transferY_blocks = 0, transferZ_blocks = 0;

	// int strings = 0;

	private boolean checkForFishingMachineNearby(World world, int x, int y, int z) {
		if (world.getBlockId(x, y, z) == 85) {
			int fish = 0;
			boolean eWater = false;
			fish = checkForFishingMachineAt(world, x - 2, y, z);
			if (fish == 0) { return true; }
			if (fish == 1) {
			}
			if (fish == 2) {
				eWater = true;
			}

			fish = checkForFishingMachineAt(world, x, y, z - 2);
			if (fish == 0) { return true; }
			if (fish == 1) {
			}
			if (fish == 2) {
				eWater = true;
			}

			fish = checkForFishingMachineAt(world, x - 2, y, z - 2);
			if (fish == 0) { return true; }
			if (fish == 1) {
			}
			if (fish == 2) {
				eWater = true;
			}

			fish = checkForFishingMachineAt(world, x, y, z);
			if (fish == 0) { return true; }
			if (fish == 1) {
			}
			if (fish == 2) {
				eWater = true;
			}

			if (eWater) {
				PC_Utils.chatMsg("Not enough water!", false);
			} else {
				PC_Utils.chatMsg("Not a valid Fish Machine structure!", false);
			}

		} else {
			if (world.getBlockId(x, y, z) == 5) {
				PC_Utils.chatMsg("Fence, not planks!", false);
			}
		}
		return false;
	}

	int[][] fishPattern = { { 85, 5, 85 }, { 5, 42, 5 }, { 85, 5, 85 } };

	private int checkForFishingMachineAt(World world, int x, int y, int z) {
		for (int i = x; i < x + 3; i++) {
			for (int j = z; j < z + 3; j++) {
				if (world.getBlockId(i, y, j) != fishPattern[i - x][j - z]) { return 1; }
			}
		}

		if (world.getBlockId(x + 1, y + 1, z + 1) != 54) { return 1; }

		// count water blocks under it - depth of 10.

		for (int i = x; i < x + 3; i++) {
			for (int j = z; j < z + 3; j++) {
				for (int k = y - 1; k > y - 10; k--) {
					if (world.getBlockId(i, k, j) != 8 && world.getBlockId(i, k, j) != 9) { return 2; }
				}
			}
		}

		transferX = x + 1.5D;
		transferY = y;
		transferZ = z + 1.5D;

		transferX_blocks = x;
		transferY_blocks = y;
		transferZ_blocks = z;

		return 0;
	}

	private void erase(World world, int x, int y, int z) {
		world.setBlockWithNotify(x, y, z, 0);
		world.setBlockWithNotify(x + 1, y, z, 0);
		world.setBlockWithNotify(x, y, z + 1, 0);
		world.setBlockWithNotify(x + 1, y, z + 1, 0);

		world.setBlockWithNotify(x, y + 1, z, 0);
		world.setBlockWithNotify(x + 1, y + 1, z, 0);
		world.setBlockWithNotify(x, y + 1, z + 1, 0);
		world.setBlockWithNotify(x + 1, y + 1, z + 1, 0);
	}

	private void spawnFishingMachine(World world) {
		// for(int i=transferX_blocks; i<transferX_blocks+3; i++){
		// for(int j=transferZ_blocks; j<transferZ_blocks+3; j++){
		// world.setBlockWithNotify(i,transferY_blocks,j, 0);
		// }
		// }

		world.setBlockWithNotify(transferX_blocks + 1, transferY_blocks, transferZ_blocks + 1, 0); // remove
																									// iron
																									// cube
		PCmo_EntityFishingMachine entity = new PCmo_EntityFishingMachine(world);

		entity.setRebuildPoint(transferX_blocks, transferY_blocks, transferZ_blocks);
		// entity.setStringCount(strings);

		entity.setLocationAndAngles(transferX, transferY - entity.yOffset, transferZ, 0.0F, 0.0F);
		world.spawnEntityInWorld(entity);
	}

	private boolean spawnMinerAt(World world, int x, int y, int z, int rot) {
		PCmo_EntityMiner entity = new PCmo_EntityMiner(world);
		entity.building = true; // disable crystal counting.

		IInventory inv = null;

		test:
		for (int i = x - 1; i <= x + 1; i++) {
			for (int k = z - 1; k <= z + 1; k++) {
				inv = PC_InvUtils.getCompositeInventoryAt(world, i, y + 1, k);
				if (inv != null) {
					break test;
				}
			}
		}

		if (inv == null) { return false; }

		// count diamonds.
		boolean[] foundTable = { false, false, false, false, false, false, false, false };

		for (int i = 0; i < inv.getSizeInventory(); i++) {
			if (inv.getStackInSlot(i) != null && inv.getStackInSlot(i).itemID == mod_PCmobile.powerCrystal.blockID) {
				foundTable[MathHelper.clamp_int(inv.getStackInSlot(i).getItemDamage(), 0, 7)] = true;
			}
		}

		int cnt = 0;
		for (int i = 0; i < 8; i++) {
			if (foundTable[i]) {
				cnt++;
			}
		}

		if (cnt == 0) { return false; }

		// move contents.

		for (int i = 0; i < Math.min(inv.getSizeInventory(), entity.getSizeInventory()); i++) {
			entity.setInventorySlotContents(i, inv.getStackInSlot(i));
			inv.setInventorySlotContents(i, null);
		}

		// remove blocks.
		erase(world, x, y, z);

		// update level.
		entity.building = false;
		entity.closeChest();

		entity.setLocationAndAngles((double) x + 1, y, (double) z + 1, (rot * 90F), 0.0F);
		world.spawnEntityInWorld(entity);
		return true;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public void addInformation(ItemStack itemstack, List list) {
		// list.add((new
		// StringBuilder()).append("Activates built Miner.").toString());
	}
}
