package hmggirlfront.entity.mob;

import java.util.List;

import gvclib.entity.EntityBBase;
import gvclib.entity.living.AI_Damage;
import gvclib.entity.living.AI_EntityWeapon;
import gvclib.entity.living.AI_EntityWeapon_new;
import gvclib.entity.living.EntityGVCLivingBase;
import gvclib.event.GVCSoundEvent;
import gvclib.world.GVCExplosionBase;
import hmggirlfront.GFSoundEvent;
import hmggirlfront.mod_GirlFront;
import hmggirlfront.entity.bullet.EntityBullet_Blade;
import hmggirlfront.event.level.WorldDifficultyLevel_new;
import hmggirlfront.world.WorldDifficultyLevel;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
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.ai.RandomPositionGenerator;
import net.minecraft.entity.monster.IMob;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.BossInfo;
import net.minecraft.world.BossInfoServer;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.EnumDifficulty;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class EntityIBCBoss03 extends EntityIBCBase implements IIBCBoss{

	private final BossInfoServer bossInfo =
			(BossInfoServer)(new BossInfoServer(this.getDisplayName(), BossInfo.Color.PURPLE, BossInfo.Overlay.PROGRESS)).setDarkenSky(true);
	
	public EntityIBCBoss03(World worldIn) {
		super(worldIn);
		this.setSize(1F, 2.5F);
		this.boss = true;
		aitype_auto = false;
		aitypemaxset = 4;
		aitypemax = 40;
		aitypemax2 = 70;
		aitypemaxset2 = 3;
		this.experienceValue = 300;
	}

	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);
	}

	protected void setEquipmentBasedOnDifficulty(DifficultyInstance difficulty) {
		super.setEquipmentBasedOnDifficulty(difficulty);
		
	}
	
	public SoundEvent  DamegeSound = SoundEvents.BLOCK_ANVIL_LAND;
	public boolean attackEntityFrom(DamageSource source, float par2)
    {
    	Entity entity;
    	entity = source.getImmediateSource();
    	Entity entity2 = source.getTrueSource();
    	
		if(entity != null && entity2 != null){
			if(entity2 instanceof IMob){
	    		return false;
	        }else
	        {
				par2 = par2 * AI_Damage.newAntiBullet(this, entity, par2, 0.25F, 0.5F, 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 dropEquipment(boolean wasRecentlyHit, int lootingModifier)
	{
    }
	
    
	public void onUpdate()
    {
        super.onUpdate();
        if (!this.world.isRemote && this.world.getDifficulty() == EnumDifficulty.PEACEFUL && this.getcanDespawn() == 0)
        {
            this.setDead();
        }
    }
    
	protected void onDeathUpdate() {
		//super.onDeathUpdate();
		++deathTime;
		if(!this.world.isRemote && this.deathTime == 1) {
			GVCExplosionBase.ExplosionKai(this, this, this.posX + 0, this.posY + 0, this.posZ + 0, 2, false,  false);
		}
		if(this.deathTime == 200) {
			//WorldDifficultyLevel.setLevel(mod_GirlFront.INSTANCE, this.world, 4, this, "hmggirlfront.cor.level3.name");
			WorldDifficultyLevel_new.setLevel(mod_GirlFront.INSTANCE, this.world, 6, "hmggirlfront.cor.level6.name");
		}
		if(!this.world.isRemote && this.deathTime == 200) {
			GVCExplosionBase.ExplosionKai(this, this, this.posX + 0, this.posY + 0, this.posZ + 0, 2, false,  false);
			this.BossDrop();
			this.setDead();
		}
	}
	
	public void getbeacon() {
    	int i = 64;
    	if(this.cooltime6 > 100 && this.getMoveT() == 1) {
    		double x = Math.abs(this.posX - this.getMoveX());
    		double y = Math.abs(this.posY - this.getMoveY());
    		double z = Math.abs(this.posZ - this.getMoveZ());
    		if(x > 40 && z > 40) {
    			this.setPositionAndUpdate(this.getMoveX(), this.getMoveY(), this.getMoveZ());
    		}
    		if(y > 15) {
    			this.setPositionAndUpdate(this.getMoveX(), this.getMoveY(), this.getMoveZ());
    		}
    		if(this.targetentity == null) {
    			//this.setPositionAndUpdate(this.getMoveX(), this.getMoveY(), this.getMoveZ());
    		}
    		
    		this.cooltime6 = 0;
    	}
    	
    }
	
	@SideOnly(Side.CLIENT)
    public int getBrightnessForRender()
    {
        return 15728880;
    }

    /**
     * Gets how bright this entity is.
     */
    public float getBrightness()
    {
        return 1.0F;
    }
	
	/**
	 * 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;
		this.reload_time1 = 60;
		this.magazine = 10;
		if(this.getRemain_L() <= 0){
			
			this.aitypetime2  = 0;
			this.setAIType2(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.1F;
		Vec3d looked = this.getLookVec();
		
		boolean exflag = false;
		if(this.getMoveT() == 0) {
			this.setMoveT(1);
			this.setMoveX((int)this.posX);
			this.setMoveY((int)this.posY);
			this.setMoveZ((int)this.posZ);
		}
		
		if(this.getHealth() > 0.0F) {
			AI_EntityWeapon.getTargetEntity(this, 50, 20, 20);
			this.newmove(this, 1, sp, 0, 2, 80);
			this.bossInfo.setPercent(this.getHealth() / this.getMaxHealth());
			this.getbeacon();
			//this.motionY *= 0.2F;
		}
		
		
		this.updateArmSwingProgress();
		
		if(AI_EntityWeapon.getRange(this, 40, 20, 20) && this.discover_count > 40 - (this.world.getDifficulty().getDifficultyId() * 5)){
			if(this.getAIType2() != 1  && this.getAIType2() != 2) {
				
				this.cooltime2 = 0;
				
				if(this.cooltime > 30 && this.getRemain_L() > 0)
				{
	    			this.counter1 = true;
	    			this.cooltime = 0;
				}
	    		if(this.counter1 && this.gun_count1 > 3 && this.getRemain_L() > 0){
	    			{
						String model = "hmggirlfront:textures/entity/bullet_shot.mqo";
			    		String tex = "hmggirlfront:textures/entity/bullet_shot.png";
			    		String modelf = "hmggirlfront:textures/entity/mflash_ibc.mqo";
			    		String texf = "hmggirlfront:textures/entity/mflash_ibc.png";
			    		SoundEvent sounds = GFSoundEvent.fire_ibc_rifle;
			    		if(sounds == null) {
			    			sounds = SoundEvent.REGISTRY.getObject(new ResourceLocation(mod_GirlFront.MOD_ID, "fire_ibc_rifle"));
			    		}
			        			AI_EntityWeapon_new we = new AI_EntityWeapon_new(this, this);
								we.id = 0;
								we.model = model;
								we.tex = tex;
								we.modelf = modelf;
								we.texf = texf;
								we.ftime = 0;
								we.sound = sounds;
								if(this.getRemain_L() %2 == 0) {
									we.f = -1.57F;
								}else {
									we.f = 1.57F;
								}
								we.w = 0.3F * 1.5;
								we.h = 1.5 * 1.5;
								we.z = 0.75;
								we.bx = 0.2F;
								we.by = 1.5;
								we.bz = 0.0;
								we.follow_rote = true;//向いている方向に
								we.xoffset = 0;
								we.yoffset = 0;
								we.px = this.posX;
								we.py = this.posY;
								we.pz = this.posZ;
								we.lock = looked;
								we.rote = this.rotationYawHead;
								we.maxy = 20;
								we.miny = 10;
								
								we.power = 3 + this.world.getDifficulty().getDifficultyId();
								we.damegetime = false;
								we.speed = 3F;
								we.bure = 5F;
								we.ex = 0;
								we.extrue = false;
								we.kazu = 1;
								we.gra = 0.029;
								we.maxtime = 80;
								we.dameid = 0;
								we.Attacktask(this, this, 40);
					}
					this.gun_count1 = 0;
					if(this.firetrue){
						this.setRemain_L(this.getRemain_L() - 1);
					}
					++this.countlimit1;
				}
				if(this.countlimit1 >= 2){
					this.gun_count1 = 0;
					this.counter1 = false;
					this.countlimit1 = 0;
				}
				if(aitypetime > 40) {
					this.aitypetime  = aitypemax;
				}
			}
			else if(this.getAIType2() == 2) {

				this.cooltime2 = 0;
				int kazu = 0;
				double k = this.posX;
				double l = this.posY;
				double i = this.posZ;
				int han = 15;
				AxisAlignedBB axisalignedbb = (new AxisAlignedBB((double) (k - han), (double) (l - han),
						(double) (i - han), (double) (k + han), (double) (l + han), (double) (i + han)))
								.grow(1);
				List<Entity> llist = this.world.getEntitiesWithinAABBExcludingEntity(this, axisalignedbb);
				if (llist != null) {
					for (int lj = 0; lj < llist.size(); lj++) {
						Entity entity1 = (Entity) llist.get(lj);
						if (entity1.canBeCollidedWith()) {
							if (entity1 instanceof EntityIBC07 && entity1 != null) {
								++kazu;
							}
						}
					}
				}
				if(aitypetime2 == 10 && kazu < 8) {
					 for(int ii = 0; ii < 4; ++ii){
							
						 int ix = this.world.rand.nextInt(6);
							int iz = this.world.rand.nextInt(6);
						 
							double xx11 = 0;
							double zz11 = 0;
							xx11 -= MathHelper.sin(this.rotationYawHead * 0.01745329252F - 1.57F) * 5;
							zz11 += MathHelper.cos(this.rotationYawHead * 0.01745329252F - 1.57F) * 5;
							
							if(ii > 1) {
								xx11 = -xx11;
								zz11 = -zz11;
							}
							
				        	EntityIBC05 entityskeleton = new EntityIBC05(this.world);
				        	entityskeleton.setLocationAndAngles(this.posX - xx11 - 3 + ix, this.posY+1, this.posZ - zz11 - 3 + iz, 0, 0.0F);
					        entityskeleton.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(mod_GirlFront.dollgun_ibc05ar));
					        entityskeleton.boss_spawn = true;
					        this.world.spawnEntity(entityskeleton);
				        }
				}
				if(aitypetime > 40) {
					this.aitypetime  = aitypemax;
				}
			}
			else if(this.getAIType2() == 1) {
				if(this.cooltime2 > 40)
				{
					this.counter2 = true;
				}
				if(this.counter2 && this.gun_count1 > 2){
	    			{
						String model = "hmggirlfront:textures/entity/bullet_shot.mqo";
			    		String tex = "hmggirlfront:textures/entity/bullet_shot.png";
			    		String modelf = "hmggirlfront:textures/entity/mflash_ibc.mqo";
			    		String texf = "hmggirlfront:textures/entity/mflash_ibc.png";
			    		SoundEvent sounds = GFSoundEvent.fire_ibc_rifle;
			    		if(sounds == null) {
			    			sounds = SoundEvent.REGISTRY.getObject(new ResourceLocation(mod_GirlFront.MOD_ID, "fire_ibc_rifle"));
			    		}
			        			AI_EntityWeapon_new we = new AI_EntityWeapon_new(this, this);
								we.id = 0;
								we.model = model;
								we.tex = tex;
								we.modelf = modelf;
								we.texf = texf;
								we.ftime = 0;
								we.sound = sounds;
								if(countlimit2 %2 == 0) {
									we.f = -1.57F;
								}else {
									we.f = 1.57F;
								}
								we.w = 0.3F * 1.5;
								we.h = 1.5 * 1.5;
								we.z = 0.75;
								we.bx = 0.2F;
								we.by = 1.5;
								we.bz = 0.0;
								we.follow_rote = true;//向いている方向に
								we.xoffset = 0;
								we.yoffset = 0;
								we.px = this.posX;
								we.py = this.posY;
								we.pz = this.posZ;
								we.lock = looked;
								we.rote = this.rotationYawHead;
								we.maxy = 20;
								we.miny = 10;
								
								we.power = 3 + this.world.getDifficulty().getDifficultyId();
								we.damegetime = false;
								we.speed = 3F;
								we.bure = 5F;
								we.ex = 0;
								we.extrue = false;
								we.kazu = 1;
								we.gra = 0.029;
								we.maxtime = 80;
								we.dameid = 0;
								we.Attacktask(this, this, 40);
					}
					++this.countlimit2;
					this.gun_count1 = 0;
				}
				if(this.countlimit2 >= 6){
					this.counter2 = false;
					this.countlimit2 = 0;
					this.cooltime2 = 0;
					this.aitypetime2  = aitypemax2;
				}
			}
		}
		
		
    	
	}
	
	@Override
	public void AIType2_Change() {
		super.AIType2_Change();
		int ra = this.world.rand.nextInt(aitypemaxset);
		if(this.getAIType() == ra) {
			for(int xxx = 0; xxx < 10; ++xxx) {
				ra = this.world.rand.nextInt(aitypemaxset);
				if(this.getAIType() != ra) {
					break;
				}
			}
		}
		this.setAIType(ra);
	}
	
	public void newmove(EntityGVCLivingBase entity, int id, float sp, float turnspeed, double max, double range1) {
		entity.sneak = false;
		boolean ta = false;
		double range = range1;
		int range2 = 30;
		
		{
			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 flag = entity.getEntitySenses().canSee(entity1);
						if (entity.CanAttack(entity1) && entity1 != null) {//target
							
							if (entity.targetentity == entity1
									&& ((EntityLivingBase) entity1).getHealth() > 0.0F) {
								double d5 = entity1.posX - entity.posX;
								double d7 = entity1.posZ - entity.posZ;
								double d6 = entity1.posY - entity.posY;
								double d1 = entity.posY - (entity1.posY);
								double d3 = (double) MathHelper.sqrt(d5 * d5 + d7 * d7);
								float f11 = (float) (-(Math.atan2(d1, d3) * 180.0D / Math.PI));
								double ddx = Math.abs(d5);
								double ddz = Math.abs(d7);
								{
									entity.rotationYawHead = entity.rotation = entity.rote = entity.renderYawOffset 
											= -((float) Math.atan2(d5, d7)) * 180.0F / (float) Math.PI;
									entity.rotationp = entity.rotationPitch = -f11 + 10;
									if ((ddx > 40 || ddz > 40)) 
									{
										this.setAIType(0);
										this.aitypetime = 0;
										this.setAIType2(0);
										this.aitypetime2 = 0;
									}
									MoveS(entity, sp, 1, entity1.posX, entity1.posY, entity1.posZ, id, (EntityLivingBase)entity1);
									
								}
								if (flag) {
									entity.targetentity = (EntityLivingBase) entity1;
									entity.noflag = 0;
									++entity.discover_count;
									entity.targetentity_lastposX = entity1.posX;
									entity.targetentity_lastposY = entity1.posY;
									entity.targetentity_lastposZ = entity1.posZ;
									if ((ddx < range2 && ddz < range2)) {
										entity.setFTMode(1);
									}else {
										entity.setFTMode(0);
									}
								}else {
									++entity.noflag;
									entity.discover_count = 0;
									entity.setFTMode(0);
									if(entity.noflag > 20) 
									{
										entity.noflag = 0;
									}
								}
								break;
							} 
						}//target
						
					}
				}
			}
		}
		if(!entity.getattacktask()){
			stay(entity, id);
		}
	}
	
	public void MoveS(EntityGVCLivingBase entity, double speed, double han, double ex, double ey, double ez, int id, EntityLivingBase en){
		{// 1
			if(!entity.world.isRemote) {
				double d5 = ex - entity.posX;
				double d7 = ez - entity.posZ;
				float off = 0;
				entity.rotationYaw = entity.renderYawOffset = -((float) Math.atan2(d5, d7)) * 180.0F / (float) Math.PI;
				float yaw = entity.rotationYaw * (2 * (float) Math.PI / 360);
				if(this.getAIType2() != 1){
					if(entity.getAIType() == 1){
						this.motionY = 0.3;
						this.motionX = 2;
						this.cooltime = 0;
						this.counter1 = false;
						this.setAIType(0);
					}
					if(entity.getAIType() == 2){
						this.motionY = 0.3;
						this.motionZ = 2;
						this.cooltime = 0;
						this.counter1 = false;
						this.setAIType(0);
					}
					if(entity.getAIType() == 3){
						this.motionY = 0.3;
						this.motionX = -2;
						this.cooltime = 0;
						this.counter1 = false;
						this.setAIType(0);
					}
					if(entity.getAIType() == 3){
						this.motionY = 0.3;
						this.motionZ = -2;
						this.cooltime = 0;
						this.counter1 = false;
						this.setAIType(0);
					}
				}
			}
			
			
			
			//if(!entity.world.isRemote)
			/*{
				double d5 = ex - entity.posX;
				double d7 = ez - entity.posZ;
				
				double root = Math.sqrt(d5 * d5 + d7 * d7);
				
				float off = 0;
				entity.rotationYaw = entity.renderYawOffset = -((float) Math.atan2(d5, d7)) * 180.0F / (float) Math.PI;
				float yaw = entity.rotationYaw * (2 * (float) Math.PI / 360);
				double mox = 0;
				double moy = entity.motionY;
				double moz = 0;
				
				//if(entity.getHealth() > entity.getMaxHealth()/2) 
				if(this.getAIType2() != 1){
					if(entity.getAIType() == 1) {
						mox -= MathHelper.sin(yaw + 1.57F) * speed * 1;
						moz += MathHelper.cos(yaw + 1.57F) * speed * 1;
					}
					else if(entity.getAIType() == 2) {
						mox -= MathHelper.sin(yaw - 1.57F) * speed * 1;
						moz += MathHelper.cos(yaw - 1.57F) * speed * 1;
					}
					else if(entity.getAIType() == 3) {
						mox -= MathHelper.sin(yaw) * speed * -1;
						moz += MathHelper.cos(yaw) * speed * -1;
					}
					else if(entity.getAIType() == 0) {
						mox -= MathHelper.sin(yaw) * speed * 1;
						moz += MathHelper.cos(yaw) * speed * 1;
					}
				}
				entity.stepHeight = entity.height * 0.8F;
				{
					{
						entity.motionX = mox;
						entity.motionZ = moz;
						entity.motionY = moy;
						entity.move(MoverType.SELF, entity.motionX, entity.motionY, entity.motionZ);
					}
				}

			}*/
		} // 1
	}
	
	public static void stay(EntityGVCLivingBase entity, int id) {
		double xPosition = 0;
	    double yPosition = 0;
	    double zPosition = 0;
		if (entity.getIdleTime() >= 100)
        {
        }
        else if (entity.getRNG().nextInt(120) != 0)
        {
        }
        else
        {
            Vec3d vec3 = RandomPositionGenerator.findRandomTarget(entity, 10, 7);

            if (vec3 == null)
            {
            }
            else
            {
                xPosition = vec3.x;
                yPosition = vec3.y;
                zPosition = vec3.z;
            }
            entity.rotation = entity.rotationYawHead = entity.getRNG().nextInt(120) - 60;
        }
		if(id != 0) {
			entity.getNavigator().tryMoveToXYZ(xPosition, yPosition, zPosition, 1D);
		}
	}
	
	 /**
     * Add the given player to the list of players tracking this entity. For instance, a player may track a boss in
     * order to view its associated boss bar.
     */
    public void addTrackingPlayer(EntityPlayerMP player)
    {
        super.addTrackingPlayer(player);
        this.bossInfo.addPlayer(player);
    }

    /**
     * Removes the given player from the list of players tracking this entity. See {@link Entity#addTrackingPlayer} for
     * more information on tracking.
     */
    public void removeTrackingPlayer(EntityPlayerMP player)
    {
        super.removeTrackingPlayer(player);
        this.bossInfo.removePlayer(player);
    }
}