package common.ai;

import common.entity.npc.EntityNPC;
import common.entity.types.EntityLiving;
import common.util.ExtMath;

public class AIRangedAttack extends EntityAIBase
{
    private final EntityNPC entity;
    private EntityLiving target;
    private int delay;
    private double speed;
    private int timeout;
    private int minDelay;
    private int maxDelay;
    private float distance;
    private float distanceSq;

    public AIRangedAttack(EntityNPC attacker, double movespeed, int minDelay, int maxDelay, float maxAttackDistanceIn)
    {
        this.delay = -1;
        this.entity = attacker;
        this.speed = movespeed;
        this.minDelay = minDelay;
        this.maxDelay = maxDelay;
        this.distance = maxAttackDistanceIn;
        this.distanceSq = maxAttackDistanceIn * maxAttackDistanceIn;
        this.setMutexBits(3);
    }

    /**
     * Returns whether the EntityAIBase should begin execution.
     */
    public boolean shouldExecute()
    {
    	if(this.entity.isFleeing())
    		return false;
        EntityLiving entitylivingbase = this.entity.getAttackTarget();

        if (entitylivingbase == null || entitylivingbase.dead)
        {
            this.target = null;
            return false;
        }
        else
        {
            this.target = entitylivingbase;
            return true;
        }
    }

    /**
     * Returns whether an in-progress EntityAIBase should continue executing
     */
    public boolean continueExecuting()
    {
        return this.shouldExecute() || (!this.entity.isFleeing() && !this.entity.getNavigator().noPath() && this.target != null && !this.target.dead);
    }
    
	public void startExecuting()
    {
        this.entity.setUsingItem(true);
    }

    /**
     * Resets the task
     */
    public void resetTask()
    {
        this.target = null;
        this.timeout = 0;
        this.delay = -1;
        this.entity.setUsingItem(false);
    }

    /**
     * Updates the task
     */
    public void updateTask()
    {
        double d0 = this.entity.getDistanceSq(this.target.posX, this.target.getEntityBoundingBox().minY, this.target.posZ);
        boolean flag = this.entity.getEntitySenses().canSee(this.target);

        if (flag)
        {
            ++this.timeout;
        }
        else
        {
            this.timeout = 0;
        }

        if (d0 <= (double)this.distanceSq && this.timeout >= 20)
        {
            this.entity.getNavigator().clearPathEntity();
        }
        else
        {
            this.entity.getNavigator().tryMoveToEntityLiving(this.target, this.speed);
        }

        this.entity.getLookHelper().setLookPositionWithEntity(this.target, 30.0F, 30.0F);

        if (--this.delay == 0)
        {
            if (d0 > (double)this.distanceSq || !flag)
            {
                return;
            }

            float f = ExtMath.sqrtd(d0) / this.distance;
            float lvt_5_1_ = ExtMath.clampf(f, 0.1F, 1.0F);
            this.entity.attackEntityWithRangedAttack(this.target, lvt_5_1_);
            this.delay = ExtMath.floorf(f * (float)(this.maxDelay - this.minDelay) + (float)this.minDelay);
        }
        else if (this.delay < 0)
        {
            float f2 = ExtMath.sqrtd(d0) / this.distance;
            this.delay = ExtMath.floorf(f2 * (float)(this.maxDelay - this.minDelay) + (float)this.minDelay);
        }
    }
}
