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

import gvclib.entity.living.EntityVehicleBase;
import hmggirlfront.mod_GirlFront;
import hmggirlfront.entity.EntityDollBase;
import hmggirlfront.entity.EntityKalina;
import hmggirlfront.entity.mob.EntityELIDBase;
import hmggirlfront.entity.mob.EntityELID_SKELETEN;
import hmggirlfront.entity.mob.EntityELID_ZOMBIE;
import hmggirlfront.entity.mob.EntityIBC01;
import hmggirlfront.entity.mob.EntityIBCBase;
import hmggirlfront.entity.mob.EntitySCMBase;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.monster.IMob;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.PotionEffect;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
 
public class TileEntity_Collapse extends TileEntity implements ITickable
{
	public boolean spawn = false;
	
	public int count = 0;
	
	public int id = 0;
	
    @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();
        
        int han = 2;
        if(this.world.getBlockState(this.pos).getBlock() == mod_GirlFront.b_block_collapse_hv) {
        	han = 4;
        }
        
        EntityLivingBase[] living = this.canspawn(x, y, z, han);
        for(int en = 0; en < living.length; ++en){
        	if(living[en] != null) {
        		EntityLivingBase entity = living[en];
        		boolean flag = false;
        		
        		ItemStack head = entity.getItemStackFromSlot(EntityEquipmentSlot.HEAD);
    			ItemStack chest = entity.getItemStackFromSlot(EntityEquipmentSlot.CHEST);
    			ItemStack legs = entity.getItemStackFromSlot(EntityEquipmentSlot.LEGS);
    			ItemStack feet = entity.getItemStackFromSlot(EntityEquipmentSlot.FEET);
    			
    			if(!head.isEmpty() && head.getItem() == mod_GirlFront.armor_anticollapse_head
    					&& !chest.isEmpty() && chest.getItem() == mod_GirlFront.armor_anticollapse_chest
    					&& !legs.isEmpty() && legs.getItem() == mod_GirlFront.armor_anticollapse_legs
    					&& !feet.isEmpty() && feet.getItem() == mod_GirlFront.armor_anticollapse_feet) {
    				flag = true;
    			}
    			if(!head.isEmpty() && head.getItem() == mod_GirlFront.armor_army_m_head
    					&& !chest.isEmpty() && chest.getItem() == mod_GirlFront.armor_army_m_chest
    					&& !legs.isEmpty() && legs.getItem() == mod_GirlFront.armor_army_m_legs
    					&& !feet.isEmpty() && feet.getItem() == mod_GirlFront.armor_army_m_feet) {
    				flag = true;
    			}
    			if(!head.isEmpty() && head.getItem() == mod_GirlFront.armor_army_h_head
    					&& !chest.isEmpty() && chest.getItem() == mod_GirlFront.armor_army_h_chest
    					&& !legs.isEmpty() && legs.getItem() == mod_GirlFront.armor_army_h_legs
    					&& !feet.isEmpty() && feet.getItem() == mod_GirlFront.armor_army_h_feet) {
    				flag = true;
    			}
    			
    			if (entity instanceof EntityDollBase) {
    				flag = true;
    			}
    			if (entity instanceof EntityIBCBase) {
    				flag = true;
    			}
    			if (entity instanceof EntitySCMBase) {
    				flag = true;
    			}
    			if (entity instanceof EntityELIDBase) {
    				flag = true;
    			}
    			if (entity instanceof EntityKalina) {
    				EntityKalina kalin = (EntityKalina) entity;
    				if(kalin.getArmID_A() == 3) {
    					flag = true;
    				}
    			}
    			if (entity instanceof EntityVehicleBase) {
    				flag = true;
    			}
    			
    			if(!flag) {
    				if (!entity.isPotionActive(mod_GirlFront.collapse_potion))
    					entity.addPotionEffect(
    							new PotionEffect(mod_GirlFront.collapse_potion, 1000, 0, true, true));
    			}
        	}
		}
        /*
		this.canspawn2(x, y, z, 80, 20);
		if (spawn) {
			if (!this.world.isRemote) {
				++count;
				boolean canspawn = false;
				{
					if (count > 600 && this.world.rand.nextInt(100) == 0) {
						canspawn = true;
						count = 0;
					}
				}
				if (canspawn) {
					if (this.world.rand.nextInt(3) == 0) {
						EntityELID_SKELETEN entityskeleton = new EntityELID_SKELETEN(this.world);
						entityskeleton.setLocationAndAngles(x, y + 1, z, 0, 0.0F);
						entityskeleton.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(mod_GirlFront.subarm_ak12prt));
						this.world.spawnEntity(entityskeleton);
					}else {
						EntityELID_ZOMBIE entityskeleton = new EntityELID_ZOMBIE(this.world);
						entityskeleton.setLocationAndAngles(x, y + 1, z, 0, 0.0F);
						this.world.spawnEntity(entityskeleton);
					}
				}
			}
		}*/

	}
	
    public EntityLivingBase[] canspawn(double x, double y, double z, int han) {
    	Entity entity = null;
    	EntityLivingBase[] living = new EntityLivingBase[1280];
    	int en = 0;
    	{
    		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() && entity1 != null)
                    {
                		if (entity1 != null && entity1 instanceof EntityLivingBase) {
                			living[en] = (EntityLivingBase) entity1;
                			++en;
                		}
                    }
                }
            }
    	}
    	return living;
    }
    
public void canspawn2(double x, double y, double z, int han, int han2) {
    		boolean canspawn = false;
    		spawn = false;
        	int mob = 0;
        	
        	/*{
        		Entity entity = null;
            	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 {
                        			canspawn = true;
                        		}
                            }
                        }
                    }
                }
        	}
        	if(canspawn)*/
        	{
        		Entity entity = null;
            	AxisAlignedBB axisalignedbb2 = (new AxisAlignedBB((double)(x-han2), (double)(y-han2), (double)(z-han2), 
            			(double)(x + han2), (double)(y + han2), (double)(z+ han2)))
                		.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 IMob && entity1 != null)
                            {
                    			++mob;
                            }
                        }
                    }
                }
                if(mob < mod_GirlFront.cfg_spawner_limit) {
                	spawn = true;
                }else {
                	spawn = false;
                }
        	}
    	}
    
}