package hmggirlfront.biome;

import java.util.Random;

import hmggirlfront.mod_GirlFront;
import net.minecraft.block.Block;
import net.minecraft.block.BlockLiquid;
import net.minecraft.block.material.Material;
import net.minecraft.init.Blocks;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.gen.feature.WorldGenerator;

public class WorldGenCollapse extends WorldGenerator
{
    private final Block block;
    /** The maximum radius used when generating a patch of blocks. */
    private final int radius;

    public WorldGenCollapse(Block p_i45462_1_, int p_i45462_2_)
    {
        this.block = p_i45462_1_;
        this.radius = p_i45462_2_;
    }

    public boolean generate(World worldIn, Random rand, BlockPos position)
    {
        /*if (worldIn.getBlockState(position).getMaterial() != Material.WATER)
        {
            return false;
        }
        else*/
        {
        	/*
            int i = rand.nextInt(this.radius - 2) + 2;
            int i2 = rand.nextInt(this.radius - 2) + 2;
            int j = 2;

            for (int k = position.getX() - i; k <= position.getX() + i; ++k)
            {
                for (int l = position.getZ() - i2; l <= position.getZ() + i2; ++l)
                {
                    int i1 = k - position.getX();
                    int j1 = l - position.getZ();

                    if (i1 * i1 + j1 * j1 <= i * i)
                    {/*
                    	
                    	int k1 = position.getY();
                        //for (int k1 = position.getY() - 2; k1 <= position.getY() + 2; ++k1)
                        {
                            BlockPos blockpos = new BlockPos(k, k1, l);
                            Block block = worldIn.getBlockState(blockpos).getBlock();
                            BlockPos bp = worldIn.getHeight(blockpos);
                            bp = new BlockPos(bp.getX(), bp.getY() - 1, bp.getZ());
                            for(int wa = 0; wa < 20; ++wa) {
                            	if(!(worldIn.getBlockState(new BlockPos(bp.getX(), bp.getY() - wa, bp.getZ())).getBlock() instanceof BlockLiquid)){
                            		bp = new BlockPos(bp.getX(), bp.getY() - wa, bp.getZ());
                            		break;
                                }
                            }
                            

                           // if (block == Blocks.DIRT || block == Blocks.GRASS)
                            {
                                worldIn.setBlockState(bp, this.block.getDefaultState(), 2);
                            }
                        }*/
                    	/* for (int k1 = position.getY() - 2; k1 <= position.getY() + 2; ++k1)
                         {
                             BlockPos blockpos = new BlockPos(k, k1, l);
                             Block block = worldIn.getBlockState(blockpos).getBlock();

                             if (block == mod_GirlFront.b_block_collapse || block == mod_GirlFront.b_block_collapse_grass)
                             {
                                 worldIn.setBlockState(blockpos, this.block.getDefaultState(), 2);
                             }
                         }
                    }
                }
            }*/
        	int i = rand.nextInt(this.radius - 2) + 2;
            int j = 2;

            for (int k = position.getX() - i; k <= position.getX() + i; ++k)
            {
                for (int l = position.getZ() - i; l <= position.getZ() + i; ++l)
                {
                    int i1 = k - position.getX();
                    int j1 = l - position.getZ();

                    if (i1 * i1 + j1 * j1 <= i * i)
                    {
                        for (int k1 = position.getY() - 2; k1 <= position.getY() + 2; ++k1)
                        {
                            BlockPos blockpos = new BlockPos(k, k1, l);
                            Block block = worldIn.getBlockState(blockpos).getBlock();

                            if (block == mod_GirlFront.b_block_collapse || block == mod_GirlFront.b_block_collapse_grass || block == Blocks.DIRT || block == Blocks.GRASS)
                            {
                            	if(rand.nextInt(4) == 0) {
                            		worldIn.setBlockState(blockpos, this.block.getDefaultState(), 2);
                            	}else {
                            		worldIn.setBlockState(blockpos, mod_GirlFront.b_block_collapse.getDefaultState(), 2);
                            	}
                            }
                        }
                    }
                }
            }
            return true;
        }
    }
}