package common.item;

import java.util.List;
import java.util.function.Predicate;

import common.entity.Entity;
import common.entity.animal.EntityChicken;
import common.entity.npc.EntityNPC;
import common.model.Transforms;
import common.util.BoundingBox;
import common.util.Vec3;
import common.world.World;

public class ItemMagnet extends Item {
	private final boolean chicken;

	public <T extends Entity> ItemMagnet(boolean chicken) {
		this.setTab(CheatTab.tabTools);
		this.setMaxStackSize(1);
		this.chicken = chicken;
	}

//	public boolean isFull3D() {
//		return true;
//	}

//	public boolean shouldRotateAroundWhenRendering() {
//		return true;
//	}

	public ItemStack onItemRightClick(ItemStack itemStackIn, World worldIn, EntityNPC playerIn) {
		float speed = 1.0f;
		if(!worldIn.client) {
			List<Entity> list = worldIn
					.getEntitiesWithinAABB(this.chicken ? EntityChicken.class : Entity.class,
							new BoundingBox(playerIn.posX - 40.0f, playerIn.posY + playerIn.getEyeHeight() - 40.0f, playerIn.posZ - 40.0f,
									playerIn.posX + 40.0f, playerIn.posY + playerIn.getEyeHeight() + 40.0f, playerIn.posZ + 40.0f),
							new Predicate<Entity>() {
								public boolean test(Entity entity) {
									return (ItemMagnet.this.chicken || /* ? !((EntityLiving)entity).isAIDisabled() : */ entity.isMagnetic())
											&& entity.getDistanceToEntity(playerIn) <= 38.0f;
								}
							});
			for(Entity entity : list) {
				if(entity.getDistance(playerIn.posX, playerIn.posY + playerIn.getEyeHeight(), playerIn.posZ) <= 2.0f) {
					entity.motionX *= 0.1f;
					entity.motionY *= 0.1f;
					entity.motionZ *= 0.1f;
				}
				else {
					Vec3 vec = new Vec3(playerIn.posX - entity.posX, playerIn.posY - entity.posY, playerIn.posZ - entity.posZ);
					vec = vec.normalize();
					entity.addVelocity(vec.xCoord * speed, vec.yCoord * speed, vec.zCoord * speed);
				}
			}
		}
//		playerIn.triggerAchievement(StatRegistry.objectUseStats[ItemRegistry.getIdFromItem(this)]);
		return itemStackIn;
	}
    
    public boolean isMagnetic() {
    	return true;
    }

    public Transforms getTransform() {
    	return Transforms.TOOL_FLIP;
    }
}
