package common.block;

import common.init.Blocks;
import common.init.Config;
import common.material.Material;
import common.rng.Random;
import common.util.BlockPos;
import common.util.Facing;
import common.world.State;
import common.world.World;
import common.world.AWorldServer;

public class BlockStaticLiquid extends BlockLiquid
{
	public BlockStaticLiquid(Material materialIn, boolean opaque, int rate)
    {
        super(materialIn, materialIn == Material.lava, opaque, rate);
//        this.setTickRandomly(false);
//
//        if (materialIn == Material.lava)
//        {
//            this.setTickRandomly(true);
//        }
    }

    /**
     * Called when a neighboring block changes.
     */
    public void onNeighborBlockChange(World worldIn, BlockPos pos, State state, Block neighborBlock)
    {
        if (!this.checkForMixing(worldIn, pos, state))
        {
            this.updateLiquid(worldIn, pos, state);
        }
    }

    private void updateLiquid(World worldIn, BlockPos pos, State state)
    {
        BlockDynamicLiquid blockdynamicliquid = getFlowingBlock(this);
        worldIn.setState(pos, blockdynamicliquid.getState().withProperty(LEVEL, state.getValue(LEVEL)), 2);
        worldIn.scheduleUpdate(pos, blockdynamicliquid, this.tickRate(worldIn, pos));
    }

    public void updateTick(AWorldServer worldIn, BlockPos pos, State state, Random rand)
    {
        if (Config.lavaFire && this.material == Material.lava)
        {
            if (Config.fire)
            {
                int i = rand.zrange(3);

                if (i > 0)
                {
                    BlockPos blockpos = pos;

                    for (int j = 0; j < i; ++j)
                    {
                        blockpos = blockpos.add(rand.zrange(3) - 1, 1, rand.zrange(3) - 1);
                        Block block = worldIn.getState(blockpos).getBlock();

                        if (block.material == Material.air)
                        {
                            if (this.isSurroundingBlockFlammable(worldIn, blockpos))
                            {
                                worldIn.setState(blockpos, Blocks.fire.getState());
                                return;
                            }
                        }
                        else if (block.material.blocksMovement())
                        {
                            return;
                        }
                    }
                }
                else
                {
                    for (int k = 0; k < 3; ++k)
                    {
                        BlockPos blockpos1 = pos.add(rand.zrange(3) - 1, 0, rand.zrange(3) - 1);

                        if (worldIn.isAirBlock(blockpos1.up()) && this.getCanBlockBurn(worldIn, blockpos1))
                        {
                            worldIn.setState(blockpos1.up(), Blocks.fire.getState());
                        }
                    }
                }
            }
        }
    }

    protected boolean isSurroundingBlockFlammable(World worldIn, BlockPos pos)
    {
        for (Facing enumfacing : Facing.values())
        {
            if (this.getCanBlockBurn(worldIn, pos.offset(enumfacing)))
            {
                return true;
            }
        }

        return false;
    }

    private boolean getCanBlockBurn(World worldIn, BlockPos pos)
    {
        return worldIn.getState(pos).getBlock().getMaterial().getCanBurn();
    }
}
