package hmggirlfront.entity.vehicle;

import java.util.List;

import gvclib.entity.living.EntityGVCLivingBase;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.MathHelper;

public class DollAI_GetTarget {
	public static void load(EntityGVCLivingBase entity, double range, double maxy, double miny, float yawbase,  float maxyaw, float minyaw){
		double k = entity.posX;
		double l = entity.posY;
		double i = entity.posZ;
		double han = range;
		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;
					            float yaw= -((float) Math.atan2(d5, d7)) * 180.0F / (float) Math.PI;
					            if(rotep_offset <= entity.rotationp_min && rotep_offset >= entity.rotationp_max && flag) {
					            	boolean ok = false;
					            	if(yaw > yawbase - minyaw && yaw < yawbase + maxyaw){
					            		ok = true;
					            	}
					            	if(yawbase - minyaw < -180){
					            		float y = yawbase - minyaw + 360;
					            		if(yaw < yawbase + maxyaw && yaw + 360 > y) {
					            			ok = true;
					            		}
					            	}
					            	if(yawbase + maxyaw > 180){
					            		float y = yawbase + maxyaw - 360;
					            		if(yaw > yawbase - minyaw && yaw - 360 < y) {
					            			ok = true;
					            		}
					            	}
									if(ok){
										entity.targetentity = (EntityLivingBase) entity1;
										entity.setMobMode(1);
										entity.sneak = true;
										entity.setattacktask(true);
										break;
									}
								}
							}
						}
					}
				}
			}
		}
	}
	
	public static boolean getRange(EntityGVCLivingBase entity, double range, double maxy, double miny, float yawbase,  float maxyaw, float minyaw){
		boolean task = false;
		double k = entity.posX;
		double l = entity.posY;
		double i = entity.posZ;
		double han = range;
		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);
				//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()) {
					if (entity.targetentity == entity1 && entity.CanAttack(entity1) && entity1 != null 
							&& ((EntityLivingBase) entity1).getHealth() > 0.0F && entity.getHealth() > 0.0F) 
					{
						boolean flag = entity.getEntitySenses().canSee(entity1);
						double ddy = Math.abs(entity1.posY - entity.posY);
						double dyp = entity.posY + maxy;
						double dym = entity.posY - miny;
						double d5 = entity1.posX - entity.posX;
						double d7 = entity1.posZ - entity.posZ;
						float yaw= -((float) Math.atan2(d5, d7)) * 180.0F / (float) Math.PI;
						if (dyp > entity1.posY && dym < entity1.posY) {
							boolean ok = false;
			            	if(yaw > yawbase - minyaw && yaw < yawbase + maxyaw){
			            		ok = true;
			            	}
			            	if(yawbase - minyaw < -180){
			            		float y = yawbase - minyaw + 360;
			            		if(yaw < yawbase + maxyaw && yaw + 360 > y) {
			            			ok = true;
			            		}
			            	}
			            	if(yawbase + maxyaw > 180){
			            		float y = yawbase + maxyaw - 360;
			            		if(yaw > yawbase - minyaw && yaw - 360 < y) {
			            			ok = true;
			            		}
			            	}
			            	if(ok) {
			            		if(entity1.isRiding() && entity1.getRidingEntity() != null && entity1.getRidingEntity() instanceof EntityLivingBase) {
									EntityLivingBase en = (EntityLivingBase) entity1.getRidingEntity();
									if(en.getHealth()> 0.0F ) {
										task = true;
										break;
									}
								}else{
									task = true;
									break;
								}
			            	}
						}
					}
				}
			}
		}
		return task;
	}
}
