package common.entity.projectile;

import java.util.List;

import common.enchantment.EnchantmentHelper;
import common.entity.DamageSource;
import common.entity.Entity;
import common.entity.EntityType;
import common.entity.types.EntityLiving;
import common.entity.types.IObjectData;
import common.entity.types.IProjectile;
import common.init.SoundEvent;
import common.tags.TagObject;
import common.util.BoundingBox;
import common.util.ExtMath;
import common.util.HitPosition;
import common.util.Vec3;
import common.vars.Vars;
import common.world.State;
import common.world.World;

public class EntityBullet extends Entity implements IProjectile, IObjectData {
	private Entity shooter;
	private int ticksMoved;
	private int age;
	private int damage = 5;

	public EntityBullet(World world) {
		super(world);
		this.renderDistWeight = 10.0D;
		this.setSize(0.5F, 0.5F);
	}

	public EntityBullet(World world, double x, double y, double z) {
		super(world);
		this.renderDistWeight = 10.0D;
		this.setSize(0.5F, 0.5F);
		this.setPosition(x, y, z);
	}

	public EntityBullet(World world, double x, double y, double z, int data) {
		this(world, x, y, z);
		Entity entity = world.getEntityByID(data);
		if(entity instanceof EntityLiving)
			this.shooter = entity;
	}

	public EntityBullet(World world, EntityLiving shooter, EntityLiving target, float velocity, float innacuracy) {
		super(world);
		this.renderDistWeight = 10.0D;
		this.shooter = shooter;
		this.posY = shooter.posY + (double)shooter.getEyeHeight() - 0.10000000149011612D;
		double xd = target.posX - shooter.posX;
		double yd = target.getEntityBoundingBox().minY + (double)(target.height * 0.7f) - this.posY;
		double zd = target.posZ - shooter.posZ;
		double xzd = (double)ExtMath.sqrtd(xd * xd + zd * zd);
		if(xzd >= 1.0E-7D) {
			float yaw = (float)(ExtMath.atan2(zd, xd) * 180.0D / Math.PI) - 90.0F;
			float pitch = (float)(-(ExtMath.atan2(yd, xzd) * 180.0D / Math.PI));
			double x = xd / xzd;
			double z = zd / xzd;
			this.setLocationAndAngles(shooter.posX + x, this.posY, shooter.posZ + z, yaw, pitch);
			this.setThrowableHeading(xd, yd, zd, velocity, innacuracy);
		}
	}

	public EntityBullet(World world, EntityLiving shooter, float velocity) {
		super(world);
		this.renderDistWeight = 10.0D;
		this.shooter = shooter;
		this.setSize(0.5F, 0.5F);
		this.setLocationAndAngles(shooter.posX, shooter.posY + (double)shooter.getEyeHeight(), shooter.posZ, shooter.rotYaw, shooter.rotPitch);
		this.posX -= (double)(ExtMath.cos(this.rotYaw / 180.0F * (float)Math.PI) * 0.16F);
		this.posY -= 0.10000000149011612D;
		this.posZ -= (double)(ExtMath.sin(this.rotYaw / 180.0F * (float)Math.PI) * 0.16F);
		this.setPosition(this.posX, this.posY, this.posZ);
		this.motionX = (double)(-ExtMath.sin(this.rotYaw / 180.0F * (float)Math.PI) * ExtMath.cos(this.rotPitch / 180.0F * (float)Math.PI));
		this.motionZ = (double)(ExtMath.cos(this.rotYaw / 180.0F * (float)Math.PI) * ExtMath.cos(this.rotPitch / 180.0F * (float)Math.PI));
		this.motionY = (double)(-ExtMath.sin(this.rotPitch / 180.0F * (float)Math.PI));
		this.setThrowableHeading(this.motionX, this.motionY, this.motionZ, velocity, 0.5F);
	}

	protected void entityInit() {
	}

	public void setThrowableHeading(double x, double y, double z, float velocity, float inaccuracy) {
		float sq = ExtMath.sqrtd(x * x + y * y + z * z);
		x = x / (double)sq;
		y = y / (double)sq;
		z = z / (double)sq;
		x = x + this.rand.gaussian() * (double)(this.rand.chance() ? -1 : 1) * 0.007499999832361937D * (double)inaccuracy;
		y = y + this.rand.gaussian() * (double)(this.rand.chance() ? -1 : 1) * 0.007499999832361937D * (double)inaccuracy;
		z = z + this.rand.gaussian() * (double)(this.rand.chance() ? -1 : 1) * 0.007499999832361937D * (double)inaccuracy;
		x = x * (double)velocity;
		y = y * (double)velocity;
		z = z * (double)velocity;
		this.motionX = x;
		this.motionY = y;
		this.motionZ = z;
		float f1 = ExtMath.sqrtd(x * x + z * z);
		this.prevYaw = this.rotYaw = (float)(ExtMath.atan2(x, z) * 180.0D / Math.PI);
		this.prevPitch = this.rotPitch = (float)(ExtMath.atan2(y, (double)f1) * 180.0D / Math.PI);
	}

	public void setClientPosition(double x, double y, double z, float yaw, float pitch, boolean teleport) {
		this.setPosition(x, y, z);
		this.setRotation(yaw, pitch);
	}

	public void setVelocity(double x, double y, double z) {
		this.motionX = x;
		this.motionY = y;
		this.motionZ = z;
		if(this.prevPitch == 0.0F && this.prevYaw == 0.0F) {
			float xz = ExtMath.sqrtd(x * x + z * z);
			this.prevYaw = this.rotYaw = (float)(ExtMath.atan2(x, z) * 180.0D / Math.PI);
			this.prevPitch = this.rotPitch = (float)(ExtMath.atan2(y, (double)xz) * 180.0D / Math.PI);
			this.prevPitch = this.rotPitch;
			this.prevYaw = this.rotYaw;
			this.setLocationAndAngles(this.posX, this.posY, this.posZ, this.rotYaw, this.rotPitch);
		}
	}

	public void onUpdate() {
		super.onUpdate();

		if(this.prevPitch == 0.0F && this.prevYaw == 0.0F) {
			float xz = ExtMath.sqrtd(this.motionX * this.motionX + this.motionZ * this.motionZ);
			this.prevYaw = this.rotYaw = (float)(ExtMath.atan2(this.motionX, this.motionZ) * 180.0D / Math.PI);
			this.prevPitch = this.rotPitch = (float)(ExtMath.atan2(this.motionY, (double)xz) * 180.0D / Math.PI);
		}

		if(!this.worldObj.client && ++this.age >= 200) {
			this.setDead();
			return;
		}
		++this.ticksMoved;
		Vec3 current = new Vec3(this.posX, this.posY, this.posZ);
		Vec3 next = new Vec3(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
		HitPosition hit = this.worldObj.rayTraceBlocks(current, next, false, true, false);
		current = new Vec3(this.posX, this.posY, this.posZ);
		next = new Vec3(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);

		if(hit != null) {
			next = new Vec3(hit.vec.xCoord, hit.vec.yCoord, hit.vec.zCoord);
		}

		Entity entity = null;
		List<Entity> list = this.worldObj.getEntitiesWithinAABBExcludingEntity(this,
				this.getEntityBoundingBox().addCoord(this.motionX, this.motionY, this.motionZ).expand(1.0D, 1.0D, 1.0D));
		double min = 0.0D;

		for(int i = 0; i < list.size(); ++i) {
			Entity ent = list.get(i);

			if(ent.canBeCollidedWith() && (ent != this.shooter || this.ticksMoved >= 5)) {
				float expand = 0.3F;
				BoundingBox bb = ent.getEntityBoundingBox().expand((double)expand, (double)expand, (double)expand);
				HitPosition entityHit = bb.calculateIntercept(current, next);

				if(entityHit != null) {
					double dist = current.squareDistanceTo(entityHit.vec);

					if(dist < min || min == 0.0D) {
						entity = ent;
						min = dist;
					}
				}
			}
		}

		if(entity != null) {
			hit = new HitPosition(entity);
		}

		if(hit != null) {
			if(hit.entity != null) {
				float velo = ExtMath.sqrtd(this.motionX * this.motionX + this.motionY * this.motionY + this.motionZ * this.motionZ);
				int damage = ExtMath.ceild((double)velo * (double)this.damage);

				DamageSource source;

				if(this.shooter == null) {
					source = DamageSource.causeShotDamage(this, this);
				}
				else {
					source = DamageSource.causeShotDamage(this, this.shooter);
				}

				if((this.worldObj.client || Vars.damageBullet) && hit.entity.attackEntityFrom(source, damage)) {
					if(hit.entity instanceof EntityLiving living && this.shooter instanceof EntityLiving shooter) {
						EnchantmentHelper.applyThornEnchantments(living, shooter);
						EnchantmentHelper.applyArthropodEnchantments(shooter, living);
					}
					this.playSound(SoundEvent.METALHIT, 0.2F);
				}
				this.setDead();
			}
			else if(!this.worldObj.client) {
				State state = hit.block != null ? this.worldObj.getState(hit.block) : null;
				if(state == null || state.getBlock().onShot(this.worldObj, hit.block, state, this)) {
					this.playSound(SoundEvent.METALHIT, 0.5F);
					this.setDead();
				}
			}
		}

		this.posX += this.motionX;
		this.posY += this.motionY;
		this.posZ += this.motionZ;
		float xzMotion = ExtMath.sqrtd(this.motionX * this.motionX + this.motionZ * this.motionZ);
		this.rotYaw = (float)(ExtMath.atan2(this.motionX, this.motionZ) * 180.0D / Math.PI);

		for(this.rotPitch = (float)(ExtMath.atan2(this.motionY, (double)xzMotion) * 180.0D / Math.PI); this.rotPitch
				- this.prevPitch < -180.0F; this.prevPitch -= 360.0F) {
			;
		}

		while(this.rotPitch - this.prevPitch >= 180.0F) {
			this.prevPitch += 360.0F;
		}

		while(this.rotYaw - this.prevYaw < -180.0F) {
			this.prevYaw -= 360.0F;
		}

		while(this.rotYaw - this.prevYaw >= 180.0F) {
			this.prevYaw += 360.0F;
		}

		this.rotPitch = this.prevPitch + (this.rotPitch - this.prevPitch) * 0.2F;
		this.rotYaw = this.prevYaw + (this.rotYaw - this.prevYaw) * 0.2F;
		this.setPosition(this.posX, this.posY, this.posZ);
	}

	public void writeEntity(TagObject tag) {
		tag.setInt("damage", this.damage);
		tag.setInt("age", this.age);
	}

	public void readEntity(TagObject tag) {
		this.damage = tag.getInt("damage");
		this.age = tag.getInt("age");
	}

	protected boolean canTriggerWalking() {
		return false;
	}

	public void setDamage(int damage) {
		this.damage = damage;
	}

	public int getDamage() {
		return this.damage;
	}

	public boolean canAttackWithItem() {
		return false;
	}

	public float getEyeHeight() {
		return 0.0F;
	}

	public int getTrackingRange() {
		return 96;
	}

	public int getUpdateFrequency() {
		return 1;
	}

	public boolean isSendingVeloUpdates() {
		return false;
	}

	public int getPacketData() {
		return this.shooter != null ? this.shooter.getId() : this.getId();
	}

	public boolean hasSpawnVelocity() {
		return true;
	}

	public EntityType getType() {
		return EntityType.PROJECTILE;
	}
}
