package common.ai;

import common.entity.npc.EntityNPC;
import common.entity.projectile.EntityFireball;
import common.entity.types.EntityLiving;
import common.init.Items;
import common.item.ItemStack;
import common.util.BlockPos;
import common.util.Vec3;
import common.world.World;

public class AIFireballAttack extends EntityAIBase
{
	public final int power;
	public final int delay;
	public final double distance;
	public final double velocity;
    private final EntityNPC parentEntity;
    public int attackTimer;

    public AIFireballAttack(EntityNPC entity, int power, int delay, double distance, double velocity)
    {
        this.parentEntity = entity;
        this.power = power;
        this.delay = delay;
        this.distance = distance;
        this.velocity = velocity;
//        this.setMutexBits(3);
    }

    public boolean shouldExecute()
    {
        return this.parentEntity.getAttackTarget() != null;
    }

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

    public void resetTask()
    {
        this.parentEntity.setItemNoUpdate(0, null);
    }

    public void updateTask()
    {
        EntityLiving target = this.parentEntity.getAttackTarget();
//        double d0 = 64.0D;

        if (target.getDistanceSqToEntity(this.parentEntity) < this.distance * this.distance && this.parentEntity.canEntityBeSeen(target))
        {
            World world = this.parentEntity.worldObj;
            ++this.attackTimer;

            if (this.attackTimer == this.delay)
            {
                world.playAuxSFX(1007, new BlockPos(this.parentEntity), 0);
                this.parentEntity.setItemNoUpdate(0, new ItemStack(Items.orb));
            }

            if (this.attackTimer == this.delay * 2)
            {
            	this.parentEntity.swingItem();
                this.parentEntity.setItemNoUpdate(0, null);
                double d1 = 1.0D;
                Vec3 vec3 = this.parentEntity.getLook(1.0F);
                double d2 = target.posX - (this.parentEntity.posX + vec3.xCoord * d1);
                double d3 = target.getEntityBoundingBox().minY + (double)(target.height / 2.0F) - (this.parentEntity.posY + (double)this.parentEntity.getEyeHeight());
                double d4 = target.posZ - (this.parentEntity.posZ + vec3.zCoord * d1);
                world.playAuxSFX(1008, new BlockPos(this.parentEntity), 0);
                EntityFireball fireball = new EntityFireball(world, this.parentEntity, d2, d3, d4, this.velocity); 
                	// 0.0, 0.0, 0.0);
//                fireball.setAcceleration(d2 + this.parentEntity.getRNG().gaussian() * 0.1D,
//                		d3 + this.parentEntity.getRNG().gaussian() * 0.1D,
//                		d4 + this.parentEntity.getRNG().gaussian() * 0.1D);
//                fireball.accelerationX *=  this.velocity;
//                fireball.accelerationY *= this.velocity;
//                fireball.accelerationZ *= this.velocity;
                fireball.explosionPower = this.power;
                fireball.posX = this.parentEntity.posX + vec3.xCoord * d1;
                fireball.posY = this.parentEntity.posY + (double)this.parentEntity.getEyeHeight();
                fireball.posZ = this.parentEntity.posZ + vec3.zCoord * d1;
                world.spawnEntityInWorld(fireball);
                this.attackTimer = -this.delay * this.parentEntity.getRNG().range(1, 4);
            }
            this.parentEntity.getLookHelper().setLookPositionWithEntity(target, 30.0f, 30.0f);
        }
        else if (this.attackTimer > 0)
        {
            --this.attackTimer;
        }

        this.parentEntity.setItemNoUpdate(0, this.attackTimer > this.delay ? new ItemStack(Items.orb) : null);
    }
}