package com.zivilon.cinder_loe.items.specials;


import com.zivilon.cinder_loe.items.WizardStaff;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.passive.EntityAnimal;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World;

import com.zivilon.cinder_loe.util.BlockPos;
import com.zivilon.cinder_loe.entity.npc.radagast.FangornBear;
import com.zivilon.cinder_loe.entity.npc.radagast.FangornWildBoar;
import com.zivilon.cinder_loe.entity.npc.radagast.FangornAuroch;
import com.zivilon.cinder_loe.entity.npc.radagast.FangornElk;
import com.zivilon.cinder_loe.entity.npc.radagast.FangornAnimal;

import java.util.Random;


import java.util.List;

public class RadagastStaff extends WizardStaff {
    public RadagastStaff() {
    	super();
    }
    
    public ItemStack onEaten(ItemStack itemstack, World world, EntityPlayer entityplayer) {
        entityplayer.swingItem();
        itemstack.damageItem(2, (EntityLivingBase)entityplayer);
        if (!world.isRemote) {
        	BlockPos location = BlockPos.findSafeSpawnLocation(world, entityplayer);
            if (location != null && !isAreaCrowded(world, entityplayer)) {
		        world.playSoundAtEntity((Entity)entityplayer, "lotr:ent.mallorn.summonEnt", 2.0F, (Item.itemRand.nextFloat() - Item.itemRand.nextFloat()) * 0.2F + 1.0F);
                spawnAnimal(location, world);
            } else {
		        world.playSoundAtEntity((Entity)entityplayer, "lotr:item.puff", 1.0F, 2.0F);            	
            }
        }
        return itemstack;
    }

    public boolean isAreaCrowded(World world, EntityPlayer player) {
        int radius = 30; // Check within 30 blocks
        int maxEntities = 10; // Maximum allowed nearby entities

        AxisAlignedBB checkArea = AxisAlignedBB.getBoundingBox(
                player.posX - radius, player.posY - radius, player.posZ - radius,
                player.posX + radius, player.posY + radius, player.posZ + radius
        );
        List<EntityAnimal> listAnimals = world.getEntitiesWithinAABB(EntityAnimal.class, checkArea);
        List<FangornAnimal> listFangornBear = world.getEntitiesWithinAABB(FangornAnimal.class, checkArea);

        // Combine counts from both lists
        int totalAnimals = listAnimals.size() + listFangornBear.size();

        return totalAnimals >= maxEntities;
    }

    public void spawnAnimal(BlockPos location, World world) {
    	Random rand = new Random();
	    int choice = rand.nextInt(4); // Generate a random number between 0 and 2
	    
	    EntityLivingBase entity = null;
	    switch (choice) {
	        case 0:
	            entity = new FangornBear(world); // Assuming FangornBear is your entity class for bears
	            break;
	        case 1:
	            entity = new FangornWildBoar(world); // Replace with your wild boar entity class
	            break;
	        case 2:
	            entity = new FangornAuroch(world); // Replace with your auroch entity class
	            break;
	        case 3:
	            entity = new FangornElk(world); // Replace with your auroch entity class
	            break;
	    }

	    if (entity != null) {
	        entity.setPosition(location.x + 0.5, location.y, location.z + 0.5); // Center the entity on the block
	        world.spawnEntityInWorld(entity);
	    }
    }
}
