package hmggirlfront.entity.mob;

import java.util.List;

import javax.annotation.Nullable;

import gvclib.entity.living.AI_Damage;
import gvclib.entity.living.AI_EntityWeapon;
import gvclib.event.GVCSoundEvent;
import gvclib.world.GVCExplosionBase;
import hmggirlfront.mod_GirlFront;
import net.minecraft.block.Block;
import net.minecraft.block.BlockLilyPad;
import net.minecraft.block.BlockLog;
import net.minecraft.block.BlockNewLeaf;
import net.minecraft.block.BlockOldLeaf;
import net.minecraft.block.BlockOldLog;
import net.minecraft.block.BlockPane;
import net.minecraft.block.BlockPlanks;
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.IEntityLivingData;
import net.minecraft.entity.MoverType;
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.entity.item.EntityXPOrb;
import net.minecraft.entity.monster.IMob;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class EntityIBC10u extends EntityIBCBase {

	public EntityIBC10u(World worldIn) {
		super(worldIn);
		this.setSize(3.0F, 4.5F);
		this.experienceValue = 100;
	}

	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(400D);
		this.getEntityAttribute(SharedMonsterAttributes.KNOCKBACK_RESISTANCE).setBaseValue(20D);
		this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.25D);
		this.getEntityAttribute(SharedMonsterAttributes.FOLLOW_RANGE).setBaseValue(100.0D);
	}
	
	protected void setEquipmentBasedOnDifficulty(DifficultyInstance difficulty) {
		super.setEquipmentBasedOnDifficulty(difficulty);
	}
	
	@Nullable
    public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, @Nullable IEntityLivingData livingdata)
    {
        livingdata = super.onInitialSpawn(difficulty, livingdata);
        return livingdata;
    }
	
	@SideOnly(Side.CLIENT)
    public int getBrightnessForRender()
    {
        return 15728880;
    }

    /**
     * Gets how bright this entity is.
     */
    public float getBrightness()
    {
        return 1.0F;
    }
	
	/**
     * Drop the equipment for this entity.
     */
    protected void dropEquipment(boolean wasRecentlyHit, int lootingModifier)
    {
    	super.dropEquipment(wasRecentlyHit, lootingModifier);
    	
    }
    public SoundEvent  DamegeSound = SoundEvents.BLOCK_ANVIL_LAND;
	public boolean attackEntityFrom(DamageSource source, float par2)
    {
    	Entity entity;
    	entity = source.getImmediateSource();
    	
		if(entity != null){
			if(this.getControllingPassenger() != null && this.getControllingPassenger() == source.getImmediateSource()){
	    		return false;
	        }else if(entity instanceof IMob){
	    		return false;
	        }else{
				par2 = par2 * AI_Damage.newAntiBullet(this, entity, par2, 0.4F, 0.8F, 1.0F, 1.0F);
				if(par2 <= 0.0) {
					this.playSound(DamegeSound, 1F, 1F);
				}
				return super.attackEntityFrom(source, par2);
			}
		}else{
			return super.attackEntityFrom(source, par2);
		}
    }
    protected void onDeathUpdate() {
    	//super.onDeathUpdate();
    	++this.deathTime;

        if (this.deathTime == 200)
        {
            if (!this.world.isRemote && (this.isPlayer() || this.recentlyHit > 0 && this.canDropLoot() && this.world.getGameRules().getBoolean("doMobLoot")))
            {
                int i = this.getExperiencePoints(this.attackingPlayer);
                i = net.minecraftforge.event.ForgeEventFactory.getExperienceDrop(this, this.attackingPlayer, i);
                while (i > 0)
                {
                    int j = EntityXPOrb.getXPSplit(i);
                    i -= j;
                    this.world.spawnEntity(new EntityXPOrb(this.world, this.posX, this.posY, this.posZ, j));
                }
            }

            this.setDead();

            for (int k = 0; k < 20; ++k)
            {
                double d2 = this.rand.nextGaussian() * 0.02D;
                double d0 = this.rand.nextGaussian() * 0.02D;
                double d1 = this.rand.nextGaussian() * 0.02D;
                this.world.spawnParticle(EnumParticleTypes.EXPLOSION_NORMAL, this.posX + (double)(this.rand.nextFloat() * this.width * 2.0F) - (double)this.width, this.posY + (double)(this.rand.nextFloat() * this.height), this.posZ + (double)(this.rand.nextFloat() * this.width * 2.0F) - (double)this.width, d2, d0, d1);
            }
        }
    	if(!this.world.isRemote && this.deathTime == 1) {
			GVCExplosionBase.ExplosionKai(this, this, this.posX + 0, this.posY + 0, this.posZ + 0, 3, false,  false);
		}
    	if(this.getArmID_A() == 1 && this.deathTime == 199) {
    		EntityIBCBoss01M entityskeleton = new EntityIBCBoss01M(this.world);
	        	entityskeleton.setLocationAndAngles(this.posX,this.posY,this.posZ, 0, 0.0F);
		        if(!this.world.isRemote)this.world.spawnEntity(entityskeleton);
		}
	}
    
	/**
	 * 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;
		if(this.getRemain_L() <= 0){
			++reload1;
			if(reload1 == reload_time1 - 10){
				this.playSound(GVCSoundEvent.reload_mag, 1.0F, 1.0F);
			}
			if(reload1 >= reload_time1){
				this.setRemain_L(this.magazine);
				reload1 = 0;
			}
		}
		float sp = 0.02F;
		Vec3d looked = this.getLookVec();
		
		//this.setAIType(0);
		this.updateArmSwingProgress();
		
		boolean exflag = false;
		
		AI_EntityWeapon.getTargetEntity(this, 50, 20, 10);
		//this.getbeacon();
		
		if(this.cooltime6 > 300 && this.getHealth() > 0){
			int mobkazu = 0;
			List llist = this.world.getEntitiesWithinAABBExcludingEntity(this, this.getEntityBoundingBox()
	        		.expand(this.motionX, this.motionY, this.motionZ).grow(50.0D));
			if (llist != null) {
				for (int lj = 0; lj < llist.size(); lj++) {
					Entity entity1 = (Entity) llist.get(lj);
					if (entity1.canBeCollidedWith() && entity1 != null) {
						if (entity1 instanceof EntityLivingBase && entity1 instanceof EntityIBCBase)
                        {
							++mobkazu;
                        }
					}
				}
			}
			if(mobkazu < 20) {
				double x = this.posX;
				double y = this.posY;
				double z = this.posZ;
				double offset_x = 5;
				double offset_z = 5;
				if(this.world.rand.nextInt(2) == 0)offset_x = offset_x * -1;
				if(this.world.rand.nextInt(2) == 0)offset_z = offset_z * -1;
				x = x + offset_x;
				z = z + offset_z;
				int i = this.world.rand.nextInt(3) + 2 +this.world.getDifficulty().getDifficultyId();
				int ran = this.world.rand.nextInt(4);
				for (int ii = 0; ii < i; ++ii) {
					if (ran == 1) {
						int ix = this.world.rand.nextInt(8);
						int iz = this.world.rand.nextInt(8);
						EntityIBC05 entityskeleton = new EntityIBC05(this.world);
						entityskeleton.setattacktask(true);
						entityskeleton.setMoveT(2);
						entityskeleton.setLocationAndAngles(x - 4 + ix, y + 1, z - 4 + iz, 0, 0.0F);
						entityskeleton.setItemStackToSlot(EntityEquipmentSlot.MAINHAND,new ItemStack(mod_GirlFront.dollgun_ibc05ar));
						if (!this.world.isRemote)
							this.world.spawnEntity(entityskeleton);
					}else if (ran == 2) {
						int ix = this.world.rand.nextInt(8);
						int iz = this.world.rand.nextInt(8);
						EntityIBC06 entityskeleton = new EntityIBC06(this.world);
						entityskeleton.setattacktask(true);
						entityskeleton.setMoveT(2);
						entityskeleton.setLocationAndAngles(x - 4 + ix, y + 1, z - 4 + iz, 0, 0.0F);
						entityskeleton.setItemStackToSlot(EntityEquipmentSlot.MAINHAND,new ItemStack(mod_GirlFront.dollgun_ibc06rf));
						if (!this.world.isRemote)
							this.world.spawnEntity(entityskeleton);
					}else {
						int ix = this.world.rand.nextInt(8);
						int iz = this.world.rand.nextInt(8);
						EntityIBC04 entityskeleton = new EntityIBC04(this.world);
						entityskeleton.setattacktask(true);
						entityskeleton.setMoveT(2);
						entityskeleton.setLocationAndAngles(x - 4 + ix, y + 1, z - 4 + iz, 0, 0.0F);
						entityskeleton.setItemStackToSlot(EntityEquipmentSlot.MAINHAND,new ItemStack(mod_GirlFront.dollgun_ibc04smg));
						entityskeleton.setItemStackToSlot(EntityEquipmentSlot.OFFHAND,new ItemStack(mod_GirlFront.dollgun_ibc04smg));
						if (!this.world.isRemote)
							this.world.spawnEntity(entityskeleton);
					}
				}
			}
			
			this.cooltime6 = 0;
		}
		
		
		int max = 30;
		int range = 40;
		int range2 = 50;
		this.magazine = 1;
		if(this.getHealth() > this.getMaxHealth()/2) {
			
		}else{
			sp = 0.01F;
		}
		if(this.getHealth() > 0){
			double mox = 0;
			double moy = this.motionY;
			double moz = 0;
			float yaw = this.rotationYaw * (2 * (float) Math.PI / 360);
			mox -= MathHelper.sin(yaw) * sp * 1;
			moz += MathHelper.cos(yaw) * sp * 1;
			this.stepHeight = this.height * 1.0F;
			{
				{
					this.motionX = mox;
					this.motionZ = moz;
					this.motionY = moy;
					this.move(MoverType.SELF, this.motionX, this.motionY,this.motionZ);
				}
			}
		}
		{
			double d5 = this.getMoveX() - this.posX;
			double d7 = this.getMoveZ() - this.posZ;
			double ddx = Math.abs(d5);
			double ddz = Math.abs(d7);
			double dxz = MathHelper.sqrt(d5 * d5 + d7 * d7);
			//if ((ddx > 120 || ddz > 120)) 
			if(dxz > 180){
				if(!this.world.isRemote) {
					this.setDead();
				}
			}
		}
		{
			if(!this.world.isRemote){
	    		AxisAlignedBB axisalignedbb = this.getEntityBoundingBox();
	    		double xx = width;
	        		for(double x = 0; x < width*2; ++x) {
	        			for(double y = 0; y < height*1.2; ++y) {
	        				for(double z = 0; z < width*2; ++z) {
	        					BlockPos pos = new BlockPos(this.posX -xx + x, this.posY + y, this.posZ - xx + z);
	        					if(this.world.getBlockState(pos).getBlock() instanceof BlockOldLog
	        						|| this.world.getBlockState(pos).getBlock() instanceof BlockOldLeaf	
	        						|| this.world.getBlockState(pos).getBlock() instanceof BlockPane	
	        						|| this.world.getBlockState(pos).getBlock() instanceof BlockLog
	        						|| this.world.getBlockState(pos).getBlock() instanceof BlockNewLeaf	
	        						|| this.world.getBlockState(pos).getBlock() instanceof BlockPlanks	
	        						|| this.world.getBlockState(pos).getBlock() instanceof BlockLilyPad	
	        						//|| this.world.getBlockState(pos).getBlock() instanceof BlockGrass	
	        							) 
	        					{
	        						IBlockState iblockstate = this.world.getBlockState(pos);
	        		                Block block = iblockstate.getBlock();
	        		                if (iblockstate.getMaterial() != Material.AIR)
	        		                {
	        		                    block.dropBlockAsItemWithChance(this.world, pos, this.world.getBlockState(pos), 1.0F, 0);
	        		                    world.setBlockToAir(pos);
	        		                }
	        					}
	        				}
	        			}
	        		}
	    	}
		}
		//AI_EntityMoveS.newmove_turret(this, 0, sp, 1.0F, max, range, range2);
		
	}
}