package hmggirlfront.block.tile;
 
import java.util.List;

import gvclib.world.GVCExplosionBase;
import hmggirlfront.mod_GirlFront;
import hmggirlfront.block.Block_BaseBase;
import hmggirlfront.entity.mob.EntityIBCBoss01;
import hmggirlfront.entity.mob.EntityIBCBoss02;
import hmggirlfront.entity.mob.EntityIBCBoss03;
import hmggirlfront.entity.mob.EntityIBCBoss04;
import hmggirlfront.entity.mob.EntityIBCBoss05;
import hmggirlfront.entity.mob.EntityIBCBoss06;
import hmggirlfront.entity.mob.EntityIBCBoss07;
import hmggirlfront.entity.mob.EntityIBCBoss08;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
 
public class TileEntity_Spawn_Boss extends TileEntity implements ITickable
{
	public boolean spawn = false;
    
	@SideOnly(Side.CLIENT)
    public double getMaxRenderDistanceSquared()
    {
        return 65536.0D;
    }
	
    @Override
	public void update() {
    	Block block = this.world.getBlockState(this.pos).getBlock();
        
        int x = this.pos.getX();
        int y= this.pos.getY();
        int z = this.pos.getZ();
        
        this.canspawn(x, y, z, 40);
        
        
        if(spawn && !this.world.isRemote){
        		
        		if(this.world.getBlockState(this.pos).getBlock() == mod_GirlFront.bdoll_ibc_boss01) {
        			EntityIBCBoss01 entityskeleton = new EntityIBCBoss01(this.world);
		        	entityskeleton.setLocationAndAngles(x + 0.5, y+1, z + 0.5, 0, 0.0F);
		        	entityskeleton.setMoveT(1);
					entityskeleton.setMoveX(x);
					entityskeleton.setMoveY(y);
					entityskeleton.setMoveZ(z);
					entityskeleton.setcanDespawn(1);
			        this.world.spawnEntity(entityskeleton);
		        }
        		if(this.world.getBlockState(this.pos).getBlock() == mod_GirlFront.bdoll_ibc_boss02) {
        			EntityIBCBoss02 entityskeleton = new EntityIBCBoss02(this.world);
		        	entityskeleton.setLocationAndAngles(x + 0.5, y+1, z + 0.5, 0, 0.0F);
		        	entityskeleton.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(mod_GirlFront.dollgun_ibcboss02sw));
		        	entityskeleton.setItemStackToSlot(EntityEquipmentSlot.OFFHAND, new ItemStack(mod_GirlFront.dollgun_ibcboss02hg));
		        	entityskeleton.setMoveT(1);
					entityskeleton.setMoveX(x);
					entityskeleton.setMoveY(y);
					entityskeleton.setMoveZ(z);
					entityskeleton.setcanDespawn(1);
			        this.world.spawnEntity(entityskeleton);
		        }
        		if(this.world.getBlockState(this.pos).getBlock() == mod_GirlFront.bdoll_ibc_boss03) {
        			EntityIBCBoss03 entityskeleton = new EntityIBCBoss03(this.world);
		        	entityskeleton.setLocationAndAngles(x + 0.5, y+1, z + 0.5, 0, 0.0F);
		        	entityskeleton.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(mod_GirlFront.dollgun_ibcboss02hg));
		        	entityskeleton.setItemStackToSlot(EntityEquipmentSlot.OFFHAND, new ItemStack(mod_GirlFront.dollgun_ibcboss02hg));
		        	entityskeleton.setMoveT(1);
					entityskeleton.setMoveX(x);
					entityskeleton.setMoveY(y);
					entityskeleton.setMoveZ(z);
					entityskeleton.setcanDespawn(1);
			        this.world.spawnEntity(entityskeleton);
		        }
        		if(this.world.getBlockState(this.pos).getBlock() == mod_GirlFront.bdoll_ibc_boss04) {
        			EntityIBCBoss04 entityskeleton = new EntityIBCBoss04(this.world);
		        	entityskeleton.setLocationAndAngles(x + 0.5, y+1, z + 0.5, 0, 0.0F);
		        	entityskeleton.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(mod_GirlFront.dollgun_ibcboss04glg));
		        	entityskeleton.setMoveT(1);
					entityskeleton.setMoveX(x);
					entityskeleton.setMoveY(y);
					entityskeleton.setMoveZ(z);
					entityskeleton.setcanDespawn(1);
			        this.world.spawnEntity(entityskeleton);
			        
			        for(int r = 0; r < 15; ++r) {
		        		for(int s = 0; s < 360; ++s) {
			        		for(int c = 0; c < 360; ++c) {
			        			//double r = 20;
			        			
			        			double rad = (2 * (float) Math.PI / 360);
			        			
				        		int xx = (int) (r * Math.sin(s * rad) * Math.cos(c * rad));
				        		int yy = (int) (r * Math.sin(s * rad) * Math.sin(c * rad));
				        		int zz = (int) (r * Math.cos(s * rad));
			        			if(world.getBlockState(new BlockPos(x + xx, y + yy - 2, z + zz)).getBlock() != Blocks.AIR && yy > 0 
			        					&& !(world.getBlockState(new BlockPos(x + xx, y + yy - 2, z + zz)).getBlock() instanceof Block_BaseBase)) {
			        				world.setBlockState(new BlockPos(x + xx, y + yy - 2, z + zz), Blocks.AIR.getDefaultState(), 2);
			        			}
				        	}
			        	}
		        	}
					GVCExplosionBase.ExplosionKai(entityskeleton, entityskeleton, x, y, z, 5, false,  true);
		        }
        		if(this.world.getBlockState(this.pos).getBlock() == mod_GirlFront.bdoll_ibc_boss05) {
        			EntityIBCBoss05 entityskeleton = new EntityIBCBoss05(this.world);
		        	entityskeleton.setLocationAndAngles(x + 0.5, y+1, z + 0.5, 0, 0.0F);
		        	entityskeleton.setMoveT(1);
					entityskeleton.setMoveX(x);
					entityskeleton.setMoveY(y);
					entityskeleton.setMoveZ(z);
					entityskeleton.setcanDespawn(1);
			        this.world.spawnEntity(entityskeleton);
		        }
        		if(this.world.getBlockState(this.pos).getBlock() == mod_GirlFront.bdoll_ibc_boss06) {
        			EntityIBCBoss06 entityskeleton = new EntityIBCBoss06(this.world);
		        	entityskeleton.setLocationAndAngles(x + 0.5, y+1, z + 0.5, 0, 0.0F);
		        	entityskeleton.setMoveT(1);
					entityskeleton.setMoveX(x);
					entityskeleton.setMoveY(y);
					entityskeleton.setMoveZ(z);
					entityskeleton.setcanDespawn(1);
			        this.world.spawnEntity(entityskeleton);
		        }
        		if(this.world.getBlockState(this.pos).getBlock() == mod_GirlFront.bdoll_ibc_boss07) {
        			EntityIBCBoss07 entityskeleton = new EntityIBCBoss07(this.world);
		        	entityskeleton.setLocationAndAngles(x + 0.5, y+1, z + 0.5, 0, 0.0F);
		        	entityskeleton.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(mod_GirlFront.dollgun_ibcboss07rf));
		        	entityskeleton.setMoveT(1);
					entityskeleton.setMoveX(x);
					entityskeleton.setMoveY(y);
					entityskeleton.setMoveZ(z);
					entityskeleton.setcanDespawn(1);
			        this.world.spawnEntity(entityskeleton);
		        }
        		if(this.world.getBlockState(this.pos).getBlock() == mod_GirlFront.bdoll_ibc_boss08) {
        			EntityIBCBoss08 entityskeleton = new EntityIBCBoss08(this.world);
		        	entityskeleton.setLocationAndAngles(x + 0.5, y+1, z + 0.5, 0, 0.0F);
		        	entityskeleton.setMoveT(1);
					entityskeleton.setMoveX(x);
					entityskeleton.setMoveY(y);
					entityskeleton.setMoveZ(z);
					entityskeleton.setcanDespawn(1);
			        this.world.spawnEntity(entityskeleton);
		        }
            if(!this.world.isRemote){
            	this.world.setBlockToAir(this.pos);
            }
        }
	}
	
    public void canspawn(double x, double y, double z, int han) {
    	Entity entity = null;
    	boolean player = false;
    	AxisAlignedBB axisalignedbb2 = (new AxisAlignedBB((double)(x-han), (double)(y-han), (double)(z-han), 
    			(double)(x + han), (double)(y + han), (double)(z+ han)))
        		.grow(1);
        List llist2 = this.world.getEntitiesWithinAABBExcludingEntity(entity, axisalignedbb2);
        if(llist2!=null){
            for (int lj = 0; lj < llist2.size(); lj++) {
            	Entity entity1 = (Entity)llist2.get(lj);
            	if (entity1.canBeCollidedWith())
                {
            		if (entity1 instanceof EntityPlayer && entity1 != null)
                    {
            			EntityPlayer ss = (EntityPlayer) entity1;
            			ItemStack itemstack = ss.getItemStackFromSlot(EntityEquipmentSlot.HEAD);
                		if(!itemstack.isEmpty() && itemstack.getItem() == Items.GOLDEN_HELMET){
                		}else {
                			player = true;
                		}
                    }
                }
            }
        }
        if(player) {
        	for(int xx = 0; xx < han; ++xx) {
        		for(int yy = 0; yy < han; ++yy) {
        			for(int zz = 0; zz < han; ++zz) {
        				int hanbun = han / 2;
        				if(this.world.getBlockState(new BlockPos(x - hanbun + xx, y - hanbun + yy, z - hanbun + zz)).getBlock() instanceof Block_BaseBase) {
        					TileEntity tileentity = this.world.getTileEntity(new BlockPos(x - hanbun + xx, y - hanbun + yy, z - hanbun + zz));
        		            if (tileentity != null && tileentity instanceof TileEntity_BaseBase)
        		            {
        		            	TileEntity_BaseBase tile = (TileEntity_BaseBase) tileentity;
        		            	if(tile.base_boss && tile.enemy && tile.invcount >= tile.invmaxcount / 2) {
        		            		spawn = true;
        		            	}
        		            }
        				}
                	}
            	}
        	}
        }
        
        
    }
    
}