package hmggirlfront.block;

import java.util.List;
import java.util.Random;

import javax.annotation.Nullable;

import gvclib.entity.living.EntityVehicleBase;
import hmggirlfront.mod_GirlFront;
import hmggirlfront.block.tile.TileEntity_Collapse;
import hmggirlfront.block.tile.TileEntity_DecoFacingBlock;
import hmggirlfront.block.tile.TileEntity_Goods;
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.EntityIBCBase;
import hmggirlfront.entity.mob.EntitySCMBase;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.MapColor;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
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.init.MobEffects;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.PotionEffect;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class Block_CollapseHV extends Block
{

	protected static final AxisAlignedBB SOUL_SAND_AABB = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.875D, 1.0D);
	
    public Block_CollapseHV()
    {
    	super(Material.ROCK, MapColor.GREEN);
       {
        setHardness(10.0F);
        setSoundType(SoundType.STONE);
        setResistance(10.0F);
        this.setTickRandomly(true);
        }
        //setLightOpacity(1);
        //setLightLevel(1.0F);
    }
    
    /**
     * Returns a new instance of a block's tile entity class. Called on placing the block.
     */
    /*public TileEntity createNewTileEntity(World worldIn, int meta)
    {
        return new TileEntity_Collapse();
    }*/
    
    @Nullable
    public AxisAlignedBB getCollisionBoundingBox(IBlockState blockState, IBlockAccess worldIn, BlockPos pos)
    {
        return SOUL_SAND_AABB;
    }

    public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
    {
        super.updateTick(worldIn, pos, state, rand);

        /*if (!worldIn.isAreaLoaded(pos, 1)) return; // Forge: prevent loading unloaded chunks when checking neighbor's light
        if (worldIn.getLightFromNeighbors(pos.up()) >= 9)
        {
            int i = this.getAge(state);

            if (i < this.getMaxAge())
            {
                float f = getGrowthChance(this, worldIn, pos);

                if(net.minecraftforge.common.ForgeHooks.onCropsGrowPre(worldIn, pos, state, rand.nextInt((int)(25.0F / f) + 1) == 0))
                {
                    worldIn.setBlockState(pos, this.withAge(i + 1), 2);
                    net.minecraftforge.common.ForgeHooks.onCropsGrowPost(worldIn, pos, state, worldIn.getBlockState(pos));
                }
            }
        }*/
        
        
        contagionelid(worldIn, pos, state, rand);
        
        if (!worldIn.isRemote) {
        	entityspawn(worldIn, pos, state, rand);
        }
    }
    
    public void contagionelid(World worldIn, BlockPos pos, IBlockState state, Random rand) {
    	int x = pos.getX();
        int y=  pos.getY();
        int z = pos.getZ();
    	EntityLivingBase[] living = this.canspawn(worldIn, x, y, z, 2);
        for(int en = 0; en < living.length; ++en){
        	if(living[en] != null) {
        		collapse_effect(living[en]);
        	}
		}
    }
    
    public EntityLivingBase[] canspawn(World worldIn, 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 = worldIn.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 entityspawn(World worldIn, BlockPos pos, IBlockState state, Random rand) {
    	int x = pos.getX();
        int y=  pos.getY();
        int z = pos.getZ();
    	if(rand.nextInt(140) == 0) 
    	{
    		if (this.canspawn2(worldIn, x, y, z, 40)) {
    			for(int ka = 0; ka < 6; ++ka) {
    				int ix = worldIn.rand.nextInt(8);
  					int iz = worldIn.rand.nextInt(8);
    				if (worldIn.rand.nextInt(4) == 0) {
    					EntityELID_SKELETEN entityskeleton = new EntityELID_SKELETEN(worldIn);
    					entityskeleton.setLocationAndAngles(x - 4 +ix, y+1, z - 4 +iz, 0, 0.0F);
    					entityskeleton.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(mod_GirlFront.subarm_ak12prt));
    					worldIn.spawnEntity(entityskeleton);
    				}else {
    					EntityELID_ZOMBIE entityskeleton = new EntityELID_ZOMBIE(worldIn);
    					entityskeleton.setLocationAndAngles(x - 4 +ix, y+1, z - 4 +iz, 0, 0.0F);
    					worldIn.spawnEntity(entityskeleton);
    				}
    			}
    		}
    	}
    }
    
    public boolean canspawn2(World worldIn, double x, double y, double z, int han) {
		boolean canspawn = false;
    	int mob = 0;
    	{
    		Entity entity = null;
        	AxisAlignedBB axisalignedbb2 = (new AxisAlignedBB((double)(x-han), (double)(y-han/2), (double)(z-han), 
        			(double)(x + han), (double)(y + han/2), (double)(z+ han)))
            		.grow(1);
            List llist2 = worldIn.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_spawn_limit_s * 3) {
            	canspawn = true;
            }else {
            	canspawn = false;
            }
    	}
    	return canspawn;
	}
    
    /**
     * Called When an Entity Collided with the Block
     */
    public void onEntityCollidedWithBlock(World worldIn, BlockPos pos, IBlockState state, Entity entityIn)
    {
        //entityIn.motionX *= 0.4D;
        //entityIn.motionZ *= 0.4D;
    	if (entityIn != null && entityIn instanceof EntityLivingBase) {
    		collapse_effect(entityIn);
    	}
    }
    
    public void collapse_effect(Entity entityIn){
    	if (entityIn != null && entityIn instanceof EntityLivingBase) {
    		EntityLivingBase living = (EntityLivingBase) entityIn;
    		
    		boolean flag = false;
    		
    		ItemStack head = living.getItemStackFromSlot(EntityEquipmentSlot.HEAD);
			ItemStack chest = living.getItemStackFromSlot(EntityEquipmentSlot.CHEST);
			ItemStack legs = living.getItemStackFromSlot(EntityEquipmentSlot.LEGS);
			ItemStack feet = living.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 (entityIn instanceof EntityDollBase) {
				flag = true;
			}
			if (entityIn instanceof EntityIBCBase) {
				flag = true;
			}
			if (entityIn instanceof EntitySCMBase) {
				flag = true;
			}
			if (entityIn instanceof EntityELIDBase) {
				flag = true;
			}
			if (entityIn instanceof EntityKalina) {
				EntityKalina kalin = (EntityKalina) entityIn;
				if(kalin.getArmID_A() == 3) {
					flag = true;
				}
			}
			if (entityIn instanceof EntityVehicleBase) {
				flag = true;
			}
			
			if(!flag) {
				if (!living.isPotionActive(mod_GirlFront.collapse_potion))
					living.addPotionEffect(
							new PotionEffect(mod_GirlFront.collapse_potion, 1200, 0, true, true));
			}
    		
    	}
    }
    
    
    @SideOnly(Side.CLIENT)
    public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos, Random rand)
    {
        super.randomDisplayTick(stateIn, worldIn, pos, rand);

        if (rand.nextInt(5) == 0)
        {
           worldIn.spawnParticle(EnumParticleTypes.TOWN_AURA, (double)((float)pos.getX() + rand.nextFloat()), (double)((float)pos.getY() + 1.5F), (double)((float)pos.getZ() + rand.nextFloat()), 0.0D, 0.0D, 0.0D);
        }
    }
}