package common.item.tool;

import java.util.List;

import common.collect.Lists;
import common.entity.item.EntityFireworks;
import common.entity.npc.EntityNPC;
import common.item.Item;
import common.item.ItemStack;
import common.tags.TagObject;
import common.tileentity.TileEntity;
import common.util.BlockPos;
import common.util.Facing;
import common.util.Vec3;
import common.world.World;

public class ItemFirework extends Item
{
    /**
     * Called when a Block is right-clicked with this Item
     */
    public boolean onItemUse(ItemStack stack, EntityNPC playerIn, World worldIn, BlockPos pos, Facing side, float hitX, float hitY, float hitZ)
    {
        if (!worldIn.client)
        {
            EntityFireworks entityfireworkrocket = new EntityFireworks(worldIn, (double)((float)pos.getX() + hitX), (double)((float)pos.getY() + hitY), (double)((float)pos.getZ() + hitZ), stack);
            worldIn.spawnEntityInWorld(entityfireworkrocket);

//            if (!playerIn.creative)
//            {
            --stack.size;
//            }

            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * allows items to add custom lines of information to the mouseover description
     */
    public void addInformation(ItemStack stack, EntityNPC playerIn, List<String> tooltip)
    {
        if (stack.hasTagCompound())
        {
            TagObject nbttagcompound = stack.getTagCompound().getObject("Fireworks");

            if (nbttagcompound != null)
            {
                if (nbttagcompound.hasByte("Flight"))
                {
                    tooltip.add("Flugdauer: " + nbttagcompound.getByte("Flight"));
                }

                List<TagObject> nbttaglist = nbttagcompound.getList("Explosions");

                if (nbttaglist != null && nbttaglist.size() > 0)
                {
                    for (int i = 0; i < nbttaglist.size(); ++i)
                    {
                        TagObject nbttagcompound1 = nbttaglist.get(i);
                        List<String> list = Lists.<String>newArrayList();
                        ItemFireworkCharge.addExplosionInfo(nbttagcompound1, list);

                        if (list.size() > 0)
                        {
                            for (int j = 1; j < ((List)list).size(); ++j)
                            {
                                list.set(j, "  " + (String)list.get(j));
                            }

                            tooltip.addAll(list);
                        }
                    }
                }
            }
        }
    }
    
    public ItemStack dispenseStack(World world, TileEntity source, Vec3 position, BlockPos blockpos, Facing facing, ItemStack stack) {
        double d0 = blockpos.getX() + 0.5 + (double)facing.getFrontOffsetX();
        double d1 = (double)blockpos.getY() + 0.2;
        double d2 = blockpos.getZ() + 0.5 + (double)facing.getFrontOffsetZ();
        EntityFireworks entityfireworkrocket = new EntityFireworks(world, d0, d1, d2, stack);
        world.spawnEntityInWorld(entityfireworkrocket);
        stack.splitStack(1);
        return stack;
	}

	public int getDispenseSoundId() {
		return 1002;
	}
}
