package common.entity.npc;

import common.attributes.Attributes;
import common.entity.types.EntityLiving;
import common.packet.CPacketAction;
import common.world.World;

public abstract class EntityHoveringNPC extends EntityNPC
{
    private float heightOffset = 0.5F;
    private int heightOffsetUpdateTime;

    private boolean serverHoverState;

    public EntityHoveringNPC(World worldIn)
    {
        super(worldIn);
//        this.xpValue = 10;
//        this.tasks.addTask(4, new AISmallFireballAttack(this));
//        this.tasks.addTask(5, new EntityAIMoveTowardsRestriction(this, 1.0D));
//        this.tasks.addTask(7, new EntityAIWander(this, 1.0D));
//        this.tasks.addTask(8, new EntityAIWatchClosest(this, EntityNPC.class, 8.0F));
//        this.tasks.addTask(8, new EntityAILookIdle(this));
//        this.targets.addTask(1, new EntityAIHurtByTarget(this, true));
//        this.targets.addTask(2, new EntityAINearestAttackableTarget(this, EntityNPC.class, true));
//        this.targets.addTask(3, new EntityAINearestAttackableTarget(this, EntityNPC.class, true));
    }

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

    protected void applyEntityAttributes()
    {
        super.applyEntityAttributes();
        this.getEntityAttribute(Attributes.FOLLOW_RANGE).setBaseValue(32.0D);
    }
    
    public boolean isHovering()
    {
        return this.getFlag(6);
    }

    public void setHovering(boolean hover)
    {
        this.setFlag(6, hover);
    }

//    protected void entityInit()
//    {
//        super.entityInit();
//        this.dataWatcher.addObject(16, new Byte((byte)0));
//    }

//    /**
//     * Returns the sound this mob makes while it's alive.
//     */
//    protected String getLivingSound()
//    {
//        return "mob.blaze.breathe";
//    }
//
//    /**
//     * Returns the sound this mob makes when it is hurt.
//     */
//    protected String getHurtSound()
//    {
//        return "mob.blaze.hit";
//    }
//
//    /**
//     * Returns the sound this mob makes on death.
//     */
//    protected String getDeathSound()
//    {
//        return "mob.blaze.death";
//    }
//
//    public int getBrightnessForRender(float partialTicks)
//    {
//        return 15728880;
//    }
//
//    /**
//     * Gets how bright this entity is.
//     */
//    public float getBrightness(float partialTicks)
//    {
//        return 1.0F;
//    }
    
    public void onUpdateWalkingPlayer()
    {
        boolean flag = this.isHovering();
        
        if (flag != this.serverHoverState)
        {
            if (flag)
            {
                this.client.addToSendQueue(new CPacketAction(CPacketAction.Action.START_HOVER));
            }
            else
            {
                this.client.addToSendQueue(new CPacketAction(CPacketAction.Action.STOP_HOVER));
            }

            this.serverHoverState = flag;
        }
        
    	super.onUpdateWalkingPlayer();
    }
    
    public void onLivingUpdate()
    {
    	if(this.client != null) {
    		if(this.client.isJumping() && this.client.isSprinting() && this.client.getMoveForward() == 0.0f && this.client.getMoveStrafe() == 0.0f)
    			this.setHovering(true);
    		if(this.isFlying() || (!this.client.isJumping() && this.client.isSneaking() && this.onGround))
    			this.setHovering(false);
    	}
        if (!this.onGround && this.motionY < 0.0D && (!this.isPlayer() || (!this.isFlying() && this.isHovering() && !this.jumping)))
        {
        	if(this.isPlayer() && this.isSneaking())
        		this.motionY = -0.1D;
        	else
        		this.motionY *= 0.6D;
        }
        if (!this.onGround && this.isPlayer() && !this.isFlying() && !this.isSneaking() && this.isHovering())
        {
            this.motionY = this.jumping ? 0.1D : -0.025D;
        }

//        if (this.worldObj.client)
//        {
//            if (this.rand.chance(24) && !this.isSilent())
//            {
//                ((WorldClient)this.worldObj).playSound(this.posX + 0.5D, this.posY + 0.5D, this.posZ + 0.5D, "random.fire", 1.0F + this.rand.floatv(), this.rand.floatv() * 0.7F + 0.3F);
//            }
//
//            for (int i = 0; i < 2; ++i)
//            {
//                this.worldObj.spawnParticle(EnumParticleTypes.SMOKE_LARGE, this.posX + (this.rand.doublev() - 0.5D) * (double)this.width, this.posY + this.rand.doublev() * (double)this.height, this.posZ + (this.rand.doublev() - 0.5D) * (double)this.width, 0.0D, 0.0D, 0.0D);
//            }
//        }

        super.onLivingUpdate();
    }

    protected float getJumpUpwardsMotion()
    {
    	return !this.isPlayer() ? super.getJumpUpwardsMotion() : (super.getJumpUpwardsMotion());
    }

    protected void updateAITasks()
    {
//        if (Config.blazeWaterDamage && this.isWet())
//        {
//            this.attackEntityFrom(DamageSource.drown, 1);
//        }

        --this.heightOffsetUpdateTime;

        if (this.heightOffsetUpdateTime <= 0)
        {
            this.heightOffsetUpdateTime = 100;
            this.heightOffset = 0.5F + (float)this.rand.gaussian() * 3.0F;
        }

        EntityLiving entitylivingbase = this.getAttackTarget();

        if (entitylivingbase != null && entitylivingbase.posY + (double)entitylivingbase.getEyeHeight() > this.posY + (double)this.getEyeHeight() + (double)this.heightOffset)
        {
            this.motionY += (0.30000001192092896D - this.motionY) * 0.30000001192092896D;
            this.isAirBorne = true;
        }

        super.updateAITasks();
    }

    public void fall(float distance, float damageMultiplier)
    {
    }

//    protected Item getDropItem()
//    {
//        return Items.blaze_rod;
//    }
    
//    public boolean isBurning()
//    {
//        return this.func_70845_n();
//    }

//    /**
//     * Drop 0-2 items of this living's type
//     *  
//     * @param wasRecentlyHit true if this this entity was recently hit by appropriate entity (generally only if player
//     * or tameable)
//     * @param lootingModifier level of enchanment to be applied to this drop
//     */
//    protected void dropFewItems(boolean wasRecentlyHit, int lootingModifier)
//    {
//        if (wasRecentlyHit)
//        {
//            int i = this.rand.zrange(2 + lootingModifier);
//
//            for (int j = 0; j < i; ++j)
//            {
//                this.dropItem(Items.blaze_rod, 1);
//            }
//        }
//    }

//    public boolean func_70845_n()
//    {
//        return (this.dataWatcher.getWatchableObjectByte(16) & 1) != 0;
//    }
//
//    public void setOnFire(boolean onFire)
//    {
//        byte b0 = this.dataWatcher.getWatchableObjectByte(16);
//
//        if (onFire)
//        {
//            b0 = (byte)(b0 | 1);
//        }
//        else
//        {
//            b0 = (byte)(b0 & -2);
//        }
//
//        this.dataWatcher.updateObject(16, Byte.valueOf(b0));
//    }

//    /**
//     * Checks to make sure the light is not too bright where the mob is spawning
//     */
//    protected boolean isValidLightLevel()
//    {
//        return true;
//    }

}
