package com.lothrazar.powerinventory.proxy;

import java.util.ArrayList;

import com.lothrazar.powerinventory.*;
import com.lothrazar.powerinventory.standalone.ContainerCustomPlayer;

import io.netty.buffer.ByteBuf;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntityChest;
//import net.minecraft.util.BlockPos;
import net.minecraft.util.MathHelper;
import cpw.mods.fml.common.network.ByteBufUtils;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
/** 
 * @author Lothrazar at https://github.com/PrinceOfAmber
 */
public class ExpButtonPacket implements IMessage , IMessageHandler<ExpButtonPacket, IMessage>
{
	public ExpButtonPacket() {}
	NBTTagCompound tags = new NBTTagCompound(); 
	
	public ExpButtonPacket(NBTTagCompound ptags)
	{
		tags = ptags;
	}

	@Override
	public void fromBytes(ByteBuf buf) 
	{
		tags = ByteBufUtils.readTag(buf);
	}

	@Override
	public void toBytes(ByteBuf buf) 
	{
		ByteBufUtils.writeTag(buf, this.tags);
	}

	@Override
	public IMessage onMessage(ExpButtonPacket message, MessageContext ctx)
	{
		EntityPlayer player = ctx.getServerHandler().playerEntity;
		ItemStack bottles ;
		if(player.openContainer instanceof ContainerCustomPlayer)
		{
			ContainerCustomPlayer c = (ContainerCustomPlayer)player.openContainer ;
			
			bottles = c.invo.getStackInSlot(Const.bottleSlot);
		}
		else
		{

			bottles = player.inventory.getStackInSlot(Const.bottleSlot);
		}
		//in the game, they drop between 3 and 11 experience //src http://minecraft.gamepedia.com/Bottle_o'_Enchanting
	 
		if(bottles != null && bottles.getItem() == Items.glass_bottle)
		{
			double current = UtilExperience.getExpTotal(player);
			
			//so how many times can we subtract ModConfig.expPerBottle from current?
			//if i have 100 exp, and each bottle costs 5, then i can fill  100/5 = 20 bottles
			
			int bottlesToDrain = MathHelper.floor_double(current / ModConfig.expPerBottle);
			
			//but wait, how many physical bottles are present? we may not have enough for all the exp
			//if we can fill 17, but ony have 16, just do the whole stack
			if(bottlesToDrain >= bottles.stackSize)
			{
				bottlesToDrain = bottles.stackSize;   //just do the whole thing
			}
	  
			if(bottlesToDrain > 0)
			{ 
				UtilExperience.drainExp(player, bottlesToDrain * ModConfig.expPerBottle);
				
				player.inventory.setInventorySlotContents(Const.bottleSlot, new ItemStack(Items.experience_bottle,bottlesToDrain));
			
			
				if(bottlesToDrain < bottles.stackSize) 
				{	
					//what iff:
					//we cannot set the whole stack, the whole slot because that would waste empties
					//drop the empty ones in the world to pick up 
					int emptyBottlesLeft = bottles.stackSize - bottlesToDrain;
					
					player.worldObj.spawnEntityInWorld(new EntityItem(player.worldObj,
		 					player.posX,player.posY,player.posZ,
		 					new ItemStack(Items.glass_bottle,emptyBottlesLeft)));
				}
			} 
		}
		
		return null; 
	}
}

@@ -370,16 +370,59 @@
 .Items;%0A
+import net.minecraft.inventory.IInventory;%0A
 import n
@@ -1491,27 +1491,27 @@
 y;%0A%09
-%09ItemStack bottles 
+ %0A%09%09IInventory invo
 ;%0A%09%09
@@ -1654,61 +1654,21 @@
 %0A%09%09%09
-%0A%09%09%09bottles = c.invo.getStackInSlot(Const.bottleSlot)
+invo = c.invo
 ;%0A%09%09
@@ -1684,19 +1684,15 @@
 %09%09%7B%0A
-%0A
 %09%09%09
-bottles
+invo
  = p
@@ -1706,16 +1706,51 @@
 nventory
+;%0A%09%09%7D%0A%09%09%0A%09%09ItemStack bottles = invo
 .getStac
@@ -1776,20 +1776,16 @@
 eSlot);%0A
-%09%09%7D%0A
 %09%09//in t
@@ -2675,32 +2675,20 @@
 %09%09%09%0A%09%09%09%09
-player.inventory
+invo
 .setInve

fix experience bottling in solo inv