package tc.oc.pgm.modules;

import java.util.Set;

import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;

import tc.oc.pgm.match.Match;
import tc.oc.pgm.match.MatchModule;
import tc.oc.pgm.match.MatchScope;
import tc.oc.pgm.events.ListenerScope;

@ListenerScope(MatchScope.RUNNING)
public class ToolRepairMatchModule extends MatchModule implements Listener {
    protected final Set<Material> toRepair;

    public ToolRepairMatchModule(Match match, Set<Material> toRepair) {
        super(match);
        this.toRepair = toRepair;
    }

    private boolean canRepair(ItemStack pickup, ItemStack invStack) {
        return invStack != null &&
               invStack.getType().equals(pickup.getType()) &&
               invStack.getEnchantments().equals(pickup.getEnchantments());
    }

    private void doRepair(PlayerPickupItemEvent event, ItemStack stack) {
        ItemStack pickup = event.getItem().getItemStack();

        int hitsLeft = pickup.getType().getMaxDurability() - pickup.getDurability() + 1;
        stack.setDurability((short) Math.max(stack.getDurability() - hitsLeft, 0));

        event.setCancelled(true);
        event.getItem().remove();
        event.getPlayer().playSound(event.getPlayer().getLocation(), Sound.ENTITY_ITEM_PICKUP, 0.5f, 1f);
    }

    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void processRepair(PlayerPickupItemEvent event) {
        ItemStack pickup = event.getItem().getItemStack();

        if(this.toRepair.contains(pickup.getType())) {
            PlayerInventory inv = event.getPlayer().getInventory();
            for(ItemStack invStack : inv.contents()) {
                if(this.canRepair(pickup, invStack)) {
                    this.doRepair(event, invStack);
                    return;
                }
            }
        }
    }
}
