package common.item.block;

import common.block.Block;
import common.block.Material;
import common.block.artificial.BlockDoor;
import common.entity.npc.EntityNPC;
import common.init.Blocks;
import common.item.CheatTab;
import common.item.Item;
import common.item.ItemStack;
import common.util.BlockPos;
import common.util.Facing;
import common.world.State;
import common.world.World;

public class ItemDoor extends Item
{
    private final BlockDoor block;

    public ItemDoor(BlockDoor block)
    {
        this.block = block;
        this.setTab(block.getMaterial() == Material.WOOD ? CheatTab.WOOD : CheatTab.TECHNOLOGY);
    }

    public Block getBlock()
    {
        return this.block;
    }

    /**
     * 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.UP)
        {
            return false;
        }
        else
        {
            State iblockstate = worldIn.getState(pos);
            Block block = iblockstate.getBlock();

            if (!block.isReplaceable(worldIn, pos))
            {
                pos = pos.offset(side);
            }

            if (!playerIn.canPlayerEdit(pos, side, stack))
            {
                return false;
            }
            else if (!this.block.canPlaceBlockAt(worldIn, pos))
            {
                return false;
            }
            else
            {
                placeDoor(worldIn, pos, Facing.fromAngle((double)playerIn.rotYaw), this.block, true);
                --stack.size;
                return true;
            }
        }
    }

    public static void placeDoor(World worldIn, BlockPos pos, Facing facing, Block door, boolean update)
    {
        BlockPos blockpos = pos.offset(facing.rotateY());
        BlockPos blockpos1 = pos.offset(facing.rotateYCCW());
        int i = (worldIn.getState(blockpos1).getBlock().isNormalCube() ? 1 : 0) + (worldIn.getState(blockpos1.up()).getBlock().isNormalCube() ? 1 : 0);
        int j = (worldIn.getState(blockpos).getBlock().isNormalCube() ? 1 : 0) + (worldIn.getState(blockpos.up()).getBlock().isNormalCube() ? 1 : 0);
        boolean flag = worldIn.getState(blockpos1).getBlock() == door || worldIn.getState(blockpos1.up()).getBlock() == door;
        boolean flag1 = worldIn.getState(blockpos).getBlock() == door || worldIn.getState(blockpos.up()).getBlock() == door;
        boolean flag2 = false;

        if (flag && !flag1 || j > i)
        {
            flag2 = true;
        }

        BlockPos blockpos2 = pos.up();
        State iblockstate = door.getState().withProperty(BlockDoor.FACING, facing).withProperty(BlockDoor.HINGE, flag2 ? BlockDoor.EnumHingePosition.RIGHT : BlockDoor.EnumHingePosition.LEFT);
        worldIn.setState(pos, iblockstate.withProperty(BlockDoor.HALF, BlockDoor.EnumDoorHalf.LOWER), 2);
        worldIn.setState(blockpos2, iblockstate.withProperty(BlockDoor.HALF, BlockDoor.EnumDoorHalf.UPPER), 2);
        if(update) {
	        worldIn.notifyNeighborsOfStateChange(pos, door);
	        worldIn.notifyNeighborsOfStateChange(blockpos2, door);
        }
    }
    
    public boolean isMagnetic() {
    	return this.block == Blocks.iron_door;
    }

    public String getDisplay(ItemStack stack)
    {
        return this.block.getDisplay();
    }
}
