package common.ai;

import common.entity.animal.EntitySheep;
import common.init.BlockRegistry;
import common.init.Blocks;
import common.util.BlockPos;
import common.vars.Vars;
import common.world.State;
import common.world.World;

public class EntityAIEatGrass extends EntityAIBase
{
    private EntitySheep grassEaterEntity;
    private World entityWorld;
    int eatingGrassTimer;

    public EntityAIEatGrass(EntitySheep grassEaterEntityIn)
    {
        this.grassEaterEntity = grassEaterEntityIn;
        this.entityWorld = grassEaterEntityIn.worldObj;
        this.setMutexBits(7);
    }

    /**
     * Returns whether the EntityAIBase should begin execution.
     */
    public boolean shouldExecute()
    {
        if (this.grassEaterEntity.getRNG().zrange(this.grassEaterEntity.isChild() ? 50 : 1000) != 0)
        {
            return false;
        }
        else
        {
            BlockPos blockpos = new BlockPos(this.grassEaterEntity.posX, this.grassEaterEntity.posY, this.grassEaterEntity.posZ);
            State state = this.entityWorld.getState(blockpos);
            return state.getBlock() == Blocks.tallgrass || 
            		this.entityWorld.getState(blockpos.down()).getBlock() == Blocks.grass;
        }
    }

    /**
     * Execute a one shot task or start executing a continuous task
     */
    public void startExecuting()
    {
        this.eatingGrassTimer = 40;
        this.entityWorld.setEntityState(this.grassEaterEntity, (byte)10);
        this.grassEaterEntity.getNavigator().clearPathEntity();
    }

    /**
     * Resets the task
     */
    public void resetTask()
    {
        this.eatingGrassTimer = 0;
    }

    /**
     * Returns whether an in-progress EntityAIBase should continue executing
     */
    public boolean continueExecuting()
    {
        return this.eatingGrassTimer > 0;
    }

    /**
     * Number of ticks since the entity started to eat grass
     */
    public int getEatingGrassTimer()
    {
        return this.eatingGrassTimer;
    }

    /**
     * Updates the task
     */
    public void updateTask()
    {
        this.eatingGrassTimer = Math.max(0, this.eatingGrassTimer - 1);

        if (this.eatingGrassTimer == 4)
        {
            BlockPos blockpos = new BlockPos(this.grassEaterEntity.posX, this.grassEaterEntity.posY, this.grassEaterEntity.posZ);

            State state = this.entityWorld.getState(blockpos);
            if (state.getBlock() == Blocks.tallgrass)
            {
                if (Vars.mobGrief)
                {
                    this.entityWorld.destroyBlock(blockpos, false);
                }

                this.grassEaterEntity.eatGrass();
            }
            else
            {
                BlockPos blockpos1 = blockpos.down();

                if (this.entityWorld.getState(blockpos1).getBlock() == Blocks.grass)
                {
                    if (Vars.mobGrief)
                    {
                        this.entityWorld.playAuxSFX(2001, blockpos1, BlockRegistry.getIdFromBlock(Blocks.grass));
                        this.entityWorld.setState(blockpos1, Blocks.dirt.getState(), 2);
                    }

                    this.grassEaterEntity.eatGrass();
                }
            }
        }
    }
}
