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.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.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.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.monster.IMob;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DamageSource;
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.DifficultyInstance;
import net.minecraft.world.World;

public class EntityIBCJUPITER extends EntityIBCBase {

	public EntityIBCJUPITER(World worldIn) {
		super(worldIn);
		this.setSize(7.0F, 7.0F);
		this.experienceValue = 10;
		this.rotationp_max = -90;
		this.rotationp_min = 20;
		this.angle_offset = 0;
		this.ai_bullet_speed_over4f = true;
	}

	protected void initEntityAI() {
	}

	protected void applyEntityAttributes() {
		super.applyEntityAttributes();
		this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(200D);
		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 && super.getCanSpawnHere();
	}
	
	protected void setEquipmentBasedOnDifficulty(DifficultyInstance difficulty) {
		super.setEquipmentBasedOnDifficulty(difficulty);
		
	}
	@Nullable
    public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, @Nullable IEntityLivingData livingdata)
    {
        livingdata = super.onInitialSpawn(difficulty, livingdata);
        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;
    	
    }
    
    @Override
    public boolean isEntityInsideOpaqueBlock()
    {
    	return false;
    }
    
    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
	        {
	        	if(this.getGene(this)){
	        		double d5 = entity2.posX - this.posX;
					double d7 = entity2.posZ - this.posZ;
					double ddx = Math.abs(d5);
					double ddz = Math.abs(d7);
					if ((ddx > 50 || ddz > 50)) {
						par2 = par2 * 0.25F;
					}else if ((ddx > 30 || ddz > 30)) {
						par2 = par2 * 0.5F;
					}else if ((ddx > 20 || ddz > 20)) {
						par2 = par2 * 0.75F;
					}
					par2 = par2 * AI_Damage.newAntiBullet(this, entity, par2, 0.1F, 0.5F, 0.5F, 0.5F);
					if(par2 <= 1.0) {
						par2 = 0;
						this.playSound(DamegeSound, 1F, 1F);
						return false;
					}else {
						return super.attackEntityFrom(source, par2);
					}
	        	}else {
	        		return super.attackEntityFrom(source, par2);
	        	}
			}
		}else{
			return super.attackEntityFrom(source, par2);
		}
    }
    
    protected void onDeathUpdate() {
    	super.onDeathUpdate();
		if (this.deathTime == 1 && !this.world.isRemote) {
			GVCExplosionBase.ExplosionKai(this, this, this.posX + 0, this.posY + 0, this.posZ + 0, 4, false,  false);
		}
	}
    
	/**
	 * 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 = 0F;
		Vec3d looked = this.getLookVec();
		
		//this.setAIType(0);
		this.updateArmSwingProgress();
		
		boolean exflag = false;
		int range = 120;
		
		//AI_EntityWeapon.getTargetEntity(this, range, 120, 5);
		IBCAI_Entity.getTargetEntity(this, 80, 120, 80, 10, 0);
		//this.getbeacon();
		
		if(this.getMoveT() == 0) {
			this.setMoveT(1);
			this.setMoveX((int)this.posX);
			this.setMoveY((int)this.posY);
			this.setMoveZ((int)this.posZ);
		}
		if(this.getMoveT() == 1)this.setPosition(this.getMoveX(), this.getMoveY(), this.getMoveZ());
		
		this.newmove(this, 1, sp, 0, 2, 120);
		
		int max = 10;
		
		if(this.getGene(this)){
    		if(this.cooltime > 80)
			{
    			{
    				String model = "hmggirlfront:textures/entity/bulletcannon.mqo";
		    		String tex = "hmggirlfront:textures/entity/bulletbeam.png";
		    		String modelf = "hmggirlfront:textures/entity/msmoke.mqo";
		    		String texf = "hmggirlfront:textures/entity/msmoke.png";
		    		SoundEvent sounds = GVCSoundEvent.fire_cannon2;
		    		if(sounds == null) {
		    			System.out.println(String.format("soundnull"));
		    		}
		    		{
		    			AI_EntityWeapon_new we = new AI_EntityWeapon_new(this, this);
						we.id = 3;
						we.model = model;
						we.tex = tex;
						we.modelf = modelf;
						we.texf = texf;
						we.ftime = 16;
						we.sound = sounds;
						we.f = 0F;
						we.w = 0.0F;
						we.h = 4.95;
						we.z = 9.75;
						we.bx = 0.0F;
						we.by = 3.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 = 90;
						we.miny = 0;
						
						we.power = 40 + this.world.getDifficulty().getDifficultyId() * 5;
						we.damegetime = false;
						we.speed = 6F;
						we.bure = 0.5F;
						we.ex = 3;
						we.extrue = false;
						we.kazu = 1;
						we.gra = 0.02999;
						we.maxtime = 20;
						we.dameid = 0;
						we.Attacktask(this, this, 120);
		    		}
		        			
				}
    			this.cooltime = 0;
			}
    	}else {
    		this.rotationYaw = 0;
    		this.renderYawOffset = 0;
    		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 = "hmggirlfront:textures/entity/mflash_ibc.mqo";
		    		String texf = "hmggirlfront:textures/entity/mflash_ibc.png";
		    		SoundEvent sounds = GFSoundEvent.fire_ibc_hg;
		    		if(sounds == null) {
		    			sounds = SoundEvent.REGISTRY.getObject(new ResourceLocation(mod_GirlFront.MOD_ID, "fire_ibc_hg"));
		    		}
		    		if(sounds == null) {
		    			System.out.println(String.format("soundnull"));
		    		}
		        			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 = 2;
							we.sound = sounds;
							we.f = 1.57F;
							we.w = 0.5F;
							we.h = 1.00;
							we.z = 4.4;
							we.bx = 0.0F;
							we.by = 1.00;
							we.bz = 4.4;
							we.follow_rote = false;//向いている方向に
							we.xoffset = 0;
							we.yoffset = 0;
							we.px = this.posX;
							we.py = this.posY;
							we.pz = this.posZ;
							we.lock = looked;
							we.rote = this.rotationYaw;
							we.maxy = 20;
							we.miny = 10;
							
							we.power = 2 + this.world.getDifficulty().getDifficultyId();
							we.damegetime = false;
							we.speed = 2F;
							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, 20);
				}
				this.gun_count1 = 0;
				++this.countlimit1;
			}
			if(this.countlimit1 >= 2){
				this.gun_count1 = 0;
				this.counter1 = false;
				this.countlimit1 = 0;
			}
    	}
		//IBCAI_EntityMoveS.newmove(this, 0, 0, 0, max, range, range);
		
	}
	
	public static boolean getGene(EntityIBCJUPITER entity) {
		boolean flag = true;
		int kazu = 0;
		double k = entity.posX;
		double l = entity.posY;
		double i = entity.posZ;
		int han = 15;
		int bit = 0;
		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 = entity.world.getEntitiesWithinAABBExcludingEntity(entity, axisalignedbb);
		if (llist != null) {
			for (int lj = 0; lj < llist.size(); lj++) {
				Entity entity1 = (Entity) llist.get(lj);
				if (entity1.canBeCollidedWith()) {
					if (entity1 instanceof EntityIBCGENE && entity1 != null) {
						++bit;
					}
				}
			}
		}
		if(bit == 0) {
			flag = false;
		}
		return flag;
	}
	
	
	public void newmove(EntityGVCLivingBase entity, int id, float sp, float turnspeed, double max, double range1) {
		entity.sneak = false;
		boolean ta = false;
		double range = range1;
		
		{
			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));
								{
									entity.rotation = entity.rotationYaw = entity.rotationYawHead = entity.renderYawOffset 
											= -((float) Math.atan2(d5, d7)) * 180.0F / (float) Math.PI;
									entity.rotationp = entity.rotationPitch = -f11 + entity.angle_offset;
									entity.noflag = 0;
									ta = true;
								}
								if (flag) {
									entity.targetentity = (EntityLivingBase) entity1;
								}
								ta = true;
								break;
							} 
						}//target
						
					}
				}
			}
		}
		{
			entity.sneak = true;
		}
	}
	
	public void MoveS(EntityGVCLivingBase entity, double speed, double han, double ex, double ey, double ez, int id, EntityLivingBase en){
		
	}
}