package hmggirlfront.entity.mob;

import java.util.List;

import javax.annotation.Nullable;

import gvclib.entity.living.AI_EntityWeapon;
import gvclib.event.GVCSoundEvent;
import hmggirlfront.mod_GirlFront;
import hmggirlfront.entity.mob.ai.IBCAI_Entity;
import hmggirlfront.entity.mob.ai.IBCAI_EntityMoveS;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.IEntityLivingData;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAIFleeSun;
import net.minecraft.entity.ai.EntityAIRestrictSun;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.World;

public class EntityELID_ZOMBIE extends EntityELIDBase {

	public EntityELID_ZOMBIE(World worldIn) {
		super(worldIn);
		this.setSize(0.5F, 1.8F);
		//this.experienceValue = 50;
		this.find_enemy_maxcount = 60;
	}

	protected void initEntityAI() {
		this.tasks.addTask(1, new EntityAISwimming(this));
		this.tasks.addTask(2, new EntityAIRestrictSun(this));
		this.tasks.addTask(3, new EntityAIFleeSun(this, 1.0D));
		
	}

	protected void applyEntityAttributes() {
		super.applyEntityAttributes();
		this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(100D);
		this.getEntityAttribute(SharedMonsterAttributes.KNOCKBACK_RESISTANCE).setBaseValue(20D);
		this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.25D);
		this.getEntityAttribute(SharedMonsterAttributes.FOLLOW_RANGE).setBaseValue(100.0D);
	}

	public boolean getCanSpawnHere() {
		return this.getLevel() >= 0 && this.getCanSpawnHere_max(mod_GirlFront.cfg_spawner_limit_hard) && super.getCanSpawnHere();
	}
	
	protected void setEquipmentBasedOnDifficulty(DifficultyInstance difficulty) {
		super.setEquipmentBasedOnDifficulty(difficulty);
	}
	
	@Nullable
    public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, @Nullable IEntityLivingData livingdata)
    {
		livingdata = super.onInitialSpawn(difficulty, livingdata);
        int i = this.world.getDifficulty().getDifficultyId() + (this.getLevel() * 1);
        for(int ii = 0; ii < i; ++ii){
        	int ix = this.world.rand.nextInt(8);
			int iz = this.world.rand.nextInt(8);
        	EntityELID_ZOMBIE entityskeleton = new EntityELID_ZOMBIE(this.world);
        	entityskeleton.setLocationAndAngles(this.posX - 4 +ix, this.posY+1, this.posZ - 4 +iz, 0, 0.0F);
	        this.world.spawnEntity(entityskeleton);
        }
        return livingdata;
    }
	
	/**
     * Drop the equipment for this entity.
     */
    protected void dropEquipment(boolean wasRecentlyHit, int lootingModifier)
    {
    	DamageSource source = this.getLastDamageSource();
    	int dame = 0;
    	if(source == null || source.getTrueSource() == null) {
    		dame = 1;
    	}else {
    		dame = 0;
    	}
    	if(dame == 1)return;
    	
    	super.dropEquipment(wasRecentlyHit, lootingModifier);
    	{
    		int ra = this.world.rand.nextInt(2);
    		if(ra == 0){
        		this.entityDropItem(new ItemStack(mod_GirlFront.idoll_collapse_sample), 0.0F);
        	}
    	}
    	
    }
    
    protected void onDeathUpdate() {
    	super.onDeathUpdate();
	}
    
	/**
	 * Called frequently so the entity can update its state every tick as
	 * required. For example, zombies and skeletons use this to react to
	 * sunlight and start to burn.
	 */
    int self_ex = 0;
	public void onLivingUpdate() {
		super.onLivingUpdate();
		this.biped = true;
		float sp = 0.03F;
		Vec3d looked = this.getLookVec();
		
		//this.setAIType(0);
		this.updateArmSwingProgress();
		
		boolean exflag = false;
		
		//IBCAI_Entity.getTargetEntity(this, 25, 50, 10, 5, 10);
		AI_EntityWeapon.getTargetEntity(this, 30, 20, 20);
		//this.getbeacon();
		
		
		int max = 0;
		int range = 30;
		int attack_range = 2;
		this.reload_time1 = 60;
		if(this.discover_count > 20 - (this.world.getDifficulty().getDifficultyId() * 5)){
			int ran = 3;
			boolean task = AI_EntityWeapon.KnifeAttack(this, 0, ran, ran, 1, 1);
			if(task){//1
				if(this.cooltime4 > 30){
					this.swingArm(EnumHand.MAIN_HAND);
					Vec3d lock = Vec3d.fromPitchYaw(this.rotationPitch, this.rotationYaw);
					float d = 3;
					List<Entity> llist = this.world.getEntitiesWithinAABBExcludingEntity(this,
							this.getEntityBoundingBox()
									.expand(lock.x * d, lock.y * d, lock.z * d)
									.grow(0.02D));
					if (llist != null) {
						for (int lj = 0; lj < llist.size(); lj++) {
							Entity entity1 = (Entity) llist.get(lj);
							if (entity1.canBeCollidedWith() && entity1 instanceof EntityLivingBase && entity1 != null) {
								if(entity1 != this && entity1 != this.getControllingPassenger()){
									entity1.attackEntityFrom(DamageSource.causeMobDamage(this), 8);
								}
							}
						}
					}
					this.cooltime4 = 0;
				}
			}//1
		}
		IBCAI_EntityMoveS.newmove(this, 0, sp, 0, max, range, attack_range);
		
	}
}