package common.entity.item;

import common.entity.DamageSource;
import common.entity.Entity;
import common.entity.types.EntityLiving;
import common.entity.types.EntityThrowable;
import common.init.Config;
import common.model.ParticleType;
import common.util.HitPosition;
import common.world.World;

public class EntityOrb extends EntityThrowable
{
    private EntityLiving thrower;

    public EntityOrb(World worldIn)
    {
        super(worldIn);
    }

    public EntityOrb(World worldIn, EntityLiving thrower)
    {
        super(worldIn, thrower);
        this.thrower = thrower;
    }

    public EntityOrb(World worldIn, double x, double y, double z)
    {
        super(worldIn, x, y, z);
    }

    protected float getVelocity()
    {
        return 3.0F;
    }

    /**
     * Called when this EntityThrowable hits a block or entity.
     */
    protected void onImpact(HitPosition pos)
    {
        EntityLiving entitylivingbase = this.getThrower();
        double x = this.posX;
        double y = this.posY;
        double z = this.posZ;

        if (pos.entity != null)
        {
//            if (pos.entity == this.thrower && !(this.thrower.isPlayer()))
//            {
//                this.setDead();
//                return;
//            }

            if(Config.telefrag) {
            	x = pos.entity.posX;
                y = pos.entity.posY;
                z = pos.entity.posZ;
            }
//            else if(Config.smackingOrb) {
//            	pos.entityHit.attackEntityFrom(DamageSource.causeThrownDamage(this, entitylivingbase), Config.orbThorns);
//            }
        }

        for (int i = 0; i < 32; ++i)
        {
            this.worldObj.spawnParticle(ParticleType.PORTAL, this.posX, this.posY + this.rand.doublev() * 2.0D, this.posZ, this.rand.gaussian(), 0.0D, this.rand.gaussian());
        }

        if (!this.worldObj.client)
        {
//            if(entitylivingbase != null)
            if (entitylivingbase != null) // && entitylivingbase.isPlayer())
            {
            	this.worldObj.playAuxSFX(1013, entitylivingbase.getPosition(), 0);
//                EntityNPCMP entityplayermp = (EntityNPCMP)entitylivingbase;

                if (/* entityplayermp.netHandler.connection.isChannelOpen() && */ entitylivingbase.worldObj == this.worldObj) // && !entityplayermp.isPlayerSleeping())
                {
//                    if (this.rand.floatv() < 0.05F && Config.mobSpawning && Config.spawnEndermites)
//                    {
//                        EntityEndermite entityendermite = new EntityEndermite(this.worldObj);
//                        entityendermite.setSpawnedByPlayer(true);
//                        entityendermite.setLocationAndAngles(entitylivingbase.posX, entitylivingbase.posY, entitylivingbase.posZ, entitylivingbase.rotYaw, entitylivingbase.rotPitch);
//                        this.worldObj.spawnEntityInWorld(entityendermite);
//                    }

                    if (entitylivingbase.isRiding())
                    {
                        entitylivingbase.mountEntity((Entity)null);
                    }

                    entitylivingbase.setPositionAndUpdate(x, y, z);
                    entitylivingbase.fallDistance = 0.0F;
                    if(Config.orbDamageSelf > 0)
                    	entitylivingbase.attackEntityFrom(DamageSource.fall, Config.orbDamageSelf);
                }
            }
//            else if (entitylivingbase != null)
//            {
//                entitylivingbase.setPositionAndUpdate(x, y, z);
//                entitylivingbase.fallDistance = 0.0F;
//            }

            this.setDead();
            if(entitylivingbase != null)
            	this.worldObj.playAuxSFX(1005, entitylivingbase.getPosition(), 0);
        }
        
        if (pos.entity != null)
        {
            if(Config.telefrag) {
            	pos.entity.attackEntityFrom(DamageSource.causeTeleFragDamage(this, entitylivingbase), 10000);
            	if(!this.worldObj.client)
            		this.worldObj.playAuxSFX(1017, pos.entity.getPosition(), 0);
            }
            else if(Config.knockOrb) {
            	pos.entity.attackEntityFrom(DamageSource.causeThrownDamage(this, entitylivingbase), Config.orbDamageOther);
            }
        }
    }

    /**
     * Called to update the entity's position/logic.
     */
    public void onUpdate()
    {
        EntityLiving entitylivingbase = this.getThrower();

        if (entitylivingbase != null && /* entitylivingbase.isPlayer() && */ !entitylivingbase.isEntityAlive())
        {
            this.setDead();
        }
        else
        {
            super.onUpdate();
        }
    }
}
