package common.entity.animal;

import common.ai.EntityAIBase;
import common.entity.npc.Alignment;
import common.entity.types.EntityWaterMob;
import common.init.Items;
import common.init.SoundEvent;
import common.item.Item;
import common.item.ItemStack;
import common.util.ExtMath;
import common.world.World;

public class EntitySquid extends EntityWaterMob
{
    public float squidPitch;
    public float prevSquidPitch;
    public float squidYaw;
    public float prevSquidYaw;

    /**
     * appears to be rotation in radians; we already have pitch & yaw, so this completes the triumvirate.
     */
    public float squidRotation;

    /** previous squidRotation in radians */
    public float prevSquidRotation;

    /** angle of the tentacles in radians */
    public float tentacleAngle;

    /** the last calculated angle of the tentacles in radians */
    public float lastTentacleAngle;
    private float randomMotionSpeed;

    /** change in squidRotation in radians. */
    private float rotationVelocity;
    private float field_70871_bB;
    private float randomMotionVecX;
    private float randomMotionVecY;
    private float randomMotionVecZ;

    public EntitySquid(World worldIn)
    {
        super(worldIn);
        this.setSize(0.95F, 0.95F);
        this.rand.setSeed((long)(1 + this.getId()));
        this.rotationVelocity = 1.0F / (this.rand.floatv() + 1.0F) * 0.2F;
        this.tasks.addTask(0, new EntitySquid.AIMoveRandom(this));
    }

    protected void applyEntityAttributes()
    {
        super.applyEntityAttributes();
        this.setMaxHealth(10);
    }

    public float getEyeHeight()
    {
        return this.height * 0.5F;
    }

//    /**
//     * Returns the sound this mob makes while it's alive.
//     */
//    protected String getLivingSound()
//    {
//        return null;
//    }

    /**
     * Returns the sound this mob makes when it is hurt.
     */
    protected SoundEvent getHurtSound()
    {
        return null;
    }

    /**
     * Returns the sound this mob makes on death.
     */
    protected SoundEvent getDeathSound()
    {
        return null;
    }

    /**
     * Returns the volume for the sounds this mob makes.
     */
    protected float getSoundVolume()
    {
        return 0.4F;
    }

    protected Item getDropItem()
    {
        return null;
    }

    /**
     * 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;
    }

    /**
     * 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)
    {
        int i = this.rand.roll(3 + lootingModifier);

        for (int j = 0; j < i; ++j)
        {
            this.entityDropItem(new ItemStack(Items.ink_sack), 0.0F);
        }
    }

    /**
     * Checks if this entity is inside water (if inWater field is true as a result of handleWaterMovement() returning
     * true)
     */
    public boolean isInLiquid()
    {
        return this.worldObj.handleLiquidAcceleration(this.getEntityBoundingBox().expand(0.0D, -0.6000000238418579D, 0.0D), this);
    }

    /**
     * Called frequently so the entity can update its state every tick as required. For example, zombies and skeletons
     * use this to react to sunlight and start to burn.
     */
    public void onLivingUpdate()
    {
        super.onLivingUpdate();
        this.prevSquidPitch = this.squidPitch;
        this.prevSquidYaw = this.squidYaw;
        this.prevSquidRotation = this.squidRotation;
        this.lastTentacleAngle = this.tentacleAngle;
        this.squidRotation += this.rotationVelocity;

        if ((double)this.squidRotation > (Math.PI * 2D))
        {
            if (this.worldObj.client)
            {
                this.squidRotation = ((float)Math.PI * 2F);
            }
            else
            {
                this.squidRotation = (float)((double)this.squidRotation - (Math.PI * 2D));

                if (this.rand.chance(10))
                {
                    this.rotationVelocity = 1.0F / (this.rand.floatv() + 1.0F) * 0.2F;
                }

                this.worldObj.setEntityState(this, (byte)19);
            }
        }

        if (this.inLiquid)
        {
            if (this.squidRotation < (float)Math.PI)
            {
                float f = this.squidRotation / (float)Math.PI;
                this.tentacleAngle = ExtMath.sin(f * f * (float)Math.PI) * (float)Math.PI * 0.25F;

                if ((double)f > 0.75D)
                {
                    this.randomMotionSpeed = 1.0F;
                    this.field_70871_bB = 1.0F;
                }
                else
                {
                    this.field_70871_bB *= 0.8F;
                }
            }
            else
            {
                this.tentacleAngle = 0.0F;
                this.randomMotionSpeed *= 0.9F;
                this.field_70871_bB *= 0.99F;
            }

            if (!this.worldObj.client)
            {
                this.motionX = (double)(this.randomMotionVecX * this.randomMotionSpeed);
                this.motionY = (double)(this.randomMotionVecY * this.randomMotionSpeed);
                this.motionZ = (double)(this.randomMotionVecZ * this.randomMotionSpeed);
            }

            float f1 = ExtMath.sqrtd(this.motionX * this.motionX + this.motionZ * this.motionZ);
            this.yawOffset += (-((float)ExtMath.atan2(this.motionX, this.motionZ)) * 180.0F / (float)Math.PI - this.yawOffset) * 0.1F;
            this.rotYaw = this.yawOffset;
            this.squidYaw = (float)((double)this.squidYaw + Math.PI * (double)this.field_70871_bB * 1.5D);
            this.squidPitch += (-((float)ExtMath.atan2((double)f1, this.motionY)) * 180.0F / (float)Math.PI - this.squidPitch) * 0.1F;
        }
        else
        {
            this.tentacleAngle = ExtMath.absf(ExtMath.sin(this.squidRotation)) * (float)Math.PI * 0.25F;

            if (!this.worldObj.client)
            {
                this.motionX = 0.0D;
                this.motionY -= 0.08D;
                this.motionY *= 0.9800000190734863D;
                this.motionZ = 0.0D;
            }

            this.squidPitch = (float)((double)this.squidPitch + (double)(-90.0F - this.squidPitch) * 0.02D);
        }
    }

    /**
     * Moves the entity based on the specified heading.  Args: strafe, forward
     */
    public void moveEntityWithHeading(float strafe, float forward)
    {
        this.moveEntity(this.motionX, this.motionY, this.motionZ);
    }

    public void handleStatusUpdate(byte id)
    {
        if (id == 19)
        {
            this.squidRotation = 0.0F;
        }
        else
        {
            super.handleStatusUpdate(id);
        }
    }

    public void setRandomMotion(float randomMotionVecXIn, float randomMotionVecYIn, float randomMotionVecZIn)
    {
        this.randomMotionVecX = randomMotionVecXIn;
        this.randomMotionVecY = randomMotionVecYIn;
        this.randomMotionVecZ = randomMotionVecZIn;
    }

    public boolean hasRandomMotion()
    {
        return this.randomMotionVecX != 0.0F || this.randomMotionVecY != 0.0F || this.randomMotionVecZ != 0.0F;
    }
    
	public int getColor() {
		return 0x0000ff;
	}
	
	public Alignment getAlignment() {
		return Alignment.CHAOTIC_GOOD;
	}

    static class AIMoveRandom extends EntityAIBase
    {
        private EntitySquid squid;

        public AIMoveRandom(EntitySquid p_i45859_1_)
        {
            this.squid = p_i45859_1_;
        }

        public boolean shouldExecute()
        {
            return true;
        }

        public void updateTask()
        {
//            int i = this.squid.getAge();
//
//            if (i > 100)
//            {
//                this.squid.func_175568_b(0.0F, 0.0F, 0.0F);
//            }
//            else 
            if (this.squid.getRNG().chance(50) || !this.squid.inLiquid || !this.squid.hasRandomMotion())
            {
                float f = this.squid.getRNG().floatv() * (float)Math.PI * 2.0F;
                float f1 = ExtMath.cos(f) * 0.2F;
                float f2 = -0.1F + this.squid.getRNG().floatv() * 0.2F;
                float f3 = ExtMath.sin(f) * 0.2F;
                this.squid.setRandomMotion(f1, f2, f3);
            }
        }
    }
}
