package common.item.block;

import common.block.Block;
import common.block.artificial.BlockSlab;
import common.entity.npc.EntityNPC;
import common.item.ItemStack;
import common.util.BlockPos;
import common.util.Facing;
import common.world.State;
import common.world.World;

public class ItemSlab extends ItemBlock
{
    private final BlockSlab slab;

    public ItemSlab(BlockSlab slab)
    {
        super(slab);
        this.slab = slab;
    }

//    /**
//     * 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)
//    {
//        return this.slab.getUnlocalizedName(stack.getMetadata());
//    }

    /**
     * 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 (stack.size == 0)
        {
            return false;
        }
        else if (!playerIn.canPlayerEdit(pos.offset(side), side, stack))
        {
            return false;
        }
        else
        {
//            Object object = this.slab.getVariant(stack);
            State iblockstate = worldIn.getState(pos);

            if (iblockstate.getBlock() == this.slab)
            {
//                IProperty iproperty = this.slab.getVariantProperty();
//                Comparable comparable = iblockstate.getValue(iproperty);
                Facing blockslab$enumblockhalf = iblockstate.getValue(BlockSlab.FACING);

                if ((side == Facing.UP && blockslab$enumblockhalf == Facing.DOWN || side == Facing.DOWN && blockslab$enumblockhalf == Facing.UP) && !iblockstate.getValue(BlockSlab.DOUBLE))
                {
                    State iblockstate1 = this.slab.getState().withProperty(BlockSlab.DOUBLE, true);

                    if (worldIn.checkNoEntityCollision(this.slab.getCollisionBoundingBox(worldIn, pos, iblockstate1)) && worldIn.setState(pos, iblockstate1, 3))
                    {
                        worldIn.playSound(this.slab.sound.getPlaceSound(), (double)((float)pos.getX() + 0.5F), (double)((float)pos.getY() + 0.5F), (double)((float)pos.getZ() + 0.5F), 1.0F);
                        --stack.size;
                    }

                    return true;
                }
            }

            return this.tryPlace(stack, worldIn, pos.offset(side)) ? true : (this.tryVerticalPlace(stack, playerIn, worldIn, pos, side, hitX, hitY, hitZ) ? true : super.onItemUse(stack, playerIn, worldIn, pos, side, hitX, hitY, hitZ));
        }
    }

    public boolean canPlaceBlockOnSide(World worldIn, BlockPos pos, Facing side, EntityNPC player, ItemStack stack)
    {
        BlockPos blockpos = pos;
//        IProperty iproperty = this.slab.getVariantProperty();
//        Object object = this.slab.getVariant(stack);
        State iblockstate = worldIn.getState(pos);

        if (iblockstate.getBlock() == this.slab && !iblockstate.getValue(BlockSlab.DOUBLE))
        {
            boolean up = iblockstate.getValue(BlockSlab.FACING) == Facing.UP;

            if ((side == Facing.UP && !up || side == Facing.DOWN && up)) // && object == iblockstate.getValue(iproperty))
            {
                return true;
            }
        }

        pos = pos.offset(side);
        State iblockstate1 = worldIn.getState(pos);
        return iblockstate1.getBlock() == this.slab && !iblockstate1.getValue(BlockSlab.DOUBLE) ? true : super.canPlaceBlockOnSide(worldIn, blockpos, side, player, stack);
    }
    
//    private boolean tryPlaceNormal(ItemStack stack, EntityNPC playerIn, World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ) {
//    	if (worldIn.canBlockBePlaced(this.block, pos, false, side, (Entity)null, stack))
//        {
//            int i = this.getMetadata(stack.getMetadata());
//            IBlockState iblockstate1 = this.block.onBlockPlaced(worldIn, pos, side, hitX, hitY, hitZ, i, playerIn);
//
//            if (worldIn.setBlockState(pos, iblockstate1, 3))
//            {
//                iblockstate1 = worldIn.getBlockState(pos);
//
//                if (iblockstate1.getBlock() == this.block)
//                {
//                    setTileEntityNBT(worldIn, playerIn, pos, stack);
//                    this.block.onBlockPlacedBy(worldIn, pos, iblockstate1, playerIn, stack);
//                }
//
//                worldIn.playSound(this.block.stepSound.getPlaceSound(), (double)((float)pos.getX() + 0.5F), (double)((float)pos.getY() + 0.5F), (double)((float)pos.getZ() + 0.5F), (this.block.stepSound.getVolume() + 1.0F) / 2.0F, this.block.stepSound.getFrequency() * 0.8F);
//                --stack.stackSize;
//            }
//
//            return true;
//        }
//    	
//    	return false;
//    }

    private boolean tryPlace(ItemStack stack, World worldIn, BlockPos pos)
    {
        State iblockstate = worldIn.getState(pos);

        if (iblockstate.getBlock() == this.slab)
        {
//            Comparable comparable = iblockstate.getValue(this.slab.getVariantProperty());

            if (!iblockstate.getValue(BlockSlab.DOUBLE))
            {
                State iblockstate1 = this.slab.getState().withProperty(BlockSlab.DOUBLE, true);

                if (worldIn.checkNoEntityCollision(this.slab.getCollisionBoundingBox(worldIn, pos, iblockstate1)) && worldIn.setState(pos, iblockstate1, 3))
                {
                    worldIn.playSound(this.slab.sound.getPlaceSound(), (double)((float)pos.getX() + 0.5F), (double)((float)pos.getY() + 0.5F), (double)((float)pos.getZ() + 0.5F), 1.0F);
                    --stack.size;
                }

                return true;
            }
        }

        return false;
    }
    
    private boolean tryVerticalPlace(ItemStack stack, EntityNPC playerIn, World worldIn, BlockPos pos, Facing side, float hitX, float hitY, float hitZ) {
        if(hitY >= 0.34f && hitY <= 0.66f) {
            State iblockstate = worldIn.getState(pos);
            Block block = iblockstate.getBlock();

            if (!block.isReplaceable(worldIn, pos))
            {
                pos = pos.offset(side);
            }
            
//            BlockVerticalSlab vslab = (stack.getMetadata() & 7) < 4 ? this.verticalSlab1 : this.verticalSlab2;
            
            if (worldIn.canBlockBePlaced(this.slab, pos, false, side, null, stack))
            {
//                int i = this.getMetadata(stack.getMetadata());
                State iblockstate1 = this.slab.getState().withProperty(BlockSlab.FACING, playerIn.getHorizontalFacing()); // .onBlockPlaced(worldIn, pos, side, hitX, hitY, hitZ, i, playerIn);

                if (worldIn.setState(pos, iblockstate1, 3))
                {
                    worldIn.playSound(this.slab.sound.getPlaceSound(), (double)((float)pos.getX() + 0.5F), (double)((float)pos.getY() + 0.5F), (double)((float)pos.getZ() + 0.5F), 1.0F);
                    --stack.size;
                }

                return true;
            }
        }
        
        return false;
    }
}
