package com.zivilon.cinder_loe.items;

import com.zivilon.cinder_loe.CinderLoE;
import com.zivilon.cinder_loe.LoECreativeTabs;
import lotr.common.entity.npc.LOTREntityNPC;
import lotr.common.entity.npc.LOTRHiredNPCInfo;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumAction;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.World;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class unitLevelTool extends Item {

    public unitLevelTool() {
        setCreativeTab(LoECreativeTabs.tabMiscLoE);
    }

    public EnumAction getItemUseAction(ItemStack itemstack) {
        return EnumAction.bow;
    }

    @Override
    public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player) {
        List<LOTREntityNPC> hiredNPCs = this.findHiredNPCsNearPlayer(player);

        if (!hiredNPCs.isEmpty()) {
            for (LOTREntityNPC hiredNPC : hiredNPCs) {
                if (hiredNPC != null && hiredNPC.hiredNPCInfo != null) {
                    try {
                        LOTRHiredNPCInfo npcInfo = hiredNPC.hiredNPCInfo;

                        Method addExperienceMethod = LOTRHiredNPCInfo.class.getDeclaredMethod("addExperience", int.class);
                        addExperienceMethod.setAccessible(true);

                        int xpToAdd = 1;

                        try {
                            xpToAdd = Integer.parseInt(itemStack.getDisplayName());
                        } catch (NumberFormatException e) {
                            player.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "Tool name wasnt a number, applying 1 Xp instead"));
                            continue;
                        }

                        addExperienceMethod.invoke(npcInfo, xpToAdd);


                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
        return itemStack;
    }

    private List<LOTREntityNPC> findHiredNPCsNearPlayer(EntityPlayer player) {
        // Logic to find all nearby hired NPCs within a certain range
        double range = 20.0D; // Set the range within which the NPCs should be found
        List<LOTREntityNPC> nearbyHiredNPCs = new ArrayList<>();

        for (Object entity : player.worldObj.getEntitiesWithinAABB(LOTREntityNPC.class, player.boundingBox.expand(range, range, range))) {
            LOTREntityNPC npc = (LOTREntityNPC) entity;

            // Check if the player has sufficient permission level (admin tool usage)
            if (npc.hiredNPCInfo.getHiringPlayerUUID() != null && player.canCommandSenderUseCommand(2, "")) {
                nearbyHiredNPCs.add(npc);
            }
        }

        return nearbyHiredNPCs;
    }


}
