package com.unascribed.correlatedpotentialistics.block;

import java.util.List;

import com.unascribed.correlatedpotentialistics.helper.Blocks;
import com.unascribed.correlatedpotentialistics.item.ItemDrive;
import com.unascribed.correlatedpotentialistics.tile.TileEntityDriveBay;
import com.unascribed.correlatedpotentialistics.tile.TileEntityNetworkMember;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

public class BlockDriveBay extends Block {
	public static final IProperty<EnumFacing> facing = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
	public BlockDriveBay() {
		super(Material.iron);
	}

	@Override
	public boolean hasTileEntity(IBlockState state) {
		return true;
	}
	
	@Override
	public TileEntity createTileEntity(World world, IBlockState state) {
		return new TileEntityDriveBay();
	}
	
	@Override
	protected BlockState createBlockState() {
		return new BlockState(this, facing);
	}
	
	@Override
	public int getMetaFromState(IBlockState state) {
		return state.getValue(facing).getHorizontalIndex();
	}
	
	@Override
	public IBlockState getStateFromMeta(int meta) {
		return getDefaultState().withProperty(facing, EnumFacing.getHorizontal(meta));
	}
	
	@Override
	public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facingIn, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) {
		return this.getDefaultState()
				.withProperty(facing, placer.getHorizontalFacing().getOpposite());
	}
	
	@Override
	public void onNeighborChange(IBlockAccess world, BlockPos pos, BlockPos neighbor) {
		TileEntity te = world.getTileEntity(pos);
		if (te instanceof TileEntityNetworkMember) {
			((TileEntityNetworkMember)te).handleNeighborChange(world, pos, neighbor);
		}
	}
	
	@Override
	public List<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune) {
		List<ItemStack> li = super.getDrops(world, pos, state, fortune);
		TileEntity te = world.getTileEntity(pos);
		if (te instanceof TileEntityDriveBay) {
			TileEntityDriveBay tedb = (TileEntityDriveBay) te;
			for (int i = 0; i < 8; i++) {
				if (tedb.hasDriveInSlot(i)) {
					li.add(tedb.getDriveInSlot(i));
				}
			}
		}
		return li;
	}
	
	@Override
	public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ) {
		if (Blocks.tryWrench(world, pos, player)) {
			return true;
		}
		ItemStack inHand = player.getHeldItem();
		int slot = getLookedAtSlot(state, side, hitX, hitY, hitZ);
		
		if (slot != -1) {
			TileEntity te = world.getTileEntity(pos);
			if (te instanceof TileEntityDriveBay) {
				TileEntityDriveBay tedb = (TileEntityDriveBay) te;
				if (tedb.hasDriveInSlot(slot)) {
					if (!world.isRemote) {
						EntityItem ent = new EntityItem(world, pos.getX()+hitX+(side.getFrontOffsetX()*0.2),
								pos.getY()+hitY+(side.getFrontOffsetY()*0.2), pos.getZ()+hitZ+(side.getFrontOffsetZ()*0.2));
						ent.setEntityItemStack(tedb.getDriveInSlot(slot));
						ent.setNoPickupDelay();
						world.spawnEntityInWorld(ent);
						tedb.setDriveInSlot(slot, null);
					}
					return true;
				} else {
					if (inHand != null && inHand.getItem() instanceof ItemDrive) {
						if (!world.isRemote) {
							tedb.setDriveInSlot(slot, inHand);
							player.setCurrentItemOrArmor(0, null);
						}
						return true;
					}
				}
			}
		}
		return super.onBlockActivated(world, pos, state, player, side, hitX, hitY, hitZ);
	}
	
	public int getLookedAtSlot(IBlockState state, EnumFacing side, float hitX, float hitY, float hitZ) {
		if (side != state.getValue(facing)) return -1;
		float x;
		float y = 1-hitY;
		switch (side) {
			case NORTH:
				x = 1-hitX;
				break;
			case EAST:
				x = 1-hitZ;
				break;
			case SOUTH:
				x = hitX;
				break;
			case WEST:
				x = hitZ;
				break;
			default:
				x = 0;
		}
		int slot = -1;
		if (withinRegion(x, y, 3, 3)) {
			slot = 0;
		} else if (withinRegion(x, y, 9, 3)) {
			slot = 1;
		} else if (withinRegion(x, y, 3, 6)) {
			slot = 2;
		} else if (withinRegion(x, y, 9, 6)) {
			slot = 3;
		} else if (withinRegion(x, y, 3, 9)) {
			slot = 4;
		} else if (withinRegion(x, y, 9, 9)) {
			slot = 5;
		} else if (withinRegion(x, y, 3, 12)) {
			slot = 6;
		} else if (withinRegion(x, y, 9, 12)) {
			slot = 7;
		}
		return slot;
	}

	private boolean withinRegion(float x, float y, int regionX, int regionY) {
		return x >= (regionX/16f) && x <= ((regionX+4)/16f)
				&& y >= (regionY/16f) && y <= ((regionY+2)/16f);
	}
}

@@ -54,32 +54,8 @@
 k;%0A%0A
-import java.util.List;%0A%0A
 impo
@@ -2330,45 +2330,29 @@
 lic 
-List%3CItemStack%3E getDrops(IBlockAccess
+void breakBlock(World
  wor
@@ -2390,91 +2390,11 @@
 tate
-, int fortune) %7B%0A%09%09List%3CItemStack%3E li = super.getDrops(world, pos, state, fortune);
+) %7B
 %0A%09%09T
@@ -2606,15 +2606,34 @@
 %09%09%09%09
-li.add(
+spawnAsEntity(world, pos, 
 tedb
@@ -2661,30 +2661,91 @@
 %09%09%09%09
-%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return li
+%09tedb.setDriveInSlot(i, null);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%09super.breakBlock(world, pos, state)
 ;%0A%09%7D

Move drive dropping logic from getDrops to breakBlock. Fixes #4