package common.block.liquid;

import java.util.Map;

import common.block.Block;
import common.block.Material;
import common.init.BlockRegistry;
import common.init.Blocks;
import common.rng.Random;
import common.util.BlockPos;
import common.util.Facing;
import common.util.Pair;
import common.vars.Vars;
import common.world.State;
import common.world.World;
import common.world.AWorldServer;

public class BlockStaticLiquid extends BlockLiquid
{
    private final BlockDynamicLiquid dynamicBlock;
    
	public BlockStaticLiquid(Material materialIn, boolean opaque, int rate, Object animation, BlockDynamicLiquid flowing)
    {
        super(materialIn, materialIn == Material.LAVA, opaque, rate, animation);
        this.dynamicBlock = flowing;
		flowing.setStaticBlock(this);
		LIQUIDS.add(new Pair<BlockStaticLiquid, BlockDynamicLiquid>(this, flowing));
    }

    public BlockDynamicLiquid getFlowingBlock() {
    	return this.dynamicBlock;
    }

    /**
     * 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)
    {
        worldIn.setState(pos, this.dynamicBlock.getState().withProperty(LEVEL, state.getValue(LEVEL)), 2);
        worldIn.scheduleUpdate(pos, this.dynamicBlock, this.tickRate(worldIn, pos));
    }

    public void updateTick(AWorldServer worldIn, BlockPos pos, State state, Random rand)
    {
        if (Vars.lavaFire && this.material == Material.LAVA)
        {
            if (Vars.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 == Blocks.air)
                        {
                            if (this.isSurroundingBlockFlammable(worldIn, blockpos))
                            {
                                worldIn.setState(blockpos, Blocks.fire.getState());
                                return;
                            }
                        }
                        else if (block.getMaterial().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();
    }

	public void getAnimatedTextures(Map<String, Object> map) {
		map.put(BlockRegistry.getNameFromBlock(this) + "_still", this.animation);
	}
	
	public String getFallbackTexture() {
		if(this.cachedTexture == null)
			this.cachedTexture = BlockRegistry.getNameFromBlock(this) + "_still";
		return this.cachedTexture;
	}
}
