package com.zivilon.cinder_loe.character;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.eventhandler.EventPriority;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingHurtEvent;

import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.passive.EntityAnimal;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World;

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

import com.zivilon.cinder_loe.entity.npc.FangornAnimal;

public class CharacterEventListener {

    public CharacterEventListener() {
        // Register the event listener
        MinecraftForge.EVENT_BUS.register(this);
    }

    @SubscribeEvent(priority = EventPriority.NORMAL, receiveCanceled = true)
    public void onLivingHurt(LivingHurtEvent event) {
        // Check if the entity being hurt is a character of interest
        EntityLivingBase damagedEntity = event.entityLiving;

        // Check if the source of damage is an entity
        if (event.source.getSourceOfDamage() instanceof EntityLivingBase) {
            // The entity that caused the damage
            EntityLivingBase damagerEntity = (EntityLivingBase) event.source.getSourceOfDamage();

            if (damagedEntity instanceof EntityPlayerMP) {
                EntityPlayerMP player = (EntityPlayerMP)damagedEntity;

                UUID radagast = CharacterRoleAPI.getCharacterRoleUUID("Radagast");
                if (player.getUniqueID().equals(radagast)) {
                    List<EntityLivingBase> entities = findAnimalsAndFangornEntities((EntityLivingBase)player);
                    for (EntityLivingBase entity : entities) {
                        EntityLiving animal = (EntityLiving) entity;
                        animal.setAttackTarget((EntityLivingBase)damagerEntity);
                    }
                }
            }
        }
    }

    public static List<EntityLivingBase> findAnimalsAndFangornEntities(EntityLivingBase centerEntity) {
        World world = centerEntity.worldObj;
        double searchRadius = 32.0D; // Define the search radius

        // Calculate the search area
        AxisAlignedBB searchArea = AxisAlignedBB.getBoundingBox(
                centerEntity.posX - searchRadius, centerEntity.posY - searchRadius, centerEntity.posZ - searchRadius,
                centerEntity.posX + searchRadius, centerEntity.posY + searchRadius, centerEntity.posZ + searchRadius);

        // Retrieve all entities within the search area
        List entitiesWithinAABB = world.getEntitiesWithinAABB(EntityLivingBase.class, searchArea);
        List<EntityLivingBase> foundEntities = new ArrayList<EntityLivingBase>();

        // Manually filter the entities to match your criteria
        for (Object obj : entitiesWithinAABB) {
            if (obj instanceof EntityAnimal || obj instanceof FangornAnimal) { // Adjust for your custom entity
                foundEntities.add((EntityLivingBase) obj);
            }
        }

        return foundEntities;
    }
}
