package hmggirlfront.entity.mob;

import java.util.List;

import gvclib.entity.EntityP_Particle;
import gvclib.entity.living.EntityGVCLivingBase;
import gvclib.entity.living.ISoldier;
import hmggirlfront.GFSoundEvent;
import hmggirlfront.mod_GirlFront;
import hmggirlfront.event.level.WorldDifficultyLevel_new;
import hmggirlfront.network.GFClientMessageKeyPressed;
import hmggirlfront.network.GFPacketHandler;
import hmggirlfront.world.WorldDifficultyLevel;
import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.EnumCreatureAttribute;
import net.minecraft.entity.monster.EntityGolem;
import net.minecraft.entity.monster.IMob;
import net.minecraft.entity.passive.EntityVillager;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.init.MobEffects;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.PotionEffect;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.util.DamageSource;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.EnumDifficulty;
import net.minecraft.world.EnumSkyBlock;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class EntityELIDBase extends EntityGVCLivingBase implements IMob
{
	
	public boolean boss_spawn = false;
	
	public boolean boss = false;
	
    public EntityELIDBase(World worldIn)
    {
        super(worldIn);
    }

    public boolean CanAttack(Entity entity){
    	/*if(!(entity instanceof EntityELIDBase) && entity instanceof EntityGolem && ((EntityLivingBase) entity).getHealth() > 0.0F){
    		return true;
    	}*/
    	/*else if((entity instanceof ISoldier || entity instanceof EntityGolem || entity instanceof EntityVillager) && ((EntityLivingBase) entity).getHealth() > 0.0F){
    		return true;
    	}
    	else if((entity instanceof EntityIBCBase) && ((EntityLivingBase) entity).getHealth() > 0.0F){
    		return true;
    	}*/
    	if(entity instanceof EntityELIDBase){
    		return false;
    	}
    	else if(entity instanceof EntityPlayer && ((EntityLivingBase) entity).getHealth() > 0.0F){
    		EntityPlayer entityplayer = (EntityPlayer) entity;
    		ItemStack itemstack = entityplayer.getItemStackFromSlot(EntityEquipmentSlot.HEAD);
    		if(!itemstack.isEmpty() && itemstack.getItem() == Items.GOLDEN_HELMET){
    			return false;
    		}else{
    			return true;
    		}
    	}
    	else if(entity instanceof EntityLiving){
    		return true;
    	}
    	else{
    		return false;
    	}
    }
    
    protected void entityInit() {
		super.entityInit();
	}

    @SideOnly(Side.CLIENT)
    public boolean getClient_Reload(EntityELIDBase entity)
    {
        return entity.getRemain_L() <= 0;
    }
    
    @SideOnly(Side.CLIENT)
    public int getClient_Gun_Count(EntityELIDBase entity)
    {
        return entity.gun_count1;
    }
    
    @SideOnly(Side.CLIENT)
    public int getClient_cooltime(EntityELIDBase entity)
    {
        return entity.cooltime;
    }
    
    @SideOnly(Side.CLIENT)
    public int getClient_cooltime2(EntityELIDBase entity)
    {
        return entity.cooltime2;
    }
    
    
    
	/**
	 * Get this Entity's EnumCreatureAttribute
	 */
	public EnumCreatureAttribute getCreatureAttribute() {
		return EnumCreatureAttribute.UNDEAD;
	}
    
    public void onUpdate()
    {
        super.onUpdate();

        if (!this.world.isRemote && this.world.getDifficulty() == EnumDifficulty.PEACEFUL)
        {
            this.setDead();
        }
        
        if(boss){
        	if(FMLCommonHandler.instance().getMinecraftServerInstance() != null) {
    			for (EntityPlayerMP player : FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayers())
                {
    				GFPacketHandler.INSTANCE2.sendTo(new GFClientMessageKeyPressed(500,  this.getEntityId(), this.getRemain_L()), player);
    				GFPacketHandler.INSTANCE2.sendTo(new GFClientMessageKeyPressed(501,  this.getEntityId(), this.gun_count1), player);
    				GFPacketHandler.INSTANCE2.sendTo(new GFClientMessageKeyPressed(502,  this.getEntityId(), this.cooltime), player);
    				GFPacketHandler.INSTANCE2.sendTo(new GFClientMessageKeyPressed(503,  this.getEntityId(), this.cooltime2), player);
                }
    		}
        }
    }
    
    @Override
    public void targetValidtime_Change() {
    	super.targetValidtime_Change();
    	/*if(! this.world.isRemote){
			EntityP_Particle particle = new EntityP_Particle( this.world);
			particle.setLocationAndAngles( this.posX,  this.posY+ this.height + 1,  this.posZ,  this.rotationYaw, 0.0F);
			particle.setModel("hmggirlfront:textures/particle/find.mqo");
			particle.setTex("hmggirlfront:textures/particle/find_null.png");
			particle.startposX =  this.posX;
			particle.startposY =  this.posY+ this.height + 1;
			particle.startposZ =  this.posZ;
			particle.timemax = 20;
			 this.world.spawnEntity(particle);
		}*/
    }
    
    public int flagtime = 0;
    
    public void doblock(EntityELIDBase entity, double range, Block block) {
    	if(!this.getattacktask() && entity.targetentity == null) {
    		boolean flag = true;
    		{
    			List<Entity> llist = entity.world.getEntitiesWithinAABBExcludingEntity(entity, entity
    					.getEntityBoundingBox().expand(entity.motionX, entity.motionY, entity.motionZ).grow(range));
    			if (llist != null) {
    				for (int lj = 0; lj < llist.size(); lj++) {
    					Entity entity1 = (Entity) llist.get(lj);
    					if (entity1.canBeCollidedWith()) {
    						boolean see = entity.getEntitySenses().canSee(entity1);
    						if (entity1 != null && entity1 instanceof EntityPlayer && see) {//target
    							flag = false;
    							flagtime = 0;
    							break;
    						}//target
    					}
    				}
    			}
    		}
    		if(flag) {
    			++flagtime;
    			if(!this.world.isRemote && flagtime > 200){
                	this.world.setBlockState(this.getPosition(), block.getDefaultState());
                	this.setDead();
                }
    		}
    	}
		
	}
    
    
    
    /**
    * Checks to make sure the light is not too bright where the mob is spawning
    */
   protected boolean isValidLightLevel()
   {
       BlockPos blockpos = new BlockPos(this.posX, this.getEntityBoundingBox().minY, this.posZ);

       if (this.world.getLightFor(EnumSkyBlock.SKY, blockpos) > this.rand.nextInt(32))
       {
           return false;
       }
       else
       {
           int i = this.world.getLightFromNeighbors(blockpos);

           if (this.world.isThundering())
           {
               int j = this.world.getSkylightSubtracted();
               this.world.setSkylightSubtracted(10);
               i = this.world.getLightFromNeighbors(blockpos);
               this.world.setSkylightSubtracted(j);
           }

           //return i <= this.rand.nextInt(8);
           return true;
       }
   }

   /**
    * Checks if the entity's current position is a valid location to spawn this entity.
    */
   /*public boolean getCanSpawnHere()
   {
       return this.world.getDifficulty() != EnumDifficulty.PEACEFUL && this.isValidLightLevel() && getCanSpawnUnderGround() && super.getCanSpawnHere();
   }*/
   /**
    * Checks if the entity's current position is a valid location to spawn this entity.
    */
   public boolean getCanSpawnHere()
   {
       int i = MathHelper.floor(this.posX);
       int j = MathHelper.floor(this.getEntityBoundingBox().minY);
       int k = MathHelper.floor(this.posZ);
       BlockPos blockpos = new BlockPos(i, j, k);
       return this.world.getBlockState(blockpos.down()).getBlock() == mod_GirlFront.b_block_collapse_hv && this.world.getLight(blockpos) > 0 && super.getCanSpawnHere();
   }
    
   
   public int getLevel() {
	   if(mod_GirlFront.cfg_spawn_original) {
		   return 10;
	   }else {
		   //return WorldDifficultyLevel.setDefaultLevel(mod_GirlFront.INSTANCE, this.world);
		   int kazu = WorldDifficultyLevel_new.getLevel();
		   if(kazu >= 10) {
			   kazu = 10;
		   }
		   return kazu;
	   }
   }
   
   
   public boolean getCanSpawnHere_max(int max)
   {
	   boolean flag = true;
	   int kazu = 0;
	   {
			List<Entity> llist = this.world.getEntitiesWithinAABBExcludingEntity(this, this
					.getEntityBoundingBox().expand(this.motionX, this.motionY, this.motionZ).grow(50));
			if (llist != null) {
				for (int lj = 0; lj < llist.size(); lj++) {
					Entity entity1 = (Entity) llist.get(lj);
					if (entity1.canBeCollidedWith() && entity1 != null && entity1 instanceof IMob) {
						++kazu;
					}
				}
			}
		}
	   if(kazu > max) {
		   flag = false;
	   }
       return flag;
   }
   
    /**
     * Determines if an entity can be despawned, used on idle far away entities
     */
    protected boolean canDespawn()
    {
    	if(this.getcanDespawn() == 1){
        return false;
    	}else{
    		return true;
    	}
        
    }
    
    protected SoundEvent getAmbientSound()
    {
        return GFSoundEvent.ibc_step;
    }
    protected SoundEvent getHurtSound(DamageSource damageSourceIn)
    {
        return SoundEvents.ENTITY_IRONGOLEM_HURT;
    }

    protected SoundEvent getDeathSound()
    {
        return SoundEvents.ENTITY_IRONGOLEM_DEATH;
    }

    protected void playStepSound(BlockPos pos, Block blockIn)
    {
    //    this.playSound(GFSoundEvent.ibc_step, 1.0F, 1.0F);
    }
    
    public void fall(float distance, float damageMultiplier)
    {
        float[] ret = net.minecraftforge.common.ForgeHooks.onLivingFall(this, distance, damageMultiplier);
        if (ret == null) return;
        distance = ret[0]; damageMultiplier = ret[1];
        super.fall(distance, damageMultiplier);
        PotionEffect potioneffect = this.getActivePotionEffect(MobEffects.JUMP_BOOST);
        float f = potioneffect == null ? 0.0F : (float)(potioneffect.getAmplifier() + 1);
        int i = MathHelper.ceil((distance - 3.0F - f) * damageMultiplier);

        if (i > 0)
        {
            this.playSound(this.getFallSound(i), 1.0F, 1.0F);
            this.attackEntityFrom(DamageSource.FALL, (float)i);
            int j = MathHelper.floor(this.posX);
            int k = MathHelper.floor(this.posY - 0.20000000298023224D);
            int l = MathHelper.floor(this.posZ);
            IBlockState iblockstate = this.world.getBlockState(new BlockPos(j, k, l));

            if (iblockstate.getMaterial() != Material.AIR)
            {
                SoundType soundtype = iblockstate.getBlock().getSoundType(iblockstate, world, new BlockPos(j, k, l), this);
                this.playSound(soundtype.getFallSound(), soundtype.getVolume() * 0.5F, soundtype.getPitch() * 0.75F);
            }
        }
    }
    
    public void BossDrop() {
    	this.world.setBlockState(new BlockPos(this.getMoveX(), this.getMoveY(), this.getMoveZ()), Blocks.CHEST.getDefaultState(), 2);
		TileEntityChest Chest3;
		Chest3 = (TileEntityChest) this.world.getTileEntity(new BlockPos(this.getMoveX(), this.getMoveY(), this.getMoveZ()));
		Chest3.setInventorySlotContents(4, new ItemStack(Blocks.DIAMOND_BLOCK, 2, 0));
		Chest3.setInventorySlotContents(12, new ItemStack(Blocks.EMERALD_BLOCK, 4, 0));
		Chest3.setInventorySlotContents(14, new ItemStack(Blocks.EMERALD_BLOCK, 4, 0));
		Chest3.setInventorySlotContents(22, new ItemStack(Blocks.EMERALD_BLOCK, 4, 0));
		
		{
			int ra = this.world.rand.nextInt(6);
			Item item = mod_GirlFront.core_416;
			if(ra == 0){
				item = this.getHG();
			}
			if(ra == 1){
				item = this.getSMG();
			}
			if(ra == 2){
				item = this.getAR();
			}
			if(ra == 3){
				item = this.getRF();
			}
			if(ra == 4){
				item = this.getMG();
			}
			if(ra == 5){
				item = this.getSG();
			}
			
    		/*if(ra == 0){
    			item = mod_GirlFront.core_416;
        	}
    		if(ra == 1){
    			item = mod_GirlFront.core_gr_g11;
        	}
    		if(ra == 2){
    			item = mod_GirlFront.core_t97;
        	}
    		if(ra == 3){
    			item = mod_GirlFront.core_vector;
        	}
    		if(ra == 4){
    			item = mod_GirlFront.core_wa2000;
        	}
    		if(ra == 5){
    			item = mod_GirlFront.core_gr_mg4;
        	}
    		if(ra == 6){
    			item = mod_GirlFront.core_fn57;
        	}
    		if(ra == 7){
    			item = mod_GirlFront.core_suomi;
        	}
    		if(ra == 8){
    			item = mod_GirlFront.core_ro635;
        	}
    		if(ra == 9){
    			item = mod_GirlFront.core_sr3mp;
        	}
    		if(ra == 10){
    			item = mod_GirlFront.core_mg36;
        	}
    		if(ra == 11){
    			item = mod_GirlFront.core_xm25;
        	}
    		if(ra == 12){
    			item = mod_GirlFront.core_type64;
        	}*/
    		
			
			Chest3.setInventorySlotContents(13, new ItemStack(item, 1, 0));
		}
    }
    
    
    public Item getHG() {
    	Item item = mod_GirlFront.core_fn57;
    	for(int i = 0; i < mod_GirlFront.hg_item.length; ++i) {
    		if(mod_GirlFront.hg_item.length > 0) {
    			int ne = this.world.rand.nextInt(mod_GirlFront.hg_item.length);
    			if(mod_GirlFront.hg_rare[ne] > 9) {
            		if (mod_GirlFront.hg_item[ne] != null) {
            			item = mod_GirlFront.hg_item[ne];
            			break;
            		} 
    			}
    		}
    	}
    	return item;
    }
    public Item getSMG() {
    	Item item = mod_GirlFront.core_vector;
    	for(int i = 0; i < mod_GirlFront.smg_item.length; ++i) {
    		if(mod_GirlFront.smg_item.length > 0) {
    			int ne = this.world.rand.nextInt(mod_GirlFront.smg_item.length);
    			if(mod_GirlFront.smg_rare[ne] > 9) {
            		if (mod_GirlFront.smg_item[ne] != null) {
            			item = mod_GirlFront.smg_item[ne];
            			break;
            		} 
    			}
    		}
    	}
    	return item;
    }
    public Item getAR() {
    	Item item = mod_GirlFront.core_416;
    	for(int i = 0; i < mod_GirlFront.ar_item.length; ++i) {
    		if(mod_GirlFront.ar_item.length > 0) {
    			int ne = this.world.rand.nextInt(mod_GirlFront.ar_item.length);
    			if(mod_GirlFront.ar_rare[ne] > 9) {
            		if (mod_GirlFront.ar_item[ne] != null) {
            			item = mod_GirlFront.ar_item[ne];
            			break;
            		} 
    			}
    		}
    	}
    	return item;
    }
    public Item getRF() {
    	Item item = mod_GirlFront.core_wa2000;
    	for(int i = 0; i < mod_GirlFront.rf_item.length; ++i) {
    		if(mod_GirlFront.rf_item.length > 0) {
    			int ne = this.world.rand.nextInt(mod_GirlFront.rf_item.length);
    			if(mod_GirlFront.rf_rare[ne] > 9) {
            		if (mod_GirlFront.rf_item[ne] != null) {
            			item = mod_GirlFront.rf_item[ne];
            			break;
            		} 
    			}
    		}
    	}
    	return item;
    }
    public Item getMG() {
    	Item item = mod_GirlFront.core_gr_mg4;
    	for(int i = 0; i < mod_GirlFront.lmg_item.length; ++i) {
    		if(mod_GirlFront.lmg_item.length > 0) {
    			int ne = this.world.rand.nextInt(mod_GirlFront.lmg_item.length);
    			if(mod_GirlFront.lmg_rare[ne] > 9) {
            		if (mod_GirlFront.lmg_item[ne] != null) {
            			item = mod_GirlFront.lmg_item[ne];
            			break;
            		} 
    			}
    		}
    	}
    	return item;
    }
    public Item getSG() {
    	Item item = mod_GirlFront.core_aa12;
    	for(int i = 0; i < mod_GirlFront.sg_item.length; ++i) {
    		if(mod_GirlFront.sg_item.length > 0) {
    			int ne = this.world.rand.nextInt(mod_GirlFront.sg_item.length);
    			if(mod_GirlFront.sg_rare[ne] > 9) {
            		if (mod_GirlFront.sg_item[ne] != null) {
            			item = mod_GirlFront.sg_item[ne];
            			break;
            		} 
    			}
    		}
    	}
    	return item;
    }
    public Item getGL() {
    	Item item = mod_GirlFront.core_mg34;
    	for(int i = 0; i < mod_GirlFront.gl_item.length; ++i) {
    		if(mod_GirlFront.gl_item.length > 0) {
    			int ne = this.world.rand.nextInt(mod_GirlFront.gl_item.length);
    			if(mod_GirlFront.gl_rare[ne] > 9) {
            		if (mod_GirlFront.gl_item[ne] != null) {
            			item = mod_GirlFront.gl_item[ne];
            			break;
            		} 
    			}
    		}
    	}
    	return item;
    }
}