package hmggirlfront.entity.mob;

import java.util.List;

import javax.annotation.Nullable;

import gvclib.entity.living.AI_EntityWeapon;
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 EntityIBC14 extends EntityIBCBase {

	public EntityIBC14(World worldIn) {
		super(worldIn);
		this.setSize(0.5F, 1.8F);
		this.experienceValue = 10;
		this.find_enemy_maxcount = 40;
	}

	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(20D);
		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() >= 6 && this.getCanSpawnHere_max(mod_GirlFront.cfg_spawner_limit_hard) && super.getCanSpawnHere();
	}
	
	protected void setEquipmentBasedOnDifficulty(DifficultyInstance difficulty) {
		super.setEquipmentBasedOnDifficulty(difficulty);
		this.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(mod_GirlFront.dollgun_ibc14sw));
		this.setItemStackToSlot(EntityEquipmentSlot.OFFHAND, new ItemStack(mod_GirlFront.dollgun_ibc14sw));
	}
	
	@Nullable
    public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, @Nullable IEntityLivingData livingdata)
    {
        livingdata = super.onInitialSpawn(difficulty, livingdata);
        int i = this.world.rand.nextInt(3) +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);
        	EntityIBC14 entityskeleton = new EntityIBC14(this.world);
	        entityskeleton.setLocationAndAngles(this.posX - 4 +ix, this.posY+1, this.posZ - 4 +iz, 0, 0.0F);
	        entityskeleton.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(mod_GirlFront.dollgun_ibc14sw));
	        entityskeleton.setItemStackToSlot(EntityEquipmentSlot.OFFHAND, new ItemStack(mod_GirlFront.dollgun_ibc14sw));
	        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_bullet), 0.0F);
        	}
    	}
    	{
    		int ra = this.world.rand.nextInt(6);
    		if(ra == 0){
        		this.entityDropItem(new ItemStack(mod_GirlFront.idoll_food), 0.0F);
        	}
    		if(ra == 1){
        		this.entityDropItem(new ItemStack(mod_GirlFront.idoll_hp), 0.0F);
        	}
    		if(ra == 2){
        		this.entityDropItem(new ItemStack(mod_GirlFront.idoll_bullet), 0.0F);
        	}
    		if(ra == 3){
        		this.entityDropItem(new ItemStack(mod_GirlFront.idoll_parts), 0.0F);
        	}
    	}
    	if(this.getMoveT() != 2){
    		int ra = this.world.rand.nextInt(30);
    		if(ra == 0){
        		this.entityDropItem(new ItemStack(mod_GirlFront.idoll_doll_contract), 0.0F);
        	}
    		if(ra == 1){
        		this.entityDropItem(new ItemStack(mod_GirlFront.idoll_doll_rapidcontract), 0.0F);
        	}
    		if(ra == 2){
        		this.entityDropItem(new ItemStack(mod_GirlFront.idoll_doll_repair), 0.0F);
        	}
    		if(ra == 3){
        		this.entityDropItem(new ItemStack(mod_GirlFront.core_mp40), 0.0F);
        	}
    		if(ra == 4){
        		this.entityDropItem(new ItemStack(mod_GirlFront.core_idw), 0.0F);
        	}
    		if(ra == 5){
        		this.entityDropItem(new ItemStack(mod_GirlFront.core_m10), 0.0F);
        	}
    		if(ra == 6){
        		this.entityDropItem(new ItemStack(mod_GirlFront.core_m1911), 0.0F);
        	}
    		if(ra == 7){
        		this.entityDropItem(new ItemStack(mod_GirlFront.core_p38), 0.0F);
        	}
    		if(ra == 8){
        		this.entityDropItem(new ItemStack(mod_GirlFront.core_m9), 0.0F);
        	}
    		if(ra == 9){
        		this.entityDropItem(new ItemStack(mod_GirlFront.core_g17), 0.0F);
        	}
    		if(ra == 10){
        		this.entityDropItem(new ItemStack(mod_GirlFront.idoll_doll_report), 0.0F);
        	}
    		if(ra == 11){
        		this.entityDropItem(new ItemStack(mod_GirlFront.idoll_squad_cn), 0.0F);
        	}
    	}
    	{
    		int ra = this.world.rand.nextInt(30);
    		if(ra == 0){
    			this.entityDropItem(new ItemStack(mod_GirlFront.idoll_l_performance_circuit), 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.1F;
		Vec3d looked = this.getLookVec();
		
		//this.setAIType(0);
		this.updateArmSwingProgress();
		
		boolean exflag = false;
		
		IBCAI_Entity.getTargetEntity(this, 20, 50, 10, 10, 10);
		if(this.getcanDespawn() == 1)this.doblock(this, 60, mod_GirlFront.bdoll_ibc14);
		//this.getbeacon();
		
		
		int max = 2;
		int range = 50;
		int attack_range = 5;
		this.magazine = 12;
		this.reload_time1 = 60;
		if(this.discover_count > 10 - (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), 4);
								}
							}
						}
					}
					this.cooltime4 = 0;
				}
			}//1
		}
		IBCAI_EntityMoveS.newmove(this, 0, sp, 0, max, range, attack_range);
	}
}