package net.minecraft.item;

import java.util.List;

import javax.annotation.Nullable;

import com.evilnotch.lib.minecraft.util.TileEntityUtil;

import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.NonNullList;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class ItemBlock
  extends Item
{
  protected final Block block;
  
  public ItemBlock(Block block)
  {
    this.block = block;
  }
  
  public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
  {
    IBlockState iblockstate = worldIn.getBlockState(pos);
    Block block = iblockstate.getBlock();
    if (!block.isReplaceable(worldIn, pos)) {
      pos = pos.offset(facing);
    }
    ItemStack itemstack = player.getHeldItem(hand);
    if ((!itemstack.isEmpty()) && (player.canPlayerEdit(pos, facing, itemstack)) && (worldIn.mayPlace(this.block, pos, false, facing, null)))
    {
      int i = getMetadata(itemstack.getMetadata());
      IBlockState iblockstate1 = this.block.getStateForPlacement(worldIn, pos, facing, hitX, hitY, hitZ, i, player, hand);
      if (placeBlockAt(itemstack, player, worldIn, pos, facing, hitX, hitY, hitZ, iblockstate1))
      {
        iblockstate1 = worldIn.getBlockState(pos);
        SoundType soundtype = iblockstate1.getBlock().getSoundType(iblockstate1, worldIn, pos, player);
        worldIn.playSound(player, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
        itemstack.shrink(1);
      }
      return EnumActionResult.SUCCESS;
    }
    return EnumActionResult.FAIL;
  }
  
  public static boolean setTileEntityNBT(World worldIn, @Nullable EntityPlayer player, BlockPos pos, ItemStack stackIn)
  {
    NBTTagCompound nbt = stackIn.getSubCompound("BlockEntityTag");
    return TileEntityUtil.setTileNBT(worldIn, player, pos, nbt,true);
  }
  
  @SideOnly(Side.CLIENT)
  public boolean canPlaceBlockOnSide(World worldIn, BlockPos pos, EnumFacing side, EntityPlayer player, ItemStack stack)
  {
    Block block = worldIn.getBlockState(pos).getBlock();
    if ((block == Blocks.SNOW_LAYER) && (block.isReplaceable(worldIn, pos))) {
      side = EnumFacing.UP;
    } else if (!block.isReplaceable(worldIn, pos)) {
      pos = pos.offset(side);
    }
    return worldIn.mayPlace(this.block, pos, false, side, null);
  }
  
  public String getUnlocalizedName(ItemStack stack)
  {
    return this.block.getUnlocalizedName();
  }
  
  public String getUnlocalizedName()
  {
    return this.block.getUnlocalizedName();
  }
  
  public CreativeTabs getCreativeTab()
  {
    return this.block.getCreativeTabToDisplayOn();
  }
  
  public void getSubItems(CreativeTabs tab, NonNullList<ItemStack> items)
  {
    if (isInCreativeTab(tab)) {
      this.block.getSubBlocks(tab, items);
    }
  }
  
  @SideOnly(Side.CLIENT)
  public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn)
  {
    super.addInformation(stack, worldIn, tooltip, flagIn);
    this.block.addInformation(stack, worldIn, tooltip, flagIn);
  }
  
  public Block getBlock()
  {
    return getBlockRaw() == null ? null : (Block)getBlockRaw().delegate.get();
  }
  
  private Block getBlockRaw()
  {
    return this.block;
  }
  
  public boolean placeBlockAt(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, IBlockState newState)
  {
    if (!world.setBlockState(pos, newState, 11)) {
      return false;
    }
    IBlockState state = world.getBlockState(pos);
    if (state.getBlock() == this.block)
    {
      setTileEntityNBT(world, player, pos, stack);
      this.block.onBlockPlacedBy(world, pos, state, player, stack);
      if ((player instanceof EntityPlayerMP)) {
        CriteriaTriggers.PLACED_BLOCK.trigger((EntityPlayerMP)player, pos, stack);
      }
    }
    return true;
  }
}
