package common.entity.item;

import common.block.Material;
import common.color.TextColor;
import common.entity.DamageSource;
import common.entity.Entity;
import common.entity.EntityType;
import common.entity.npc.EntityNPC;
import common.entity.types.IObjectData;
import common.init.SoundEvent;
import common.model.ParticleType;
import common.tags.TagObject;
import common.util.BlockPos;
import common.util.ExtMath;
import common.util.PortalType;
import common.vars.Vars;
import common.world.World;

public class EntityXp extends Entity implements IObjectData
{
    /**
     * A constantly increasing value that RenderXPOrb uses to control the colour shifting (Green / yellow)
     */
    public int xpColor;

    /** The age of the XP orb in ticks. */
    public int xpOrbAge;
    public int delayBeforeCanPickup;

    /** The health of this XP orb. */
    private int xpOrbHealth = 5;

    /** This is how much XP this orb has. */
    private int xpValue;
//
//    /** The closest pulling Entity to this orb. */
//    private Entity closestPlayer;
//
//    /** Threshold color for tracking players */
//    private int xpTargetColor;

    public EntityXp(World worldIn, double x, double y, double z, int expValue)
    {
        super(worldIn);
        this.setSize(0.5F, 0.5F);
        this.setPosition(x, y, z);
        this.rotYaw = (float)(Math.random() * 360.0D);
        this.motionX = (double)((float)(Math.random() * 0.20000000298023224D - 0.10000000149011612D) * 2.0F);
        this.motionY = (double)((float)(Math.random() * 0.2D) * 2.0F);
        this.motionZ = (double)((float)(Math.random() * 0.20000000298023224D - 0.10000000149011612D) * 2.0F);
        this.xpValue = expValue;
        if(!worldIn.client)
        	this.setCustomNameTag("" + this.xpValue);
    }

    /**
     * returns if this entity triggers Block.onEntityWalking on the blocks they walk on. used for spiders and wolves to
     * prevent them from trampling crops
     */
    protected boolean canTriggerWalking()
    {
        return false;
    }

    public EntityXp(World worldIn)
    {
        super(worldIn);
        this.setSize(0.25F, 0.25F);
    }

    protected void entityInit()
    {
    }

    public int getBrightnessForRender(float partialTicks)
    {
        float f = 0.5F;
        f = ExtMath.clampf(f, 0.0F, 1.0F);
        int i = super.getBrightnessForRender(partialTicks);
        int j = i & 255;
        int k = i >> 16 & 255;
        j = j + (int)(f * 15.0F * 16.0F);

        if (j > 240)
        {
            j = 240;
        }

        return j | k << 16;
    }

    /**
     * Called to update the entity's position/logic.
     */
    public void onUpdate()
    {
        super.onUpdate();

        if (this.delayBeforeCanPickup > 0)
        {
            --this.delayBeforeCanPickup;
        }

        this.prevX = this.posX;
        this.prevY = this.posY;
        this.prevZ = this.posZ;
        this.motionY -= 0.029999999329447746D;

        if (this.worldObj.getState(new BlockPos(this)).getBlock().getMaterial() == Material.LAVA)
        {
            this.motionY = 0.20000000298023224D;
            this.motionX = (double)((this.rand.floatv() - this.rand.floatv()) * 0.2F);
            this.motionZ = (double)((this.rand.floatv() - this.rand.floatv()) * 0.2F);
            this.playSound(SoundEvent.FIZZ, 0.4F);
        }

        this.pushOutOfBlocks(this.posX, (this.getEntityBoundingBox().minY + this.getEntityBoundingBox().maxY) / 2.0D, this.posZ);
        double d0 = 8.0D;

//        if (!this.worldObj.client && this.xpTargetColor < this.xpColor - 20 + this.getId() % 100)
//        {
//            if (this.closestPlayer == null || this.closestPlayer.getDistanceSqToEntity(this) > d0 * d0)
//            {
//            	List<EntityXp> list = this.worldObj.getEntitiesWithinAABB(EntityXp.class, 
//            			this.getEntityBoundingBox().expand(1.5D, 1.0D, 1.5D), new Predicate<EntityXp>() {
//							public boolean test(EntityXp entity) {
//								return entity != EntityXp.this && entity.isEntityAlive() && EntityXp.this.xpValue + entity.xpValue <= 2477;
//							}
//						});
//                this.closestPlayer = list.isEmpty() ? this.worldObj.getClosestPlayerToEntity(this, d0) : list.get(0);
//            }
//
//            this.xpTargetColor = this.xpColor;
//        }

//        if (this.closestPlayer != null && this.closestPlayer.isSpectator())
//        {
//            this.closestPlayer = null;
//        }

//        if (this.ticksExisted % 25 == 0)
//        {
//            if (!this.worldObj.client)
//            {
//            }
//        }
//        if (this.closestPlayer != null)
//        {
//            double d1 = (this.closestPlayer.posX - this.posX) / d0;
//            double d2 = (this.closestPlayer.posY + (double)this.closestPlayer.getEyeHeight() - this.posY) / d0;
//            double d3 = (this.closestPlayer.posZ - this.posZ) / d0;
//            double d4 = Math.sqrt(d1 * d1 + d2 * d2 + d3 * d3);
//            double d5 = 1.0D - d4;
//
//            if (d5 > 0.0D)
//            {
//                d5 = d5 * d5;
//                this.motionX += d1 / d4 * d5 * 0.1D;
//                this.motionY += d2 / d4 * d5 * 0.1D;
//                this.motionZ += d3 / d4 * d5 * 0.1D;
//            }
//        }

        this.moveEntity(this.motionX, this.motionY, this.motionZ);
        boolean flag = (int)this.prevX != (int)this.posX || (int)this.prevY != (int)this.posY || (int)this.prevZ != (int)this.posZ;

        if (flag || this.ticksExisted % 25 == 0)
        {
            if (!this.worldObj.client)
            {
            	this.searchForOtherXpNearby();
            }
        }
        float f = 0.98F;

        if (this.onGround)
        {
            f = this.worldObj.getState(new BlockPos(ExtMath.floord(this.posX), ExtMath.floord(this.getEntityBoundingBox().minY) - 1, ExtMath.floord(this.posZ))).getBlock().slipperiness * 0.98F;
        }

        this.motionX *= (double)f;
        this.motionY *= 0.9800000190734863D;
        this.motionZ *= (double)f;

        if (this.onGround)
        {
            this.motionY *= -0.8999999761581421D;
        }

        ++this.xpColor;
        ++this.xpOrbAge;

        if (!this.worldObj.client && this.xpOrbAge >= 6000)
        {
            this.setDead();
        }
    }
    
    public Entity travelToDimension(int dimensionId, BlockPos pos, float yaw, float pitch, PortalType portal)
    {
        Entity entity = super.travelToDimension(dimensionId, pos, yaw, pitch, portal);

        if (!this.worldObj.client)
        {
            this.searchForOtherXpNearby();
        }
        
        return entity;
    }
    
    private void searchForOtherXpNearby()
    {
        for (EntityXp entityitem : this.worldObj.getEntitiesWithinAABB(EntityXp.class, this.getEntityBoundingBox().expand(3.0D, 3.0D, 3.0D)))
        {
            this.combineXp(entityitem);
        }
    }
    
    private boolean combineXp(EntityXp other)
    {
        if (other == this)
        {
            return false;
        }
        else if (other.isEntityAlive() && this.isEntityAlive())
        {
            if (other.xpValue < this.xpValue)
            {
                return other.combineXp(this);
            }
            else if (this.xpValue + other.xpValue > 2477)
            {
                return false;
            }
            else
            {
            	other.xpValue += this.xpValue;
                other.delayBeforeCanPickup = Math.max(other.delayBeforeCanPickup, this.delayBeforeCanPickup);
                other.xpOrbAge = Math.min(other.xpOrbAge, this.xpOrbAge);
                other.worldObj.setEntityState(other, (byte)other.getTextureByXP());
                other.setCustomNameTag("" + other.xpValue);
                this.setDead();
                return true;
            }
        }
        else
        {
            return false;
        }
    }

    /**
     * Returns if this entity is in water and will end up adding the waters velocity to the entity
     */
    public boolean handleWaterMovement()
    {
        return this.worldObj.handleLiquidAcceleration(this.getEntityBoundingBox(), this);
    }

    /**
     * Will deal the specified amount of damage to the entity if the entity isn't immune to fire damage. Args:
     * amountDamage
     */
    protected void dealFireDamage(int amount)
    {
    	if(Vars.xpOrbBurn)
    		this.attackEntityFrom(DamageSource.inFire, amount);
    }

    /**
     * Called when the entity is attacked.
     */
    public boolean attackEntityFrom(DamageSource source, int amount)
    {
//        if (this.isEntityInvulnerable(source))
//        {
//            return false;
//        }
//        else
//        {
        this.setBeenAttacked();
        this.xpOrbHealth = this.xpOrbHealth - (int)amount;

        if (this.xpOrbHealth <= 0)
        {
            this.setDead();
        }

        return false;
//        }
    }

    /**
     * (abstract) Protected helper method to write subclass entity data to NBT.
     */
    public void writeEntity(TagObject tagCompound)
    {
        tagCompound.setShort("Health", (short)((byte)this.xpOrbHealth));
        tagCompound.setShort("Age", (short)this.xpOrbAge);
        tagCompound.setShort("Value", (short)this.xpValue);
    }

    /**
     * (abstract) Protected helper method to read subclass entity data from NBT.
     */
    public void readEntity(TagObject tagCompund)
    {
        this.xpOrbHealth = tagCompund.getShort("Health") & 255;
        this.xpOrbAge = tagCompund.getShort("Age");
        this.xpValue = tagCompund.getShort("Value");
        this.setCustomNameTag("" + this.xpValue);
    }

    /**
     * Called by a player entity when they collide with an entity
     */
    public void onCollideWithPlayer(EntityNPC entityIn)
    {
        if (!this.worldObj.client)
        {
            if (this.delayBeforeCanPickup == 0 && entityIn.xpCooldown == 0)
            {
                entityIn.xpCooldown = Vars.xpDelay;
                this.worldObj.playSoundAtEntity(entityIn, SoundEvent.ORB, 0.1F);
                entityIn.onItemPickup(this, this.xpValue);
                entityIn.addExperience(this.xpValue);
                this.setDead();
            }
        }
    }

    /**
     * Returns the XP value of this XP orb.
     */
    public int getXpValue()
    {
        return this.xpValue;
    }

    public void handleStatusUpdate(byte id)
    {
    	this.xpValue = id;
    	for (int i = 0; i < 2; i++)
        {
            double d0 = this.rand.gaussian() * 0.02D;
            double d1 = this.rand.gaussian() * 0.02D;
            double d2 = this.rand.gaussian() * 0.02D;
            this.worldObj.spawnParticle(ParticleType.GROW, 
            		this.posX + (double)(this.rand.floatv() * this.width) - (double)this.width * 0.5, 
            		this.posY + (double)(this.rand.floatv() * this.height), 
            		this.posZ + (double)(this.rand.floatv() * this.width) - (double)this.width * 0.5, 
            		d0, d1, d2);
        }
    }

    /**
     * Returns a number from 1 to 10 based on how much XP this orb is worth. This is used by RenderXPOrb to determine
     * what texture to use.
     */
    public int getTextureByXP()
    {
        return this.xpValue >= 2477 ? 10 : (this.xpValue >= 1237 ? 9 : (this.xpValue >= 617 ? 8 : (this.xpValue >= 307 ? 7 : (this.xpValue >= 149 ? 6 : (this.xpValue >= 73 ? 5 : (this.xpValue >= 37 ? 4 : (this.xpValue >= 17 ? 3 : (this.xpValue >= 7 ? 2 : (this.xpValue >= 3 ? 1 : 0)))))))));
    }

    /**
     * Get a fragment of the maximum experience points value for the supplied value of experience points value.
     */
    public static int getXPSplit(int expValue)
    {
        return expValue >= 2477 ? 2477 : expValue;
    }

    /**
     * If returns false, the item will not inflict any damage against entities.
     */
    public boolean canAttackWithItem()
    {
        return false;
    }
    
    public int getTrackingRange() {
    	return 160;
    }
    
    public int getUpdateFrequency() {
    	return 20;
    }
    
    public boolean isSendingVeloUpdates() {
    	return true;
    }
	
	public int getPacketData() {
		return this.getTextureByXP();
	}
	
//	public boolean hasSpawnVelocity() {
//		return false;
//	}
    
    public String getDisplayName()
    {
//    	if(!this.hasCustomName()) {
//    		return super.getDisplayName();
//    	}
    	return this.hasCustomName() ? TextColor.GREEN + this.getCustomNameTag() : null;
    }
    
    public EntityType getType() {
    	return EntityType.OBJECT;
    }
}
