package net.minecraft.block;

import java.util.Iterator;
import java.util.List;
import java.util.Random;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumWorldBlockLayer;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class BlockTripWire extends Block
{
    public static final PropertyBool POWERED_PROP = PropertyBool.create("powered");
    public static final PropertyBool SUSPENDED_PROP = PropertyBool.create("suspended");
    public static final PropertyBool ATTACHED_PROP = PropertyBool.create("attached");
    public static final PropertyBool DISARMED_PROP = PropertyBool.create("disarmed");
    public static final PropertyBool NORTH_DIRECTION_PROP = PropertyBool.create("north");
    public static final PropertyBool EAST_DIRECTION_PROP = PropertyBool.create("east");
    public static final PropertyBool SOUTH_DIRECTION_PROP = PropertyBool.create("south");
    public static final PropertyBool WEST_DIRECTION_PROP = PropertyBool.create("west");
    private static final String __OBFID = "CL_00000328";

    public BlockTripWire()
    {
        super(Material.circuits);
        this.setDefaultState(this.blockState.getBaseState().withProperty(POWERED_PROP, Boolean.valueOf(false)).withProperty(SUSPENDED_PROP, Boolean.valueOf(false)).withProperty(ATTACHED_PROP, Boolean.valueOf(false)).withProperty(DISARMED_PROP, Boolean.valueOf(false)).withProperty(NORTH_DIRECTION_PROP, Boolean.valueOf(false)).withProperty(EAST_DIRECTION_PROP, Boolean.valueOf(false)).withProperty(SOUTH_DIRECTION_PROP, Boolean.valueOf(false)).withProperty(WEST_DIRECTION_PROP, Boolean.valueOf(false)));
        this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.15625F, 1.0F);
        this.setTickRandomly(true);
    }

    /**
     * Get the actual Block state of this Block at the given position. This applies properties not visible in the
     * metadata, such as fence connections.
     */
    public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos)
    {
        return state.withProperty(NORTH_DIRECTION_PROP, Boolean.valueOf(func_176287_c(worldIn, pos, state, EnumFacing.NORTH))).withProperty(EAST_DIRECTION_PROP, Boolean.valueOf(func_176287_c(worldIn, pos, state, EnumFacing.EAST))).withProperty(SOUTH_DIRECTION_PROP, Boolean.valueOf(func_176287_c(worldIn, pos, state, EnumFacing.SOUTH))).withProperty(WEST_DIRECTION_PROP, Boolean.valueOf(func_176287_c(worldIn, pos, state, EnumFacing.WEST)));
    }

    public AxisAlignedBB getCollisionBoundingBox(World worldIn, BlockPos pos, IBlockState state)
    {
        return null;
    }

    public boolean isOpaqueCube()
    {
        return false;
    }

    public boolean isFullCube()
    {
        return false;
    }

    @SideOnly(Side.CLIENT)
    public EnumWorldBlockLayer getBlockLayer()
    {
        return EnumWorldBlockLayer.TRANSLUCENT;
    }

    /**
     * Get the Item that this Block should drop when harvested.
     *  
     * @param fortune the level of the Fortune enchantment on the player's tool
     */
    public Item getItemDropped(IBlockState state, Random rand, int fortune)
    {
        return Items.string;
    }

    public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
    {
        boolean flag = ((Boolean)state.getValue(SUSPENDED_PROP)).booleanValue();
        boolean flag1 = !World.doesBlockHaveSolidTopSurface(worldIn, pos.offsetDown());

        if (flag != flag1)
        {
            this.dropBlockAsItem(worldIn, pos, state, 0);
            worldIn.setBlockToAir(pos);
        }
    }

    public void setBlockBoundsBasedOnState(IBlockAccess access, BlockPos pos)
    {
        IBlockState iblockstate = access.getBlockState(pos);
        boolean flag = ((Boolean)iblockstate.getValue(ATTACHED_PROP)).booleanValue();
        boolean flag1 = ((Boolean)iblockstate.getValue(SUSPENDED_PROP)).booleanValue();

        if (!flag1)
        {
            this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.09375F, 1.0F);
        }
        else if (!flag)
        {
            this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.5F, 1.0F);
        }
        else
        {
            this.setBlockBounds(0.0F, 0.0625F, 0.0F, 1.0F, 0.15625F, 1.0F);
        }
    }

    @SideOnly(Side.CLIENT)
    public Item getItem(World worldIn, BlockPos pos)
    {
        return Items.string;
    }

    public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
    {
        state = state.withProperty(SUSPENDED_PROP, Boolean.valueOf(!World.doesBlockHaveSolidTopSurface(worldIn, pos.offsetDown())));
        worldIn.setBlockState(pos, state, 3);
        this.func_176286_e(worldIn, pos, state);
    }

    public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
    {
        this.func_176286_e(worldIn, pos, state.withProperty(POWERED_PROP, Boolean.valueOf(true)));
    }

    public void onBlockHarvested(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn)
    {
        if (!worldIn.isRemote)
        {
            if (playerIn.getCurrentEquippedItem() != null && playerIn.getCurrentEquippedItem().getItem() == Items.shears)
            {
                worldIn.setBlockState(pos, state.withProperty(DISARMED_PROP, Boolean.valueOf(true)), 4);
            }
        }
    }

    private void func_176286_e(World worldIn, BlockPos p_176286_2_, IBlockState p_176286_3_)
    {
        EnumFacing[] aenumfacing = new EnumFacing[] {EnumFacing.SOUTH, EnumFacing.WEST};
        int i = aenumfacing.length;
        int j = 0;

        while (j < i)
        {
            EnumFacing enumfacing = aenumfacing[j];
            int k = 1;

            while (true)
            {
                if (k < 42)
                {
                    BlockPos blockpos1 = p_176286_2_.offset(enumfacing, k);
                    IBlockState iblockstate1 = worldIn.getBlockState(blockpos1);

                    if (iblockstate1.getBlock() == Blocks.tripwire_hook)
                    {
                        if (iblockstate1.getValue(BlockTripWireHook.FACING_PROP) == enumfacing.getOpposite())
                        {
                            Blocks.tripwire_hook.func_176260_a(worldIn, blockpos1, iblockstate1, false, true, k, p_176286_3_);
                        }
                    }
                    else if (iblockstate1.getBlock() == Blocks.tripwire)
                    {
                        ++k;
                        continue;
                    }
                }

                ++j;
                break;
            }
        }
    }

    /**
     * Called When an Entity Collided with the Block
     */
    public void onEntityCollidedWithBlock(World worldIn, BlockPos pos, IBlockState state, Entity entityIn)
    {
        if (!worldIn.isRemote)
        {
            if (!((Boolean)state.getValue(POWERED_PROP)).booleanValue())
            {
                this.func_176288_d(worldIn, pos);
            }
        }
    }

    /**
     * Called randomly when setTickRandomly is set to true (used by e.g. crops to grow, etc.)
     */
    public void randomTick(World worldIn, BlockPos pos, IBlockState state, Random random) {}

    public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
    {
        if (!worldIn.isRemote)
        {
            if (((Boolean)worldIn.getBlockState(pos).getValue(POWERED_PROP)).booleanValue())
            {
                this.func_176288_d(worldIn, pos);
            }
        }
    }

    private void func_176288_d(World worldIn, BlockPos p_176288_2_)
    {
        IBlockState iblockstate = worldIn.getBlockState(p_176288_2_);
        boolean flag = ((Boolean)iblockstate.getValue(POWERED_PROP)).booleanValue();
        boolean flag1 = false;
        List list = worldIn.getEntitiesWithinAABBExcludingEntity((Entity)null, new AxisAlignedBB((double)p_176288_2_.getX() + this.minX, (double)p_176288_2_.getY() + this.minY, (double)p_176288_2_.getZ() + this.minZ, (double)p_176288_2_.getX() + this.maxX, (double)p_176288_2_.getY() + this.maxY, (double)p_176288_2_.getZ() + this.maxZ));

        if (!list.isEmpty())
        {
            Iterator iterator = list.iterator();

            while (iterator.hasNext())
            {
                Entity entity = (Entity)iterator.next();

                if (!entity.doesEntityNotTriggerPressurePlate())
                {
                    flag1 = true;
                    break;
                }
            }
        }

        if (flag1 != flag)
        {
            iblockstate = iblockstate.withProperty(POWERED_PROP, Boolean.valueOf(flag1));
            worldIn.setBlockState(p_176288_2_, iblockstate, 3);
            this.func_176286_e(worldIn, p_176288_2_, iblockstate);
        }

        if (flag1)
        {
            worldIn.scheduleUpdate(p_176288_2_, this, this.tickRate(worldIn));
        }
    }

    public static boolean func_176287_c(IBlockAccess p_176287_0_, BlockPos p_176287_1_, IBlockState p_176287_2_, EnumFacing p_176287_3_)
    {
        BlockPos blockpos1 = p_176287_1_.offset(p_176287_3_);
        IBlockState iblockstate1 = p_176287_0_.getBlockState(blockpos1);
        Block block = iblockstate1.getBlock();

        if (block == Blocks.tripwire_hook)
        {
            EnumFacing enumfacing1 = p_176287_3_.getOpposite();
            return iblockstate1.getValue(BlockTripWireHook.FACING_PROP) == enumfacing1;
        }
        else if (block == Blocks.tripwire)
        {
            boolean flag = ((Boolean)p_176287_2_.getValue(SUSPENDED_PROP)).booleanValue();
            boolean flag1 = ((Boolean)iblockstate1.getValue(SUSPENDED_PROP)).booleanValue();
            return flag == flag1;
        }
        else
        {
            return false;
        }
    }

    /**
     * Convert the given metadata into a BlockState for this Block
     */
    public IBlockState getStateFromMeta(int meta)
    {
        return this.getDefaultState().withProperty(POWERED_PROP, Boolean.valueOf((meta & 1) > 0)).withProperty(SUSPENDED_PROP, Boolean.valueOf((meta & 2) > 0)).withProperty(ATTACHED_PROP, Boolean.valueOf((meta & 4) > 0)).withProperty(DISARMED_PROP, Boolean.valueOf((meta & 8) > 0));
    }

    /**
     * Convert the BlockState into the correct metadata value
     */
    public int getMetaFromState(IBlockState state)
    {
        int i = 0;

        if (((Boolean)state.getValue(POWERED_PROP)).booleanValue())
        {
            i |= 1;
        }

        if (((Boolean)state.getValue(SUSPENDED_PROP)).booleanValue())
        {
            i |= 2;
        }

        if (((Boolean)state.getValue(ATTACHED_PROP)).booleanValue())
        {
            i |= 4;
        }

        if (((Boolean)state.getValue(DISARMED_PROP)).booleanValue())
        {
            i |= 8;
        }

        return i;
    }

    protected BlockState createBlockState()
    {
        return new BlockState(this, new IProperty[] {POWERED_PROP, SUSPENDED_PROP, ATTACHED_PROP, DISARMED_PROP, NORTH_DIRECTION_PROP, EAST_DIRECTION_PROP, WEST_DIRECTION_PROP, SOUTH_DIRECTION_PROP});
    }
}