package hmggirlfront.block;
 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import hmggirlfront.mod_GirlFront;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
//import net.minecraft.world.gen.structure.StructureStrongholdPieces;;
 
public class Block_BuildingBase extends Block
{
	public FMLPreInitializationEvent pEvent;
	public String txt;
	public int sizex = 16;
	public int sizez = 16;
	public int sizey = 16;
	public int offsety = 0;
	public int offsetx = 0;
	public int offsetz = 0;
	public boolean setair = false;
	public boolean setair_water = false;
	public boolean setbed = false;
	
    public Block_BuildingBase(String t) {
        super(Material.ROCK);
        txt = t;
        setHardness(1.5F);
        setResistance(1.0F);
        setLightOpacity(1);
        setLightLevel(1.0F);
    }
 
    public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
    {
    	BaseBuilding building = new BaseBuilding();
    	{
    		building.txt = txt;
    		building.sizex = sizex;
    		building.sizez = sizez;
    		building.sizey = sizey;
    		building.offsety = offsety;
    		building.offsetx = offsetx;
    		building.offsetz = offsetz;
    		building.setair = setair;
    		building.setair_water = setair_water;
    		building.setbed = setbed; 
    		building.BuildingBase(worldIn, pos, state);
    	}
    	/*
    	int par1 = pos.getX();
    	int par2 = pos.getY();
    	int par3 = pos.getZ();
    	//if (FMLCommonHandler.instance().getSide().isClient()) 
    	{
    		if(setair) {
        		for (int i0 = 0; i0 < sizex; ++i0){
            		for (int i1 = 0; i1 < sizey; ++i1){
            			for (int i2 = 0; i2 < sizez; ++i2){
            				if(worldIn.getBlockState(new BlockPos(par1+i0, par2+i1 + offsety, par3+i2)).getBlock() != Blocks.AIR){
            					worldIn.setBlockState(new BlockPos(par1+i0, par2+i1 + offsety, par3+i2), Blocks.AIR.getDefaultState(), 2);
            				}
            			}
            		}
            	}
        	}
        	
        	if(setbed){
        		int sety = pos.getY() - 60;
        		if(sety > 0) {
        			for (int i0 = 0; i0 < sizex; ++i0){
        	    		for (int i1 = 0; i1 < sety; ++i1){
        	    			for (int i2 = 0; i2 < sizez; ++i2){
        	    				worldIn.setBlockState(new BlockPos(par1+i0, par2-i1 -1, par3+i2), Blocks.STONE.getDefaultState(), 2);
        	    			}
        	    		}
        	    	}
        		}
        	}
        	
    		try {
    			InputStreamReader packFile = new InputStreamReader(getClass().getClassLoader()
    		            .getResourceAsStream("assets/hmggirlfront/building/" + 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){
              					//BlockBuilding building = new BlockBuilding();
              					//building.ChestGun(worldIn, pos, state, par1 + x, par2 + y, par3 + z);
              					TileEntityChest Chest3;
              					Chest3 = (TileEntityChest) worldIn.getTileEntity(new BlockPos(par1+x, par2+y + offsety, par3+z));
              					{
              						int kazu = worldIn.rand.nextInt(16) + 4;
              						for (int i = 1; i < kazu; ++i) {
                  						int isuu = worldIn.rand.nextInt(5);
                  						int suro2 = worldIn.rand.nextInt(27);
                  						int rand = worldIn.rand.nextInt(4);
                  						if(rand == 0) 
                  						{
                  							Chest3.setInventorySlotContents(suro2, new ItemStack(mod_GirlFront.idoll_bullet, isuu+1, 0));
                  						}
                  						else if(rand == 1) 
                  						{
                  							Chest3.setInventorySlotContents(suro2, new ItemStack(mod_GirlFront.idoll_parts, isuu+1, 0));
                  						}
                  						else if(rand == 2) 
                  						{
                  							Chest3.setInventorySlotContents(suro2, new ItemStack(mod_GirlFront.idoll_hp, isuu+1, 0));
                  						}
                  						else if(rand == 3) 
                  						{
                  							Chest3.setInventorySlotContents(suro2, new ItemStack(mod_GirlFront.idoll_food, isuu+1, 0));
                  						}
                  					}
              					}
              					{
              						int kazu = worldIn.rand.nextInt(6) + 2;
              						for (int i = 1; i < kazu; ++i) {
                  						int isuu = worldIn.rand.nextInt(2);
                  						int suro2 = worldIn.rand.nextInt(27);
                  						int rand = worldIn.rand.nextInt(5);
                  						if(rand == 0) 
                  						{
                  							Chest3.setInventorySlotContents(suro2, new ItemStack(mod_GirlFront.idoll_doll_repair, isuu+1, 0));
                  						}
                  						else if(rand == 1) 
                  						{
                  							Chest3.setInventorySlotContents(suro2, new ItemStack(mod_GirlFront.idoll_doll_contract, isuu+1, 0));
                  						}
                  						else if(rand == 2) 
                  						{
                  							Chest3.setInventorySlotContents(suro2, new ItemStack(mod_GirlFront.idoll_doll_rapidcontract, isuu+1, 0));
                  						}
                  						else if(rand == 3) 
                  						{
                  							Chest3.setInventorySlotContents(suro2, new ItemStack(mod_GirlFront.idoll_doll_report, isuu+1, 0));
                  						}
                  						else if(rand == 4) 
                  						{
                  							Chest3.setInventorySlotContents(suro2, new ItemStack(mod_GirlFront.idoll_l_performance_circuit, isuu+1, 0));
                  						}
                  					}
              					}
              					{
                  						int suro2 = worldIn.rand.nextInt(27);
                  						int rand = worldIn.rand.nextInt(5);
                  						if(rand == 0) 
                  						{
                  							Chest3.setInventorySlotContents(suro2, new ItemStack(mod_GirlFront.idoll_squad_cn, 1, 0));
                  						}
              					}
              					{
              						int kazu = worldIn.rand.nextInt(2) + 2;
              						for (int i = 1; i < kazu; ++i) {
                  						int suro2 = worldIn.rand.nextInt(27);
                  						int rand = worldIn.rand.nextInt(30);
                  						{
                  							int ra = worldIn.rand.nextInt(6);
                  							Item item = mod_GirlFront.core_fn49;
                  							
                  							if(ra == 1){
                  								item = this.getSMG(worldIn, 0 , 7);
                  							}else
                  							if(ra == 2){
                  								item = this.getAR(worldIn, 0 , 7);
                  							}else
                  							if(ra == 3){
                  								item = this.getRF(worldIn, 0 , 7);
                  							}else{
                  								item = this.getHG(worldIn, 0 , 7);
                  							}
                  							Chest3.setInventorySlotContents(suro2, new ItemStack(item, 1, 0));
                  						}
                  						
                  						/*if(rand == 0) 
                  						{
                  							Chest3.setInventorySlotContents(suro2, new ItemStack(mod_GirlFront.core_m1911, 1, 0));
                  						}
                  						else if(rand == 1) 
                  						{
                  							Chest3.setInventorySlotContents(suro2, new ItemStack(mod_GirlFront.core_p38, 1, 0));
                  						}
                  						else if(rand == 2) 
                  						{
                  							Chest3.setInventorySlotContents(suro2, new ItemStack(mod_GirlFront.core_l85a1, 1, 0));
                  						}
                  						else if(rand == 3) 
                  						{
                  							Chest3.setInventorySlotContents(suro2, new ItemStack(mod_GirlFront.core_galil, 1, 0));
                  						}
                  						else if(rand == 4) 
                  						{
                  							Chest3.setInventorySlotContents(suro2, new ItemStack(mod_GirlFront.core_f2000, 1, 0));
                  						}
                  						else if(rand == 5) 
                  						{
                  							Chest3.setInventorySlotContents(suro2, new ItemStack(mod_GirlFront.core_mp40, 1, 0));
                  						}
                  						else if(rand == 6) 
                  						{
                  							Chest3.setInventorySlotContents(suro2, new ItemStack(mod_GirlFront.core_idw, 1, 0));
                  						}
                  						else if(rand == 7) 
                  						{
                  							Chest3.setInventorySlotContents(suro2, new ItemStack(mod_GirlFront.core_mab38, 1, 0));
                  						}
                  						else if(rand == 8) 
                  						{
                  							Chest3.setInventorySlotContents(suro2, new ItemStack(mod_GirlFront.core_fn49, 1, 0));
                  						}
                  						else if(rand == 9) 
                  						{
                  							int rand2 = worldIn.rand.nextInt(5);
                  							if(rand2 == 1) {
                  								Chest3.setInventorySlotContents(suro2, new ItemStack(mod_GirlFront.drone_command, 1, 0));
                  							}else if(rand2 == 2) {
                  								Chest3.setInventorySlotContents(suro2, new ItemStack(mod_GirlFront.drone_mortar, 1, 0));
                  							}else if(rand2 == 3) {
                  								Chest3.setInventorySlotContents(suro2, new ItemStack(mod_GirlFront.drone_airborne, 1, 0));
                  							}
                  						}***
                  					}
              					}
              				}
            	        }
              		  }
                    }
                }
                }
            	} catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
    	}
    	worldIn.setBlockState(new BlockPos(par1+0 + 0, par2+0, par3+0), Blocks.AIR.getDefaultState(), 2);
    	System.out.println(String.format("HMGGVC-" + txt));
    	*/
    }
    
    
    public Item getHG(World world, int rare, int raremax) {
    	Item item = mod_GirlFront.core_m1911;
    	for(int i = 0; i < mod_GirlFront.hg_item.length; ++i) {
    		if(mod_GirlFront.hg_item.length > 0) {
    			int ne = world.rand.nextInt(mod_GirlFront.hg_item.length);
    			if(mod_GirlFront.hg_rare[ne] >= rare && mod_GirlFront.hg_rare[ne] < raremax) {// > 9
            		if (mod_GirlFront.hg_item[ne] != null) {
            			item = mod_GirlFront.hg_item[ne];
            			break;
            		} 
    			}
    		}
    	}
    	return item;
    }
    public Item getSMG(World world, int rare, int raremax) {
    	Item item = mod_GirlFront.core_mp40;
    	for(int i = 0; i < mod_GirlFront.smg_item.length; ++i) {
    		if(mod_GirlFront.smg_item.length > 0) {
    			int ne = world.rand.nextInt(mod_GirlFront.smg_item.length);
    			if(mod_GirlFront.smg_rare[ne] > rare && mod_GirlFront.smg_rare[ne] < raremax) {
            		if (mod_GirlFront.smg_item[ne] != null) {
            			item = mod_GirlFront.smg_item[ne];
            			break;
            		} 
    			}
    		}
    	}
    	return item;
    }
    public Item getAR(World world, int rare, int raremax) {
    	Item item = mod_GirlFront.core_galil;
    	for(int i = 0; i < mod_GirlFront.ar_item.length; ++i) {
    		if(mod_GirlFront.ar_item.length > 0) {
    			int ne = world.rand.nextInt(mod_GirlFront.ar_item.length);
    			if(mod_GirlFront.ar_rare[ne] > rare && mod_GirlFront.ar_rare[ne] < raremax) {
            		if (mod_GirlFront.ar_item[ne] != null) {
            			item = mod_GirlFront.ar_item[ne];
            			break;
            		} 
    			}
    		}
    	}
    	return item;
    }
    public Item getRF(World world, int rare, int raremax) {
    	Item item = mod_GirlFront.core_fn49;
    	for(int i = 0; i < mod_GirlFront.rf_item.length; ++i) {
    		if(mod_GirlFront.rf_item.length > 0) {
    			int ne = world.rand.nextInt(mod_GirlFront.rf_item.length);
    			if(mod_GirlFront.rf_rare[ne] > rare && mod_GirlFront.rf_rare[ne] < raremax) {
            		if (mod_GirlFront.rf_item[ne] != null) {
            			item = mod_GirlFront.rf_item[ne];
            			break;
            		} 
    			}
    		}
    	}
    	return item;
    }
    
    private static boolean checkBeforeReadfile(File file){
	    if (file.exists()){
	      if (file.isFile() && file.canRead()){
	        return true;
	      }
	    }

	    return false;
	  }
    
}