package net.minecraft.item;

import java.util.List;
import java.util.Random;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityArmorStand;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.MathHelper;
import net.minecraft.util.Rotations;
import net.minecraft.world.World;

public class ItemArmorStand extends Item
{
    private static final String __OBFID = "CL_00002182";

    public ItemArmorStand()
    {
        this.setCreativeTab(CreativeTabs.tabDecorations);
    }

    /**
     * Called when a Block is right-clicked with this Item
     *  
     * @param pos The block being right-clicked
     * @param side The side being right-clicked
     */
    public boolean onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
    {
        if (side == EnumFacing.DOWN)
        {
            return false;
        }
        else
        {
            boolean flag = worldIn.getBlockState(pos).getBlock().isReplaceable(worldIn, pos);
            BlockPos blockpos1 = flag ? pos : pos.offset(side);

            if (!playerIn.func_175151_a(blockpos1, side, stack))
            {
                return false;
            }
            else
            {
                BlockPos blockpos2 = blockpos1.offsetUp();
                boolean flag1 = !worldIn.isAirBlock(blockpos1) && !worldIn.getBlockState(blockpos1).getBlock().isReplaceable(worldIn, blockpos1);
                flag1 |= !worldIn.isAirBlock(blockpos2) && !worldIn.getBlockState(blockpos2).getBlock().isReplaceable(worldIn, blockpos2);

                if (flag1)
                {
                    return false;
                }
                else
                {
                    double d0 = (double)blockpos1.getX();
                    double d1 = (double)blockpos1.getY();
                    double d2 = (double)blockpos1.getZ();
                    List list = worldIn.getEntitiesWithinAABBExcludingEntity((Entity)null, AxisAlignedBB.fromBounds(d0, d1, d2, d0 + 1.0D, d1 + 2.0D, d2 + 1.0D));

                    if (list.size() > 0)
                    {
                        return false;
                    }
                    else
                    {
                        if (!worldIn.isRemote)
                        {
                            worldIn.setBlockToAir(blockpos1);
                            worldIn.setBlockToAir(blockpos2);
                            EntityArmorStand entityarmorstand = new EntityArmorStand(worldIn, d0 + 0.5D, d1, d2 + 0.5D);
                            float f3 = (float)MathHelper.floor_float((MathHelper.wrapAngleTo180_float(playerIn.rotationYaw - 180.0F) + 22.5F) / 45.0F) * 45.0F;
                            entityarmorstand.setLocationAndAngles(d0 + 0.5D, d1, d2 + 0.5D, f3, 0.0F);
                            this.func_179221_a(entityarmorstand, worldIn.rand);
                            NBTTagCompound nbttagcompound = stack.getTagCompound();

                            if (nbttagcompound != null && nbttagcompound.hasKey("EntityTag", 10))
                            {
                                NBTTagCompound nbttagcompound1 = new NBTTagCompound();
                                entityarmorstand.writeToNBTOptional(nbttagcompound1);
                                nbttagcompound1.merge(nbttagcompound.getCompoundTag("EntityTag"));
                                entityarmorstand.readFromNBT(nbttagcompound1);
                            }

                            worldIn.spawnEntityInWorld(entityarmorstand);
                        }

                        --stack.stackSize;
                        return true;
                    }
                }
            }
        }
    }

    private void func_179221_a(EntityArmorStand p_179221_1_, Random p_179221_2_)
    {
        Rotations rotations = p_179221_1_.getHeadRotation();
        float f = p_179221_2_.nextFloat() * 5.0F;
        float f1 = p_179221_2_.nextFloat() * 20.0F - 10.0F;
        Rotations rotations1 = new Rotations(rotations.func_179415_b() + f, rotations.func_179416_c() + f1, rotations.func_179413_d());
        p_179221_1_.setHeadRotation(rotations1);
        rotations = p_179221_1_.getBodyRotation();
        f = p_179221_2_.nextFloat() * 10.0F - 5.0F;
        rotations1 = new Rotations(rotations.func_179415_b(), rotations.func_179416_c() + f, rotations.func_179413_d());
        p_179221_1_.setBodyRotation(rotations1);
    }
}