package hmggirlfront.entity.mob.ai;

import java.util.List;

import gvclib.entity.EntityP_Particle;
import gvclib.entity.living.EntityGVCLivingBase;
import hmggirlfront.entity.mob.EntityIBCBoss01_Bit;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.monster.IMob;
import net.minecraft.init.MobEffects;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.MathHelper;

public class IBCAI_Entity {
	/**range=通常時の索敵距離///
	 * range_active=entity.getMobMode() != 0の索敵距離///
	 * maxy,miny = 上下(値は絶対値)///
	 * notifyrange=周りの味方に知らせる距離*/
	public static void getTargetEntity(EntityGVCLivingBase entity, double range, double range_active, double maxy, double miny, double notifyrange) {
	
		
		if(entity.world.isRemote)return;
		if(entity.find_enemy_count > 0)--entity.find_enemy_count;
		
		double k = entity.posX;
		double l = entity.posY;
		double i = entity.posZ;
		double han = range;
		
		if(entity.targetentity == null) {
			if(entity.getMobMode() == 0) {
		//if(entity.targetentity == null) {
			AxisAlignedBB axisalignedbb = (new AxisAlignedBB((double) (k - han), (double) (l - miny),
					(double) (i - han), (double) (k + han), (double) (l + maxy), (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 (entity.CanAttack(entity1) && entity1 != null
								&& ((EntityLivingBase) entity1).getHealth() > 0.0F && entity.getHealth() > 0.0F) {
								boolean ridding_nottarget = true;
								if (entity1.getRidingEntity() instanceof EntityGVCLivingBase
										&& entity1.getRidingEntity() != null) {// 1
									EntityGVCLivingBase vehicle = (EntityGVCLivingBase) entity1.getRidingEntity();
									if (vehicle.ridding_nottarget) {
										ridding_nottarget = false;
									}
								}
								if (ridding_nottarget) {
									boolean flag = entity.getEntitySenses().canSee(entity1);
									if (flag) {
										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));
										float rotep_offset = -f11 + entity.angle_offset;
										if (rotep_offset <= entity.rotationp_min && rotep_offset >= entity.rotationp_max) {
											entity.find_enemy_count = entity.find_enemy_count + 2;
											double ddx = Math.abs(d5);
											double ddz = Math.abs(d7);
											if ((ddx < 5 && ddz < 5)) {
												entity.find_enemy_count = entity.find_enemy_count + 20;
											}else if ((ddx < 15 && ddz < 15)) {
												entity.find_enemy_count = entity.find_enemy_count + 10;
											}else if ((ddx < 25 && ddz < 25)) {
												entity.find_enemy_count = entity.find_enemy_count + 5;
											}
											
											
											{
												//entity.world.spawnParticle(EnumParticleTypes.LAVA, entity.posX,
												//		entity.posY + entity.height + 1, entity.posZ, 0, 0, 0);
												if(!entity.world.isRemote && entity.find_enemy_count %10 == 0){
													EntityP_Particle particle = new EntityP_Particle(entity.world);
													particle.setLocationAndAngles(entity.posX, entity.posY+entity.height + 1, entity.posZ, entity.rotationYaw, 0.0F);
													particle.setModel("hmggirlfront:textures/particle/find.mqo");
													particle.setTex("hmggirlfront:textures/particle/find.png");
													particle.startposX = entity.posX;
													particle.startposY = entity.posY+entity.height + 1;
													particle.startposZ = entity.posZ;
													particle.timemax = 20;
										            entity.world.spawnEntity(particle);
												}
												entity.rotationYawHead = entity.rotation = entity.rote = entity.renderYawOffset 
														= -((float) Math.atan2(d5, d7)) * 180.0F / (float) Math.PI;
												entity.rotationp = entity.rotationPitch = -f11 + entity.angle_offset;
											}
											
											
											if (entity.find_enemy_count > entity.find_enemy_maxcount && !((EntityLivingBase) entity1)
													.isPotionActive(MobEffects.INVISIBILITY)) {
													entity.targetentity = (EntityLivingBase) entity1;
													entity.setMobMode(1);
													entity.sneak = true;
													{
														if(!entity.world.isRemote){
															EntityP_Particle particle = new EntityP_Particle(entity.world);
															particle.setLocationAndAngles(entity.posX, entity.posY+entity.height + 1, entity.posZ, entity.rotationYaw, 0.0F);
															particle.setModel("hmggirlfront:textures/particle/search.mqo");
															particle.setTex("hmggirlfront:textures/particle/search.png");
															particle.startposX = entity.posX;
															particle.startposY = entity.posY+entity.height + 1;
															particle.startposZ = entity.posZ;
															particle.timemax = 20;
												            entity.world.spawnEntity(particle);
														}
													}
													notifyEntity(entity, notifyrange, (EntityLivingBase) entity1, true);
													if (!entity.getattacktask()) {
														//entity.world.spawnParticle(EnumParticleTypes.NOTE, entity.posX,
														//		entity.posY + entity.height + 1, entity.posZ, 0, 0, 0);
														
														entity.setattacktask(true);
													}
													break;
											}
											break;
										}
									}
								}
						}
					}
				}
			
			}
			if (entity.biped) {
				if (entity.isPotionActive(MobEffects.BLINDNESS)) {
					entity.targetentity = null;
					entity.setMobMode(1);
					entity.sneak = false;
					entity.setattacktask(false);
				}
			}
		}else {
			AxisAlignedBB axisalignedbb = (new AxisAlignedBB((double) (k - range_active), (double) (l - miny),
					(double) (i - range_active), (double) (k + range_active), (double) (l + maxy), (double) (i + range_active))).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 (entity.CanAttack(entity1) && entity1 != null
								&& ((EntityLivingBase) entity1).getHealth() > 0.0F && entity.getHealth() > 0.0F) {
								boolean ridding_nottarget = true;
								if (entity1.getRidingEntity() instanceof EntityGVCLivingBase
										&& entity1.getRidingEntity() != null) {// 1
									EntityGVCLivingBase vehicle = (EntityGVCLivingBase) entity1.getRidingEntity();
									if (vehicle.ridding_nottarget) {
										ridding_nottarget = false;
									}
								}
								if (ridding_nottarget) {
									boolean flag = entity.getEntitySenses().canSee(entity1);
									if (flag) {
										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));
										float rotep_offset = -f11 + entity.angle_offset;
										if (rotep_offset <= entity.rotationp_min && rotep_offset >= entity.rotationp_max) {
											if (!((EntityLivingBase) entity1)
													.isPotionActive(MobEffects.INVISIBILITY)) {
													entity.targetentity = (EntityLivingBase) entity1;
													entity.setMobMode(1);
													entity.sneak = true;
													notifyEntity(entity, notifyrange, (EntityLivingBase) entity1, true);
													{
														if(!entity.world.isRemote){
															EntityP_Particle particle = new EntityP_Particle(entity.world);
															particle.setLocationAndAngles(entity.posX, entity.posY+entity.height + 1, entity.posZ, entity.rotationYaw, 0.0F);
															particle.setModel("hmggirlfront:textures/particle/search.mqo");
															particle.setTex("hmggirlfront:textures/particle/search.png");
															particle.startposX = entity.posX;
															particle.startposY = entity.posY+entity.height + 1;
															particle.startposZ = entity.posZ;
															particle.timemax = 20;
												            entity.world.spawnEntity(particle);
														}
													}
													if (!entity.getattacktask()) {
														entity.setattacktask(true);
													}
													break;
											}
											break;
										}
									}
								}
						}
					}
				}
			
			}
			if (entity.biped) {
				if (entity.isPotionActive(MobEffects.BLINDNESS)) {
					entity.targetentity = null;
					entity.setMobMode(1);
					entity.sneak = false;
					entity.setattacktask(false);
				}
			}
		}
	}
		
		
		
	}
	
	
	public static void notifyEntity(EntityGVCLivingBase entity, double notifyrange, EntityLivingBase target, boolean cansee) {
		double k = entity.posX;
		double l = entity.posY;
		double i = entity.posZ;

		AxisAlignedBB axisalignedbb = (new AxisAlignedBB((double) (k - notifyrange), (double) (l - notifyrange),
				(double) (i - notifyrange), (double) (k + notifyrange), (double) (l + notifyrange),
				(double) (i + notifyrange))).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 IMob && entity1 != null && ((EntityLivingBase) entity1).getHealth() > 0.0F
							&& entity.getHealth() > 0.0F) {
						if (entity1 instanceof EntityGVCLivingBase) {
							EntityGVCLivingBase mob = (EntityGVCLivingBase) entity1;
							boolean see = entity.getEntitySenses().canSee(entity1);
							boolean flag = true;
							if(cansee && !see) {
								flag = false;
							}
							if (flag && mob.targetentity == null) {
								{
									mob.targetentity = target;
									mob.setMobMode(1);
									mob.sneak = true;
									{
										if (!mob.world.isRemote) {
											EntityP_Particle particle = new EntityP_Particle(mob.world);
											particle.setLocationAndAngles(mob.posX, mob.posY + mob.height + 1, mob.posZ,
													mob.rotationYaw, 0.0F);
											particle.setModel("hmggirlfront:textures/particle/search.mqo");
											particle.setTex("hmggirlfront:textures/particle/search.png");
											particle.startposX = mob.posX;
											particle.startposY = mob.posY + mob.height + 1;
											particle.startposZ = mob.posZ;
											particle.timemax = 20;
											mob.world.spawnEntity(particle);
										}
									}
									if (!mob.getattacktask()) {
										mob.setattacktask(true);
									}

								}
							}
							break;
						}
					}
				}
			}
		}
	}
	
}
