package common.item;

import common.block.Block;
import common.block.BlockStandingSign;
import common.block.BlockWallSign;
import common.entity.npc.EntityNPC;
import common.init.Blocks;
import common.tileentity.TileEntity;
import common.tileentity.TileEntitySign;
import common.util.BlockPos;
import common.util.ExtMath;
import common.util.Facing;
import common.world.World;

public class ItemSign extends Item
{
    public ItemSign()
    {
        this.setTab(CheatTab.tabDeco);
    }

    public Block getBlock()
    {
        return Blocks.sign;
    }

    /**
     * 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 (side == Facing.DOWN)
        {
            return false;
        }
        else if (!worldIn.getState(pos).getBlock().getMaterial().isSolid())
        {
            return false;
        }
        else
        {
            pos = pos.offset(side);

            if (!playerIn.canPlayerEdit(pos, side, stack))
            {
                return false;
            }
            else if (!Blocks.sign.canPlaceBlockAt(worldIn, pos))
            {
                return false;
            }
            else if (worldIn.client)
            {
                return true;
            }
            else
            {
                if (side == Facing.UP)
                {
                    int i = ExtMath.floord((double)((playerIn.rotYaw + 180.0F) * 16.0F / 360.0F) + 0.5D) & 15;
                    worldIn.setState(pos, Blocks.sign.getState().withProperty(BlockStandingSign.ROTATION, Integer.valueOf(i)), 3);
                }
                else
                {
                    worldIn.setState(pos, Blocks.wall_sign.getState().withProperty(BlockWallSign.FACING, side), 3);
                }

                --stack.stackSize;
                TileEntity tileentity = worldIn.getTileEntity(pos);

                if (tileentity instanceof TileEntitySign && !ItemBlock.setTileEntityNBT(worldIn, playerIn, pos, stack))
                {
                    playerIn.openEditSign((TileEntitySign)tileentity);
                }

                return true;
            }
        }
    }
    
//    public Set<String> getValidTags() {
//    	return Sets.newHashSet("BlockEntityTag");
//    }
//    
//    protected boolean validateNbt(NBTTagCompound tag) {
//    	if(tag.hasKey("BlockEntityTag")) {
//    		if(!tag.hasKey("BlockEntityTag", 10)) {
//        		return false;
//        	}
//    	}
//    	return true;
//    }
}
