package com.lothrazar.samscontent.block;

import com.lothrazar.samscontent.ModLoader;
import com.lothrazar.samscontent.item.ItemRegistry;
import com.lothrazar.util.SamsUtilities;

import net.minecraft.block.Block;
import net.minecraft.block.BlockChest;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class BlockBucketStorage extends Block implements ITileEntityProvider //extends BlockContainer
{
	//http://www.minecraftforge.net/wiki/Basic_Tile_Entity
	//to be used in tandem with ItemBucketStorage. the (block/item) will remember how many full buckets are inside of it.
	
	protected BlockBucketStorage() 
	{
		super(Material.iron);
		this.setHardness(5F);
		this.setResistance(5F);
		this.setCreativeTab(ModLoader.tabSamsContent);
		this.setStepSound(soundTypeMetal);
		this.setHarvestLevel("pickaxe", 1);
	}
	 
	@Override
	public TileEntity createNewTileEntity(World worldIn, int meta) 
	{ 
		return new TileEntityBucketStorage(meta);
	} 
	
	@Override
	public void onBlockHarvested(World world, BlockPos pos, IBlockState state, EntityPlayer player) 
	{
		TileEntityBucketStorage container = (TileEntityBucketStorage)world.getTileEntity(pos);
		
		//since they are not stackable
		for(int i = 0; i < container.getBuckets(); i++)
		{
			SamsUtilities.dropItemStackInWorld(world, pos, new ItemStack(Items.lava_bucket));
		}
	}

	@SubscribeEvent
	public void onPlayerInteract(PlayerInteractEvent event)
  	{      
		if(event.world.isRemote){ return; }//server side only!
		if(event.entityPlayer.isSneaking()) {return;}//they wanted to place real lava, not insert
		ItemStack held = event.entityPlayer.getCurrentEquippedItem();  
		if(held == null) { return; }//empty hand so do nothing
		if(held.getItem() != Items.lava_bucket) { return; }
		
		if(event.action.RIGHT_CLICK_BLOCK == event.action)
		{ 
			Block blockClicked = event.entityPlayer.worldObj.getBlockState(event.pos).getBlock();
			
			if(blockClicked == null || blockClicked == Blocks.air ){return;}
			
			if(blockClicked instanceof BlockBucketStorage)// && event.entityPlayer.isSneaking()
			{   
				TileEntity container = event.world.getTileEntity(event.pos);
				
				if(container == null) 
					System.out.println("instance NULL");
				
				if(container instanceof TileEntityBucketStorage)
				{
					System.out.println("instance of is true");
					TileEntityBucketStorage storage = (TileEntityBucketStorage)container;
					

					storage.addBucket();
					
					int b = storage.getBuckets();
					
					//Testing confirms this works, since we do it on server side only 
					//AND since the block data does not affect renderign on the client -> we do not need custom Packets
					System.out.println("bbb==="+b);
					
					event.entityPlayer.destroyCurrentEquippedItem();
					//TODO: sparkle
					//TODO: sound effect
					event.setCanceled(true);//stop laving from landing on the world
				}
			} 
		}
  	}
}

@@ -2109,49 +2109,17 @@
 ;%7D//
-they wanted to place real lava, not inser
+consisten
 t%0A%09%09
@@ -2182,16 +2182,19 @@
 em();  %0A
+%09%09%0A
 %09%09if(hel
@@ -2208,45 +2208,128 @@
 ll) 
-%7B return; %7D//empty hand so do nothing
+//empty hand %0A%09%09%7B %0A%09%09%09//TODO: if right click with empty handremove exactly one bucket, if we can%0A%09%09%09return;%0A%09%09%7D%0A%09%09%0A%09%09%0A%09%09
 %0A%09%09i
@@ -2378,24 +2378,30 @@
 eturn; %7D%0A%09%09%0A
+%09%09%0A%09%09%0A
 %09%09if(event.a
@@ -2410,12 +2410,11 @@
 ion.
-RIGH
+LEF
 T_CL
@@ -3433,24 +3433,26 @@
 effect%0A%09%09%09%09%09
+//
 event.setCan
@@ -3502,16 +3502,32 @@
 he world
+, but didnt work
 %0A%09%09%09%09%7D%0A%09

change to left click