package hmggirlfront.biome;

import com.google.common.base.Predicates;

import hmggirlfront.mod_GirlFront;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.block.BlockLeaves;
import net.minecraft.block.BlockSand;
import net.minecraft.block.BlockSlab;
import net.minecraft.block.BlockStaticLiquid;
import net.minecraft.block.BlockStoneSlab;
import net.minecraft.block.state.IBlockState;
import net.minecraft.block.state.pattern.BlockStateMatcher;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.gen.feature.WorldGenerator;

public class WorldGen_Ruin extends WorldGenerator
{
    private static final BlockStateMatcher IS_SAND = BlockStateMatcher.forBlock(Blocks.SAND).where(BlockSand.VARIANT, Predicates.equalTo(BlockSand.EnumType.SAND));
    private final IBlockState sandSlab = Blocks.STONE_SLAB.getDefaultState().withProperty(BlockStoneSlab.VARIANT, BlockStoneSlab.EnumType.SAND).withProperty(BlockSlab.HALF, BlockSlab.EnumBlockHalf.BOTTOM);
    private final IBlockState sandstone = Blocks.SANDSTONE.getDefaultState();
    private final IBlockState water = Blocks.FLOWING_WATER.getDefaultState();
    
    public String txt = "ruin_building.txt";
    
    public int sizex = 8;
    public int sizez = 8;
    public int offset = 0;
    public boolean setair = false;
    
    public WorldGen_Ruin(String txtbase, int xx, int zz, int off, boolean air) {
    	txt = txtbase;
    	sizex = xx;
    	sizez = zz;
    	setair = air;
    	offset = off;
    }
    

    public boolean generate(World worldIn, Random rand, BlockPos position)
    {
        /*while (worldIn.isAirBlock(position) && position.getY() > 2)
        {
            position = position.down();
        }

        if (!IS_SAND.apply(worldIn.getBlockState(position)))
        {
            return false;
        }
        else
        {
            for (int i = -2; i <= 2; ++i)
            {
                for (int j = -2; j <= 2; ++j)
                {
                    if (worldIn.isAirBlock(position.add(i, -1, j)) && worldIn.isAirBlock(position.add(i, -2, j)))
                    {
                        return false;
                    }
                }
            }

            for (int l = -1; l <= 0; ++l)
            {
                for (int l1 = -2; l1 <= 2; ++l1)
                {
                    for (int k = -2; k <= 2; ++k)
                    {
                        worldIn.setBlockState(position.add(l1, l, k), this.sandstone, 2);
                    }
                }
            }

            worldIn.setBlockState(position, this.water, 2);

            for (EnumFacing enumfacing : EnumFacing.Plane.HORIZONTAL)
            {
                worldIn.setBlockState(position.offset(enumfacing), this.water, 2);
            }

            for (int i1 = -2; i1 <= 2; ++i1)
            {
                for (int i2 = -2; i2 <= 2; ++i2)
                {
                    if (i1 == -2 || i1 == 2 || i2 == -2 || i2 == 2)
                    {
                        worldIn.setBlockState(position.add(i1, 1, i2), this.sandstone, 2);
                    }
                }
            }

            worldIn.setBlockState(position.add(2, 1, 0), this.sandSlab, 2);
            worldIn.setBlockState(position.add(-2, 1, 0), this.sandSlab, 2);
            worldIn.setBlockState(position.add(0, 1, 2), this.sandSlab, 2);
            worldIn.setBlockState(position.add(0, 1, -2), this.sandSlab, 2);

            for (int j1 = -1; j1 <= 1; ++j1)
            {
                for (int j2 = -1; j2 <= 1; ++j2)
                {
                    if (j1 == 0 && j2 == 0)
                    {
                        worldIn.setBlockState(position.add(j1, 4, j2), this.sandstone, 2);
                    }
                    else
                    {
                        worldIn.setBlockState(position.add(j1, 4, j2), this.sandSlab, 2);
                    }
                }
            }

            for (int k1 = 1; k1 <= 3; ++k1)
            {
                worldIn.setBlockState(position.add(-1, k1, -1), this.sandstone, 2);
                worldIn.setBlockState(position.add(-1, k1, 1), this.sandstone, 2);
                worldIn.setBlockState(position.add(1, k1, -1), this.sandstone, 2);
                worldIn.setBlockState(position.add(1, k1, 1), this.sandstone, 2);
            }

            return true;
        }*/
    	
    	
    	/*while (worldIn.isAirBlock(position) && position.getY() > 2)
        {
            position = position.down();
        }*/
    	
    	
    	
    	BlockPos bp = worldIn.getHeight(new BlockPos(position.getX(), 60 + rand.nextInt(100), position.getZ()));
    	int par1 = position.getX();
    	int par2 = bp.getY() + offset;
    	int par3 = position.getZ();
    	
    	BlockPos bp2 = worldIn.getHeight(new BlockPos(position.getX() + sizex, 60 + rand.nextInt(100), position.getZ() + sizez));
    	BlockPos bp3 = worldIn.getHeight(new BlockPos(position.getX(), 60 + rand.nextInt(100), position.getZ() + sizez));
    	BlockPos bp4 = worldIn.getHeight(new BlockPos(position.getX() + sizex, 60 + rand.nextInt(100), position.getZ()));
    	int par22 = bp2.getY();
    	int par23 = bp3.getY();
    	int par24 = bp4.getY();
    	
    	int [] array = {par2, par22, par23, par24};
    	int intMin = array[0];

        for (int i = 1; i < array.length; i++ ) {
            if(intMin > array[i]) {
                intMin = array[i];
            }
        }
    	/*
    	if(par2 < par22 - 1)par2 = par22 - 1;
    	if(par2 < par23 - 1)par2 = par23 - 1;
    	if(par2 < par24 - 1)par2 = par24 - 1;
    	par2 = par2 + offset;*/
        par2 = intMin;
    	
    	if(setair) {
    		for(int xxx = 0; xxx < sizex; ++xxx) {
    			for(int yyy = 0; yyy < sizex; ++yyy) {
    				for(int zzz = 0; zzz < sizez; ++zzz) {
    					worldIn.setBlockState(new BlockPos(par1 + xxx, par2 + yyy, par3 + zzz), Blocks.AIR.getDefaultState(), 2);
    	    		}
        		}
    		}
    	}
    	
    	if(worldIn.getBlockState(new BlockPos(par1, par2-1, par3)).getBlock() instanceof BlockStaticLiquid) {
    		return false;
    	}else {
    		int offsety = 0;
        	try {
    			InputStreamReader packFile = new InputStreamReader(getClass().getClassLoader()
    		            .getResourceAsStream("assets/hmggirlfront/building/collapse/" + txt), "UTF-8");
                {
              	  BufferedReader br = new BufferedReader(packFile);  // ファイルを開く
                
                String str;
                while((str = br.readLine()) != null){  // 1行ずつ読み込む
                  //System.out.println(str);
              	  String[] type = str.split(",");
              	  if (type.length != 0)
                    {
              		  if(type[0].equals("Block")){
              			String blockd = type[1];
              			String blockid = type[2];//Integer.parseInt(type[1]);
              			int x = Integer.parseInt(type[3]);
              			int y = Integer.parseInt(type[4]);
              			int z = Integer.parseInt(type[5]);
              			int meta = Integer.parseInt(type[6]);
              			Block block;
              			boolean chest = false;
              			if(blockid.equals("tilechest")) {
              				block = (Block)Block.REGISTRY.getObject(new ResourceLocation(blockd + ":" + "chest"));
              				chest = true;
              			}else
              			{
              				block = (Block)Block.REGISTRY.getObject(new ResourceLocation(blockd + ":" + blockid));
              			}
              			if (!worldIn.isRemote && block != null)
            	        {
              			//par1World.setBlock(par1+x, par2+y, par3+z, block,meta,2);
              				worldIn.setBlockState(new BlockPos(par1+x, par2+y + offsety, par3+z), block.getStateFromMeta(meta), 2);
              				if(chest){
              					TileEntityChest Chest3;
              					if(worldIn.getTileEntity(new BlockPos(par1+x, par2+y + offsety, par3+z)) != null && worldIn.getTileEntity(new BlockPos(par1+x, par2+y + offsety, par3+z)) instanceof TileEntityChest) {
              					Chest3 = (TileEntityChest) worldIn.getTileEntity(new BlockPos(par1+x, par2+y + offsety, par3+z));
              				}
              				}
            	        }
              		  }
                    }
                }
                //System.out.println(String.format("coll"));
                }
                
            	} catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
        	return true;
    	}
    	
    	
    }
}