package net.minecraft.src;

import java.util.Random;

public class PCma_BlockOptical extends BlockContainer implements PC_IBlockType {

	protected PCma_BlockOptical(int id) {
		super(id, Material.glass);
		float f = 0.4F;
		float f1 = 1.0F;
		setBlockBounds(0.5F - f, 0.1F, 0.5F - f, 0.5F + f, f1 - 0.1F, 0.5F + f);
	}

	@Override
	public int getRenderType() {
		return -1;
	}

	@Override
	public TileEntity getBlockEntity() {
		return new PCma_TileEntityOptical();
	}

	@Override
	public boolean renderAsNormalBlock() {
		return false;
	}

	@Override
	public boolean getBlocksMovement(IBlockAccess par1IBlockAccess, int par2, int par3, int i) {
		return true;
	}

	@Override
	public boolean isOpaqueCube() {
		return false;
	}

	@Override
	public int idDropped(int par1, Random random, int par3) {
		return -1;
	}

	@Override
	public int quantityDropped(Random random) {
		return 0;
	}

	@Override
	public void onBlockRemoval(World world, int i, int j, int k) {
		if (!ModLoader.getMinecraftInstance().playerController.isInCreativeMode()) {
			if (isMirror(world, i, j, k)) {
				dropBlockAsItem_do(world, i, j, k, new ItemStack(mod_PCmachines.mirrorPlacer, 1));
			} else {
				dropBlockAsItem_do(world, i, j, k, new ItemStack(mod_PCmachines.prismPlacer, 1));
				PCma_TileEntityOptical teo = getTE(world, i, j, k);
				for (int q = 0; q <= 9; q++) {
					if (teo.getPrismSide(q)) {
						dropBlockAsItem_do(world, i, j, k, new ItemStack(Block.thinGlass, 1));
					}
				}
			}
		}
		super.onBlockRemoval(world, i, j, k);
	}

	@Override
	public void onNeighborBlockChange(World par1World, int par2, int par3, int par4, int par5) {
		super.onNeighborBlockChange(par1World, par2, par3, par4, par5);
	}

	@Override
	public boolean blockActivated(World world, int i, int j, int k, EntityPlayer player) {
		ItemStack ihold = player.getCurrentEquippedItem();
		if (ihold != null) {
			if (ihold.itemID == mod_PCmobile.powerCrystal.blockID) {

				getTE(world, i, j, k).setMirrorColor(ihold.getItemDamage());

				return true;
			}

			if (ihold.getItem() instanceof ItemBlock) {
				Block bhold = Block.blocksList[ihold.getItem().shiftedIndex];
				if (bhold instanceof PC_IBlockType) { return false; }
			}
		}

		if (isMirror(world, i, j, k)) {
			int m = world.getBlockMetadata(i, j, k);
			int m2;
			if (player.isSneaking()) {
				m2 = m + 1;
				if (m2 > 15) {
					m2 = 0;
				}
			} else {
				m2 = m - 1;
				if (m2 < 0) {
					m2 = 15;
				}
			}
			world.setBlockMetadataWithNotify(i, j, k, m2);
		} else {
			// prism...

			int angle = MathHelper.floor_double((((player.rotationYaw + 180F) * 16F) / 360F) + 0.5D) & 0xf;
			angle &= 0xE;
			angle = angle >> 1;
			angle += 2;
			if (angle > 7) {
				angle = angle - 8;
			}

			angle += 2;

			// if close enough
			if (MathHelper.abs((float) player.posX - (i + 0.5F)) < 2F && MathHelper.abs((float) player.posZ - (k + 0.5F)) < 2F) {
				double d = (player.posY + 1.8200000000000001D) - player.yOffset;

				if (d - j > 2D) {
					angle = 1;
				}

				if (j - d > 0.0D) {
					angle = 0;
				}
			}

			boolean drop = true;
			if (ihold != null) {
				if (ihold.getItem().shiftedIndex == Block.thinGlass.blockID) {

					if (isSideOn(world, i, j, k, angle) == false) {

						getTE(world, i, j, k).setPrismSide(angle, true);
						ihold.stackSize--;
						drop = false;

					}

				}
			}
			if (drop) {

				if (isSideOn(world, i, j, k, angle)) {

					getTE(world, i, j, k).setPrismSide(angle, false);
					dropBlockAsItem_do(world, i, j, k, new ItemStack(Block.thinGlass, 1));

				}

			}

		}
		return true;
	}

	public static PCma_TileEntityOptical getTE(IBlockAccess iblockaccess, int i, int j, int k) {
		TileEntity te = iblockaccess.getBlockTileEntity(i, j, k);
		if (te == null) { return null; }
		PCma_TileEntityOptical tem = (PCma_TileEntityOptical) te;

		return tem;
	}

	public static boolean isSideOn(IBlockAccess iblockaccess, int i, int j, int k, int side) {
		return getTE(iblockaccess, i, j, k).getPrismSide(side);
	}

	public static boolean isMirror(IBlockAccess iblockaccess, int i, int j, int k) {
		return getTE(iblockaccess, i, j, k).isMirror();
	}

	public static int getMirrorColor(IBlockAccess iblockaccess, int i, int j, int k) {
		return getTE(iblockaccess, i, j, k).getMirrorColor();
	}

	public static boolean isPrism(IBlockAccess iblockaccess, int i, int j, int k) {
		return !getTE(iblockaccess, i, j, k).isMirror();
	}

	//@formatter:off
	
	@Override
	public boolean isTranslucentForLaser() { return true; }

	@Override
	public boolean isHarvesterIgnored() { return true; }

	@Override
	public boolean isHarvesterDelimiter() { return false; }

	@Override
	public boolean isBuilderIgnored() { return true; }

	@Override
	public boolean isConveyor(){ return false; }

	@Override
	public boolean isElevator() { return false; }
	
	//@formatter:on
}
