package hmggirlfront.entity.mob;

import java.util.List;

import gvclib.entity.living.AI_Damage;
import gvclib.entity.living.AI_EntityWeapon;
import gvclib.entity.living.AI_EntityWeapon_new;
import gvclib.event.GVCSoundEvent;
import gvclib.world.GVCExplosionBase;
import hmggirlfront.mod_GirlFront;
import hmggirlfront.entity.mob.ai.IBCAI_EntityMoveS;
import hmggirlfront.event.level.WorldDifficultyLevel_new;
import hmggirlfront.world.WorldDifficultyLevel;
import net.minecraft.entity.Entity;
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.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.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 EntityIBCBoss05 extends EntityIBCBase implements IIBCBoss{

	private final BossInfoServer bossInfo =
			(BossInfoServer)(new BossInfoServer(this.getDisplayName(), BossInfo.Color.PURPLE, BossInfo.Overlay.PROGRESS)).setDarkenSky(true);
	
	public EntityIBCBoss05(World worldIn) {
		super(worldIn);
		this.setSize(1F, 2.5F);
		this.boss = true;
		aitypemax = 60;
		aitypemax2 = 82;
		aitypemaxset2 = 4;
		this.experienceValue = 300;
		
		aitype2_auto = false;
	}

	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(1000D);
		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, 6, this, "hmggirlfront.cor.level6.name");
			WorldDifficultyLevel_new.setLevel(mod_GirlFront.INSTANCE, this.world, 8, "hmggirlfront.cor.level8.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;
		
		float sp = 0.00F;
		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, 60, 20, 20);
			IBCAI_EntityMoveS.newmove(this, 0, sp, 0, 30, 60, 60);
			this.bossInfo.setPercent(this.getHealth() / this.getMaxHealth());
			this.getbeacon();
			this.motionY *= 0.2F;
		}
		if(this.getAIType2() != 1) {
			this.cooltime2 = 0;
		}
		
		{
			//if(this.aitype2_auto) 
			{
	    		if(!this.world.isRemote)++aitypetime2;
	        	if(aitypetime2 >= aitypemax2){
	        		int ra = this.world.rand.nextInt(aitypemaxset2);
	        		if(this.getAIType2() == ra) {
	        			for(int xxx = 0; xxx < 10; ++xxx) {
	        				ra = this.world.rand.nextInt(aitypemaxset2);
	        				if(this.getAIType2() != ra) {
	        					break;
	        				}
	        			}
	        		}
	        		this.setAIType2(ra);
	        		aitypetime2 = 0;
	        		this.cooltime = 0;
	        		this.counter1 = false;
	        		this.gun_count1 = 0;
	        		this.countlimit1 = 0;
	        	}
	    	}
		}
		
		
		
		if(AI_EntityWeapon.getRange(this, 40, 20, 20)){
			if(this.getAIType2() != 1   && this.getAIType2() != 2) {
				if(this.cooltime > 50)
				{
	    			this.counter1 = true;
	    			this.cooltime = 0;
				}
	    		if(this.counter1 && this.gun_count1 > 10){
	    			{
						String model = "hmggirlfront:textures/entity/bulletnormal.mqo";
			    		String tex = "hmggirlfront:textures/entity/bulletbeam.png";
			    		String modelf = "gvclib:textures/entity/mflash.mqo";
			    		String texf = "gvclib:textures/entity/mflash.png";
			    		SoundEvent sounds = GVCSoundEvent.fire_grenade;
			        			AI_EntityWeapon_new we = new AI_EntityWeapon_new(this, this);
								we.id = 2;
								we.model = model;
								we.tex = tex;
								we.modelf = model;
								we.texf = tex;
								we.ftime = 0;
								we.sound = sounds;
								if(countlimit1 %2 == 0) {
									we.f = 1.57F;
								}else {
									we.f = -1.57F;
								}
								we.w = 0.7F * 1.5;
								we.h = 0.9 * 1.5;
								we.z = 1.1;
								we.bx = 0.7F;
								we.by = 0.9;
								we.bz = 0.0;
								we.follow_rote = true;//向いている方向に
								we.xoffset = 0;
								we.yoffset = -3;
								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 = 6 + this.world.getDifficulty().getDifficultyId();
								we.damegetime = false;
								we.speed = 1F;
								we.bure = 3F;
								we.ex = 2;
								we.extrue = false;
								we.kazu = 1;
								we.gra = 0.010;
								we.maxtime = 200;
								we.dameid = 0;
								we.Attacktask(this, this, 40);
					}
					this.gun_count1 = 0;
					++this.countlimit1;
				}
				if(this.countlimit1 >= 2){
					this.gun_count1 = 0;
					this.counter1 = false;
					this.countlimit1 = 0;
				}
			}
			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;
							}
							
							EntityIBC07 entityskeleton = new EntityIBC07(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_ibc07hg));
					        entityskeleton.setItemStackToSlot(EntityEquipmentSlot.OFFHAND, new ItemStack(mod_GirlFront.dollgun_ibc07sd));
					        entityskeleton.boss_spawn = true;
					        this.world.spawnEntity(entityskeleton);
				        }
				}
				if(aitypetime > 40) {
					this.aitypetime  = aitypemax;
				}
			}
			else if(this.getAIType2() == 1) {
				if(this.cooltime > 50)
				{
	    			this.counter1 = true;
	    			this.cooltime = 0;
				}
	    		if(this.counter1 && this.gun_count1 > 5){
	    			{
						String model = "hmggirlfront:textures/entity/bulletnormal.mqo";
			    		String tex = "hmggirlfront:textures/entity/bulletbeam.png";
			    		String modelf = "gvclib:textures/entity/mflash.mqo";
			    		String texf = "gvclib:textures/entity/mflash.png";
			    		SoundEvent sounds = GVCSoundEvent.fire_grenade;
			    		
			        			AI_EntityWeapon_new we = new AI_EntityWeapon_new(this, this);
								we.id = 12;
								we.model = model;
								we.tex = tex;
								we.modelf = model;
								we.texf = tex;
								we.ftime = 0;
								we.sound = sounds;
								if(countlimit1 %2 == 0) {
									we.f = 1.57F;
								}else {
									we.f = -1.57F;
								}
								we.w = 0.7F * 1.5;
								we.h = 0.9 * 1.5;
								we.z = 1.1;
								we.bx = 0.7F;
								we.by = 0.9;
								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 = 6 + this.world.getDifficulty().getDifficultyId();
								we.damegetime = false;
								we.speed = 1F;
								we.bure = 6F;
								we.ex = 2;
								we.extrue = false;
								we.kazu = 1;
								we.gra = 0.010;
								we.maxtime = 200;
								we.dameid = 0;
								we.Attacktask(this, this, 40);
					}
					this.gun_count1 = 0;
					++this.countlimit1;
				}
				if(this.countlimit1 >= 6){
					this.gun_count1 = 0;
					this.counter1 = false;
					this.countlimit1 = 0;
				}
			}
		}
		
		
    	
	}

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