package hmggirlfront.entity;

import java.util.List;

import gvclib.entity.EntityP_Particle;
import gvclib.entity.living.EntityGVCLivingBase;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.init.MobEffects;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.MathHelper;

public class DollAI_EntityWeapon {
	public static void getTargetEntity(EntityDollBase entity, double range, double maxy, double miny){
		double k = entity.posX;
		double l = entity.posY;
		double i = entity.posZ;
		double han = range;
		
		Entity target = null;
		Entity[] targetlist = new Entity[128];
		int targetplus = 0;
		
		
		if(!entity.world.isRemote){
			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) 
						{
//							if(entity.targetentity == null) 
							{
								boolean flag = entity.getEntitySenses().canSee(entity1);
								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 + 10;
					            if(rotep_offset <= entity.rotationp_min && rotep_offset >= entity.rotationp_max && flag && targetplus < 127) {
					            	targetlist[targetplus] = entity1;
									++targetplus;
								}
							}
						}
					}
				}
			}
			
			for(int xs = 0; xs < targetlist.length; ++xs) {
				if(targetlist[xs] != null) {
					if(entity.gun_class == 3) {
						if(target != null) {
							double d5 = targetlist[xs].posX - entity.posX;
							double d7 = targetlist[xs].posZ - entity.posZ;
							double ddx = Math.abs(d5);
							double ddz = Math.abs(d7);
							
							double d51 = target.posX - entity.posX;
							double d71 = target.posZ - entity.posZ;
							double ddx1 = Math.abs(d51);
							double ddz1 = Math.abs(d71);
							if((ddx > ddx1 && ddz > ddz1)) {
								target = targetlist[xs];
							}
						}else {
							target = targetlist[xs];
						}
					}
					else {
						//target = targetlist[xs];
						if(target != null) {
							double d5 = targetlist[xs].posX - entity.posX;
							double d7 = targetlist[xs].posZ - entity.posZ;
							double ddx = Math.abs(d5);
							double ddz = Math.abs(d7);
							
							double d51 = target.posX - entity.posX;
							double d71 = target.posZ - entity.posZ;
							double ddx1 = Math.abs(d51);
							double ddz1 = Math.abs(d71);
							if((ddx < ddx1 || ddz < ddz1)) {
								target = targetlist[xs];
							}
						}else {
							target = targetlist[xs];
						}
	//					break;
					}
				}
			}
			if(target != null) {
				{
					entity.targetentity = (EntityLivingBase) target;
					entity.setMobMode(1);
					entity.sneak = true;
					if(!entity.getattacktask())
					{
						//entity.world.spawnParticle(EnumParticleTypes.NOTE, 
						//		entity.posX,  entity.posY + entity.height + 1, entity.posZ, 0, 0, 0);
						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);
						}
						entity.setattacktask(true);
        			}
				}
			}
			
			
			if(entity.biped) {
				if (entity.isPotionActive(MobEffects.BLINDNESS)) {
					entity.targetentity = null;
					entity.setMobMode(1);
					entity.sneak = false;
					entity.setattacktask(false);
				}
			}
			
		}
	}
}
