package common.block;

import common.entity.types.EntityLiving;
import common.init.Items;
import common.item.Item;
import common.item.ItemStack;
import common.material.Material;
import common.model.Transforms;
import common.properties.IProperty;
import common.properties.PropertyDirection;
import common.rng.Random;
import common.tileentity.TileEntity;
import common.tileentity.TileEntitySkull;
import common.util.BlockPos;
import common.util.BoundingBox;
import common.util.Facing;
import common.world.IWorldAccess;
import common.world.State;
import common.world.World;
import common.world.AWorldServer;

public class BlockSkull extends BlockContainer
{
    public static final PropertyDirection FACING = PropertyDirection.create("facing");

    public BlockSkull()
    {
        super(Material.circuits);
        this.setDefaultState(this.getBaseState().withProperty(FACING, Facing.NORTH));
        this.setBlockBounds(0.25F, 0.0F, 0.25F, 0.75F, 0.5F, 0.75F);
    }
    
    public boolean isOpaqueCube()
    {
        return false;
    }

    public boolean isFullCube()
    {
        return false;
    }

    public void setBlockBoundsBasedOnState(IWorldAccess worldIn, BlockPos pos)
    {
        switch ((Facing)worldIn.getState(pos).getValue(FACING))
        {
            case UP:
            default:
                this.setBlockBounds(0.25F, 0.0F, 0.25F, 0.75F, 0.5F, 0.75F);
                break;

            case NORTH:
                this.setBlockBounds(0.25F, 0.25F, 0.5F, 0.75F, 0.75F, 1.0F);
                break;

            case SOUTH:
                this.setBlockBounds(0.25F, 0.25F, 0.0F, 0.75F, 0.75F, 0.5F);
                break;

            case WEST:
                this.setBlockBounds(0.5F, 0.25F, 0.25F, 1.0F, 0.75F, 0.75F);
                break;

            case EAST:
                this.setBlockBounds(0.0F, 0.25F, 0.25F, 0.5F, 0.75F, 0.75F);
        }
    }

    public BoundingBox getCollisionBoundingBox(World worldIn, BlockPos pos, State state)
    {
        this.setBlockBoundsBasedOnState(worldIn, pos);
        return super.getCollisionBoundingBox(worldIn, pos, state);
    }
    
    public State onBlockPlaced(World worldIn, BlockPos pos, Facing facing, float hitX, float hitY, float hitZ, int meta, EntityLiving placer)
    {
        return this.getState().withProperty(FACING, placer.getHorizontalFacing());
    }
    
    public TileEntity createNewTileEntity(World worldIn, int meta)
    {
        return new TileEntitySkull();
    }

    public Item getItem(World worldIn, BlockPos pos)
    {
        return Items.skull;
    }
    
    public void dropBlockAsItemWithChance(World worldIn, BlockPos pos, State state, float chance, int fortune)
    {
    }

//    public void onBlockHarvested(World worldIn, BlockPos pos, State state, EntityNPC player)
//    {
//        if (player.creative)
//        {
//            if(!worldIn.client)
//            	worldIn.removeTileEntity(pos);
//        }
//
//        super.onBlockHarvested(worldIn, pos, state, player);
//    }

    public void onBlockRemoved(AWorldServer worldIn, BlockPos pos, State state)
    {
        if (!worldIn.client)
        {
            TileEntity tileentity = worldIn.getTileEntity(pos);
            if (tileentity instanceof TileEntitySkull)
            {
//                TileEntitySkull tileentityskull = (TileEntitySkull)tileentity;
                ItemStack itemstack = new ItemStack(Items.skull, 1, 0);
//                if (tileentityskull.getUser() != null)
//                {
//                    itemstack.setTagCompound(new NBTTagCompound());
//                    itemstack.getTagCompound().setString("SkullOwner", tileentityskull.getUser());
//                }
                spawnAsEntity(worldIn, pos, itemstack);
            }
            super.onBlockRemoved(worldIn, pos, state);
        }
    }
    
    public Item getItemDropped(State state, Random rand, int fortune)
    {
        return Items.skull;
    }
    
    public State getStateFromMeta(int meta)
    {
        return this.getState().withProperty(FACING, Facing.getFront(meta));
    }
    
    public int getMetaFromState(State state)
    {
        return ((Facing)state.getValue(FACING)).getIndex();
    }

    protected IProperty[] getProperties()
    {
        return new IProperty[] {FACING};
    }
    
    public boolean isXrayVisible()
    {
        return true;
    }

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