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 SarumanFireball extends LOTREntityGandalfFireball {
    public SarumanFireball(World world) {
        super(world);
    }
    
    public SarumanFireball(World world, EntityLivingBase entityliving) {
        super(world, entityliving);
    }
    
    public SarumanFireball(World world, double d, double d1, double d2) {
        super(world, d, d1, d2);
    }
    
    protected void onImpact(MovingObjectPosition m) {
        if (!((Entity)this).worldObj.isRemote)
            if (m.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK) {
                explode((Entity)null);
            } else if (m.typeOfHit == MovingObjectPosition.MovingObjectType.ENTITY) {
                explode(m.entityHit); 
            }    
    }
    
    private void explode(Entity target) {
        if (((Entity)this).worldObj.isRemote)
            return; 
        ((Entity)this).worldObj.playSoundAtEntity((Entity)this, "lotr:item.gandalfFireball", 4.0F, (rand.nextFloat() - rand.nextFloat()) * 0.2F + 1.0F);

        List<EntityLiving> entities = ((Entity)this).worldObj.getEntitiesWithinAABB(EntityLiving.class, ((Entity)this).boundingBox.expand(6.0D, 6.0D, 6.0D));

        if (!entities.isEmpty())
        for (int i = 0; i < entities.size(); i++) {
            Entity entity = entities.get(i);
            if (entity instanceof EntityLiving && !(entity instanceof EntityPlayer) && !(entity instanceof LOTREntityHorse && ((LOTREntityHorse)entity).getBelongsToNPC() == true)) {
                targetRandomEntity((EntityLiving)entity);
            }
        }
        setDead();
    }
    
    public void targetRandomEntity(EntityLiving npc) {
        LOTRFaction faction = null;
        if (npc instanceof LOTREntityNPC) {
            LOTREntityNPC lotrNpc = (LOTREntityNPC) npc;
            faction = lotrNpc.getFaction();
        }

        // Define the search area based on the npc's location
        IAttributeInstance followRangeAttribute = npc.getEntityAttribute(SharedMonsterAttributes.followRange);
        double range = followRangeAttribute != null ? followRangeAttribute.getAttributeValue() : 12.0D; // Default to 12 if attribute is not found

        AxisAlignedBB searchArea = npc.boundingBox.expand(range, 4.0D, range);

        // Filter potential targets to not include hiring player or NPCs from same faction
        List<EntityLiving> allTargets = npc.worldObj.getEntitiesWithinAABB(EntityLiving.class, searchArea);
        List<EntityLiving> validTargets = new ArrayList<>();

        for (EntityLiving potentialTarget : allTargets) {
            Entity entity = (Entity)potentialTarget; // Because for some reason I can't directly check if EntityLiving is instance of EntityPlayer
            if (entity != npc &&
                !(entity instanceof EntityPlayer) &&
                !(entity instanceof LOTREntityNPC && faction != null && ((LOTREntityNPC)entity).getFaction() == faction) &&
                !(entity instanceof LOTREntityHorse && ((LOTREntityHorse)entity).getBelongsToNPC() == true)) {
                validTargets.add(potentialTarget);
            }
        }

        // Randomly select a new target from the filtered list
        if (!validTargets.isEmpty()) {
            EntityLiving newTarget = validTargets.get(npc.getRNG().nextInt(validTargets.size()));
            npc.setAttackTarget(newTarget); // Set the new attack target
        }
    }
}