package com.zivilon.cinder_loe.entity;

import cpw.mods.fml.common.network.simpleimpl.IMessage;

import lotr.common.LOTRLevelData;
import lotr.common.LOTRMod;
import lotr.common.entity.npc.LOTREntityNPC;
import lotr.common.entity.animal.LOTREntityHorse;
import lotr.common.entity.projectile.LOTREntityGandalfFireball;
import lotr.common.fac.LOTRFaction;
import lotr.common.network.LOTRPacketHandler;
import lotr.common.network.LOTRPacketWeaponFX;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.attributes.IAttributeInstance;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityThrowable;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.DamageSource;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;

import java.util.ArrayList;
import java.util.List;

public class SarumanWhiteFireball extends LOTREntityGandalfFireball {
    public SarumanWhiteFireball(World world) {
        super(world);
    }
    
    public SarumanWhiteFireball(World world, EntityLivingBase entityliving) {
        super(world, entityliving);
    }
    
    public SarumanWhiteFireball(World world, double d, double d1, double d2) {
        super(world, d, d1, d2);
    }
    
    protected void onImpact(MovingObjectPosition m) {
        if (!this.worldObj.isRemote)
            if (m.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK) {
                explode((Entity)null);
            } else if (m.typeOfHit == MovingObjectPosition.MovingObjectType.ENTITY) {
                Entity entity = m.entityHit;
                if (isEntityVulnerable(entity))
                    explode(entity); 
            }  
    }
           
        private void explode(Entity target) {
        if (this.worldObj.isRemote)
            return; 
        this.worldObj.playSoundAtEntity((Entity)this, "lotr:item.gandalfFireball", 4.0F, (this.rand.nextFloat() - this.rand.nextFloat()) * 0.2F + 1.0F);
        LOTRPacketWeaponFX packet = new LOTRPacketWeaponFX(LOTRPacketWeaponFX.Type.MACE_SAURON, (Entity)this);
        LOTRPacketHandler.networkWrapper.sendToAllAround((IMessage)packet, LOTRPacketHandler.nearEntity((Entity)this, 64.0D));
        if (target != null && isEntityVulnerable(target))
            target.attackEntityFrom(DamageSource.causeMobDamage(getThrower()), 10.0F); 
        List<EntityLivingBase> entities = this.worldObj.getEntitiesWithinAABB(EntityLivingBase.class, this.boundingBox.expand(6.0D, 6.0D, 6.0D));
        if (!entities.isEmpty())
            for (int i = 0; i < entities.size(); i++) {
                EntityLivingBase entity = entities.get(i);
                if (entity != target && isEntityVulnerable((Entity)entity)) {
                    float damage = 10.0F - getDistanceToEntity((Entity)entity) * 0.5F;
                    if (damage > 0.0F)
                        entity.attackEntityFrom(DamageSource.causeMobDamage(getThrower()), damage); 
                } 
            }  
        setDead();
    }
          
    private boolean isEntityVulnerable(Entity entity) {
        if (entity == getThrower())
            return false; 
        if (!(entity instanceof EntityLivingBase))
            return false; 
        if (entity instanceof EntityPlayer)
            return (LOTRLevelData.getData((EntityPlayer)entity).getAlignment(LOTRFaction.HIGH_ELF) < 0.0F); 
        return !LOTRFaction.HIGH_ELF.isGoodRelation(LOTRMod.getNPCFaction(entity));
    }
          
    protected float func_70182_d() {
        return 1.5F;
    }
           
    protected float getGravityVelocity() {
        return 0.0F;
    }
}