package common.item;

import common.block.Block;
import common.block.BlockSkull;
import common.entity.npc.EntityNPC;
import common.init.Blocks;
import common.model.Model;
import common.model.ModelProvider;
import common.model.Transforms;
import common.tileentity.TileEntity;
import common.tileentity.TileEntitySkull;
import common.util.BlockPos;
import common.util.ExtMath;
import common.util.Facing;
import common.world.State;
import common.world.World;

public class ItemSkull extends Item
{
    public ItemSkull()
    {
        this.setTab(CheatTab.tabDeco);
//        this.setMaxDamage(0);
//        this.setHasSubtypes(true);
    }

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

    /**
     * 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
        {
            State iblockstate = worldIn.getState(pos);
            Block block = iblockstate.getBlock();
            boolean flag = block.isReplaceable(worldIn, pos);

            if (!flag)
            {
                if (!worldIn.getState(pos).getBlock().getMaterial().isSolid())
                {
                    return false;
                }

                pos = pos.offset(side);
            }

            if (!playerIn.canPlayerEdit(pos, side, stack))
            {
                return false;
            }
            else if (!Blocks.skull.canPlaceBlockAt(worldIn, pos))
            {
                return false;
            }
            else
            {
                if (!worldIn.client)
                {
                    worldIn.setState(pos, Blocks.skull.getState().withProperty(BlockSkull.FACING, side), 3);
                    int i = 0;

                    if (side == Facing.UP)
                    {
                        i = ExtMath.floord((double)(playerIn.rotYaw * 16.0F / 360.0F) + 0.5D) & 15;
                    }

                    TileEntity tileentity = worldIn.getTileEntity(pos);

                    if (tileentity instanceof TileEntitySkull)
                    {
                        TileEntitySkull tileentityskull = (TileEntitySkull)tileentity;

//                        if (stack.getMetadata() == 3)
//                        {
//                        String user = null;
//
//                        if (stack.hasTagCompound())
//                        {
//                            NBTTagCompound nbttagcompound = stack.getTagCompound();
//
//                            if (nbttagcompound.hasKey("SkullOwner", 8) && nbttagcompound.getString("SkullOwner").length() > 0)
//                            {
//                                user = nbttagcompound.getString("SkullOwner");
//                            }
//                        }
//
//                        tileentityskull.setUser(user);
//                        }
//                        else
//                        {
//                            tileentityskull.setType(stack.getMetadata());
//                        }

                        tileentityskull.setSkullRotation(i);
//                        Blocks.skull.checkWitherSpawn(worldIn, pos, tileentityskull);
                    }

                    --stack.stackSize;
                }

                return true;
            }
        }
    }

//    /**
//     * returns a list of items with the same ID, but different meta (eg: dye returns 16 items)
//     */
//    public void getSubItems(Item itemIn, CreativeTabs tab, List<ItemStack> subItems)
//    {
//        for (int i = 0; i < skullTypes.length; ++i)
//        {
//            subItems.add(new ItemStack(itemIn, 1, i));
//        }
//    }

//    /**
//     * Converts the given ItemStack damage value into a metadata value to be placed in the world when this Item is
//     * placed as a Block (mostly used with ItemBlocks).
//     */
//    public int getMetadata(int damage)
//    {
//        return damage;
//    }

//    /**
//     * Returns the unlocalized name of this item. This version accepts an ItemStack so different stacks can have
//     * different names based on their damage or NBT.
//     */
//    public String getUnlocalizedName(ItemStack stack)
//    {
//        int i = stack.getMetadata();
//
//        if (i < 0 || i >= skullTypes.length)
//        {
//            i = 0;
//        }
//
//        return super.getUnlocalizedName() + "." + skullTypes[i];
//    }

//    public String getDisplay(ItemStack stack)
//    {
//        if (stack.hasTagCompound())
//        {
//            if (stack.getTagCompound().hasKey("SkullOwner", 8))
//            {
//                return super.getDisplay(stack) + " von " + stack.getTagCompound().getString("SkullOwner");
//            }
//        }
//
//        return super.getDisplay(stack);
//    }
    
//    public Set<String> getValidTags() {
//    	return Sets.newHashSet("SkullOwner");
//    }
//    
//    protected boolean validateNbt(NBTTagCompound tag) {
//    	if(tag.hasKey("SkullOwner")) {
////    		if(!adv) {
////    			return false;
////    		}
//    		if(!tag.hasKey("SkullOwner", 8)) {
//        		return false;
//        	}
//    	}
//    	return true;
//    }

    public Transforms getTransform() {
    	return Transforms.SKULL;
    }

    public Model getModel(ModelProvider provider, String name, int meta) {
    	return provider.getModel(provider.getEntityModel(), this.getTransform());
    }
}
