package net.minecraft.block;

import java.util.EnumSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;

public class BlockDynamicLiquid extends BlockLiquid
{
    int field_149815_a;
    private static final String __OBFID = "CL_00000234";

    protected BlockDynamicLiquid(Material p_i45403_1_)
    {
        super(p_i45403_1_);
    }

    private void func_180690_f(World worldIn, BlockPos p_180690_2_, IBlockState p_180690_3_)
    {
        worldIn.setBlockState(p_180690_2_, getStaticLiquidForMaterial(this.blockMaterial).getDefaultState().withProperty(LEVEL, p_180690_3_.getValue(LEVEL)), 2);
    }

    public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
    {
        int i = ((Integer)state.getValue(LEVEL)).intValue();
        byte b0 = 1;

        if (this.blockMaterial == Material.lava && !worldIn.provider.func_177500_n())
        {
            b0 = 2;
        }

        int j = this.tickRate(worldIn);
        int i1;

        if (i > 0)
        {
            int k = -100;
            this.field_149815_a = 0;
            EnumFacing enumfacing;

            for (Iterator iterator = EnumFacing.Plane.HORIZONTAL.iterator(); iterator.hasNext(); k = this.func_176371_a(worldIn, pos.offset(enumfacing), k))
            {
                enumfacing = (EnumFacing)iterator.next();
            }

            int l = k + b0;

            if (l >= 8 || k < 0)
            {
                l = -1;
            }

            if (this.func_176362_e(worldIn, pos.offsetUp()) >= 0)
            {
                i1 = this.func_176362_e(worldIn, pos.offsetUp());

                if (i1 >= 8)
                {
                    l = i1;
                }
                else
                {
                    l = i1 + 8;
                }
            }

            if (this.field_149815_a >= 2 && this.blockMaterial == Material.water)
            {
                IBlockState iblockstate2 = worldIn.getBlockState(pos.offsetDown());

                if (iblockstate2.getBlock().getMaterial().isSolid())
                {
                    l = 0;
                }
                else if (iblockstate2.getBlock().getMaterial() == this.blockMaterial && ((Integer)iblockstate2.getValue(LEVEL)).intValue() == 0)
                {
                    l = 0;
                }
            }

            if (this.blockMaterial == Material.lava && i < 8 && l < 8 && l > i && rand.nextInt(4) != 0)
            {
                j *= 4;
            }

            if (l == i)
            {
                this.func_180690_f(worldIn, pos, state);
            }
            else
            {
                i = l;

                if (l < 0)
                {
                    worldIn.setBlockToAir(pos);
                }
                else
                {
                    state = state.withProperty(LEVEL, Integer.valueOf(l));
                    worldIn.setBlockState(pos, state, 2);
                    worldIn.scheduleUpdate(pos, this, j);
                    worldIn.notifyNeighborsOfStateChange(pos, this);
                }
            }
        }
        else
        {
            this.func_180690_f(worldIn, pos, state);
        }

        IBlockState iblockstate1 = worldIn.getBlockState(pos.offsetDown());

        if (this.func_176373_h(worldIn, pos.offsetDown(), iblockstate1))
        {
            if (this.blockMaterial == Material.lava && worldIn.getBlockState(pos.offsetDown()).getBlock().getMaterial() == Material.water)
            {
                worldIn.setBlockState(pos.offsetDown(), Blocks.stone.getDefaultState());
                this.func_180688_d(worldIn, pos.offsetDown());
                return;
            }

            if (i >= 8)
            {
                this.func_176375_a(worldIn, pos.offsetDown(), iblockstate1, i);
            }
            else
            {
                this.func_176375_a(worldIn, pos.offsetDown(), iblockstate1, i + 8);
            }
        }
        else if (i >= 0 && (i == 0 || this.func_176372_g(worldIn, pos.offsetDown(), iblockstate1)))
        {
            Set set = this.func_176376_e(worldIn, pos);
            i1 = i + b0;

            if (i >= 8)
            {
                i1 = 1;
            }

            if (i1 >= 8)
            {
                return;
            }

            Iterator iterator1 = set.iterator();

            while (iterator1.hasNext())
            {
                EnumFacing enumfacing1 = (EnumFacing)iterator1.next();
                this.func_176375_a(worldIn, pos.offset(enumfacing1), worldIn.getBlockState(pos.offset(enumfacing1)), i1);
            }
        }
    }

    private void func_176375_a(World worldIn, BlockPos p_176375_2_, IBlockState p_176375_3_, int p_176375_4_)
    {
        if (this.func_176373_h(worldIn, p_176375_2_, p_176375_3_))
        {
            if (p_176375_3_.getBlock() != Blocks.air)
            {
                if (this.blockMaterial == Material.lava)
                {
                    this.func_180688_d(worldIn, p_176375_2_);
                }
                else
                {
                    p_176375_3_.getBlock().dropBlockAsItem(worldIn, p_176375_2_, p_176375_3_, 0);
                }
            }

            worldIn.setBlockState(p_176375_2_, this.getDefaultState().withProperty(LEVEL, Integer.valueOf(p_176375_4_)), 3);
        }
    }

    private int func_176374_a(World worldIn, BlockPos p_176374_2_, int p_176374_3_, EnumFacing p_176374_4_)
    {
        int j = 1000;
        Iterator iterator = EnumFacing.Plane.HORIZONTAL.iterator();

        while (iterator.hasNext())
        {
            EnumFacing enumfacing1 = (EnumFacing)iterator.next();

            if (enumfacing1 != p_176374_4_)
            {
                BlockPos blockpos1 = p_176374_2_.offset(enumfacing1);
                IBlockState iblockstate = worldIn.getBlockState(blockpos1);

                if (!this.func_176372_g(worldIn, blockpos1, iblockstate) && (iblockstate.getBlock().getMaterial() != this.blockMaterial || ((Integer)iblockstate.getValue(LEVEL)).intValue() > 0))
                {
                    if (!this.func_176372_g(worldIn, blockpos1.offsetDown(), iblockstate))
                    {
                        return p_176374_3_;
                    }

                    if (p_176374_3_ < 4)
                    {
                        int k = this.func_176374_a(worldIn, blockpos1, p_176374_3_ + 1, enumfacing1.getOpposite());

                        if (k < j)
                        {
                            j = k;
                        }
                    }
                }
            }
        }

        return j;
    }

    private Set func_176376_e(World worldIn, BlockPos p_176376_2_)
    {
        int i = 1000;
        EnumSet enumset = EnumSet.noneOf(EnumFacing.class);
        Iterator iterator = EnumFacing.Plane.HORIZONTAL.iterator();

        while (iterator.hasNext())
        {
            EnumFacing enumfacing = (EnumFacing)iterator.next();
            BlockPos blockpos1 = p_176376_2_.offset(enumfacing);
            IBlockState iblockstate = worldIn.getBlockState(blockpos1);

            if (!this.func_176372_g(worldIn, blockpos1, iblockstate) && (iblockstate.getBlock().getMaterial() != this.blockMaterial || ((Integer)iblockstate.getValue(LEVEL)).intValue() > 0))
            {
                int j;

                if (this.func_176372_g(worldIn, blockpos1.offsetDown(), worldIn.getBlockState(blockpos1.offsetDown())))
                {
                    j = this.func_176374_a(worldIn, blockpos1, 1, enumfacing.getOpposite());
                }
                else
                {
                    j = 0;
                }

                if (j < i)
                {
                    enumset.clear();
                }

                if (j <= i)
                {
                    enumset.add(enumfacing);
                    i = j;
                }
            }
        }

        return enumset;
    }

    private boolean func_176372_g(World worldIn, BlockPos p_176372_2_, IBlockState p_176372_3_)
    {
        Block block = worldIn.getBlockState(p_176372_2_).getBlock();
        return !(block instanceof BlockDoor) && block != Blocks.standing_sign && block != Blocks.ladder && block != Blocks.reeds ? (block.blockMaterial == Material.portal ? true : block.blockMaterial.blocksMovement()) : true;
    }

    protected int func_176371_a(World worldIn, BlockPos p_176371_2_, int p_176371_3_)
    {
        int j = this.func_176362_e(worldIn, p_176371_2_);

        if (j < 0)
        {
            return p_176371_3_;
        }
        else
        {
            if (j == 0)
            {
                ++this.field_149815_a;
            }

            if (j >= 8)
            {
                j = 0;
            }

            return p_176371_3_ >= 0 && j >= p_176371_3_ ? p_176371_3_ : j;
        }
    }

    private boolean func_176373_h(World worldIn, BlockPos p_176373_2_, IBlockState p_176373_3_)
    {
        Material material = p_176373_3_.getBlock().getMaterial();
        return material != this.blockMaterial && material != Material.lava && !this.func_176372_g(worldIn, p_176373_2_, p_176373_3_);
    }

    public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
    {
        if (!this.func_176365_e(worldIn, pos, state))
        {
            worldIn.scheduleUpdate(pos, this, this.tickRate(worldIn));
        }
    }
}