package ncrashed.warp;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import ncrashed.warp.client.ClientProxy;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumToolMaterial;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class DebugItem extends Item {
	
	private int x1, x2, y1, y2, z1, z2;
	private int useCounter = 0;
	private boolean isFirst = true;

	public DebugItem(int par1) {
		
		super(par1);

		setMaxStackSize(1);
		//setIconIndex(0);
		setCreativeTab(CreativeTabs.tabTools);
	}
	
	public String getTextureFile()
	{
		return ClientProxy.ITEMS_TEXTURE;
	}
	
    /**
     * Called before a block is broken.  Return true to prevent default block harvesting.
     *
     * Note: In SMP, this is called on both client and server sides!
     *
     * @param itemstack The current ItemStack
     * @param X The X Position
     * @param Y The X Position
     * @param Z The X Position
     * @param player The Player that is wielding the item
     * @return True to prevent harvesting, false to continue as normal
     */
	@Override
    public boolean onBlockStartBreak(ItemStack itemstack, int x, int y, int z, EntityPlayer player)
    {
		if(FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT)
		{
			
			return true;
		}
		
		//System.out.printf("Warp:(%d %d %d)-(%d %d %d) dv:(%d %d %d)\n",
		//		x1, y1, z1, x2, y2, z2, X-x1, Y-y1, Z-z1);
		//ChunkCopier.copyVolume(player.worldObj, x1, y1, z1, x2, y2, z2, X-x1, Y-y1, Z-z1);
		
		System.out.println(player.worldObj.getBlockId(x, y, z) + ":" + player.worldObj.getBlockMetadata(x, y, z));
		
		/*TileEntity expander = player.worldObj.getBlockTileEntity(x, y, z);
		boolean bitVal = true;
		int bitPos = 10;
		ModHelper.rp2SetIOExpander(expander, ModHelper.rp2GetIOExpander(expander) ^ ((bitVal ? 1 : 0 ) << bitPos));*/
		
		//ModHelper.rp2SetIOExpander(entity, 8);
        return true;
    }
	
	@Override
	public boolean onItemUse(ItemStack par1ItemStack,
			EntityPlayer par2EntityPlayer, World par3World, int par4, int par5,
			int par6, int par7, float par8, float par9, float par10) {
		
		if(FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT)
		{
			
			return true;
		}
		//if(useCounter++ % 2 == 1) return false;
		
		if (isFirst)
		{
			x1 = par4;
			
			y1 = par5;
			
			z1 = par6;
			
			isFirst = false;
		}
		else
		{
			x2 = par4;
			
			y2 = par5;
			
			z2 = par6;
			
			System.out.printf("WarpVolume:(%d %d %d)-(%d %d %d)\n",
					x1, y1, z1, x2, y2, z2);
			
			isFirst = true;
			
			ChunkCopier.copyVolume(par3World, x1, y1, z1, x2, y2, z2, 50, 20, 0);
		}
		
		
		return true;
		//return false;
		// TODO Auto-generated method stub
		//return super.onItemUse(par1ItemStack, par2EntityPlayer, par3World, par4, par5,
		//		par6, par7, par8, par9, par10);
	}
	
	

}
