package common.ai;

import common.entity.npc.EntityNPC;
import common.entity.projectile.EntityFireCharge;
import common.entity.types.EntityLiving;
import common.util.BlockPos;

public class AISmallFireballAttack extends EntityAIBase
{
    private EntityNPC entity;
    private int timer;
    private int charge;
    private int amount;

    public AISmallFireballAttack(EntityNPC entity)
    {
        this.entity = entity;
        this.setMutexBits(3);
    }

    public boolean shouldExecute()
    {
        EntityLiving tgt = this.entity.getAttackTarget();
        return tgt != null && tgt.isEntityAlive();
    }

    public void startExecuting()
    {
        this.timer = 0;
    }

    public void resetTask()
    {
        this.entity.setAttacking(false);
    }

    public void updateTask()
    {
        --this.charge;
        EntityLiving target = this.entity.getAttackTarget();
        double d0 = this.entity.getDistanceSqToEntity(target);

        if (d0 < 4.0D)
        {
            if (this.charge <= 0)
            {
                this.charge = this.entity.getRNG().range(8, 20);
                this.entity.attackEntityAsMob(target);
            }

            this.entity.getMoveHelper().setMoveTo(target.posX, target.posY, target.posZ, 1.0D);
        }
        else if (d0 < 256.0D)
        {
            double d1 = target.posX - this.entity.posX;
            double d2 = target.getEntityBoundingBox().minY + (double)(target.height / 2.0F) - (this.entity.posY + (double)(this.entity.height / 2.0F));
            double d3 = target.posZ - this.entity.posZ;

            if (this.charge <= 0)
            {
                ++this.timer;

                if (this.timer == 1)
                {
                    this.charge = this.entity.getRNG().range(10, 20); // 60
                    this.entity.setAttacking(true);
                    this.amount = this.entity.getRNG().range(5, 25);
                }
                else if (this.timer <= this.amount)
                {
                    this.charge = this.entity.getRNG().range(2, 6); // 6
                }
                else
                {
                    this.charge = this.entity.getRNG().range(2, 6); // 100
                    this.timer = 0;
                    this.entity.setAttacking(false);
                }

                if (this.timer > 1)
                {
                	this.entity.swingItem();
//                    float f = MathHelper.sqrt_float(MathHelper.sqrt_double(d0)) * 0.5F;
                    this.entity.worldObj.playAuxSFX(1009, new BlockPos((int)this.entity.posX, (int)this.entity.posY, (int)this.entity.posZ), 0);

                    for (int i = 0; i < 1; ++i)
                    {
                        EntityFireCharge entitysmallfireball = new EntityFireCharge(this.entity.worldObj, this.entity, d1 /* + this.entity.getRNG().gaussian() * (double)f */, d2, d3 /* + this.entity.getRNG().gaussian() * (double)f */);
                        entitysmallfireball.posY = this.entity.posY + (double)(this.entity.height / 2.0F) + 0.5D;
                        this.entity.worldObj.spawnEntityInWorld(entitysmallfireball);
                    }
                }
            }

            this.entity.getLookHelper().setLookPositionWithEntity(target, 30.0F, 30.0F);
        }
        else
        {
            this.entity.getNavigator().clearPathEntity();
            this.entity.getMoveHelper().setMoveTo(target.posX, target.posY, target.posZ, 1.0D);
        }

        super.updateTask();
    }
}