/**
 * TerraCraftTools(SuperSoapTools) - NoGuardDrops.java
 * Copyright (c) 2013 Jeremy Koletar (jjkoletar), <http://jj.koletar.com>
 * Copyright (c) 2013 computerdude5000,<computerdude5000@gmail.com>
 *
 * TerraCraftTools is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TerraCraftTools is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TerraCraftTools.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.github.computerdude5000.terracrafttools.modules;

import com.github.computerdude5000.terracrafttools.TerraCraftTools;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.inventory.ItemStack;

import java.util.LinkedList;
import java.util.List;

/**
 * NoGuardDrops Module
 * <p/>
 * Module that prevents guards from dropping certain configurable items both manually, and on death.
 *
 * @author jjkoletar
 */
public class NoGuardDrops implements SuperSoapToolsModule, Listener {
    private TerraCraftTools plugin = null;
    private List<Integer> nonDroppedItems = null;
    private List<String> noDropGroups = null;

    /**
     * initModule enables the module called by {@link TerraCraftTools}
     */
    public void initModule(TerraCraftTools sst) {
        plugin = sst;
        plugin.getServer().getPluginManager().registerEvents(this, plugin);
        nonDroppedItems = plugin.getModuleConfig("NoGuardDrops").getIntegerList("DeniedDrops");
        noDropGroups = plugin.getModuleConfig("NoGuardDrops").getStringList("GuardGroups");
        plugin.logger.info("[TerraCraftTools][NoGuardDrops] NoGuardDrops Module Enabled!");
    }

    /**
     * deinitModule disables the module called by {@link TerraCraftTools}
     */
    public void deinitModule() {
        HandlerList.unregisterAll(this);
        nonDroppedItems = null;
        noDropGroups = null;
        plugin.logger.info("[TerraCraftTools][NoGuardDrops] NoGuardDrops Module Disabled!");
        plugin = null;
    }

    /**
     * Returns true or false when a player types a command
     *
     * @param sender  sender who sent the message
     * @param command command that was sent
     * @param label   idk what this does.
     * @param args    arguments that were sent along with the command
     * @return true or false
     */
    public boolean callCommand(CommandSender sender, Command command, String label, String[] args) {
        return false;
    }

    @EventHandler
    public void onEntityDeath(EntityDeathEvent event) {
        Player p;
        if (event.getEntity() instanceof Player) {
            p = (Player) event.getEntity();
        } else {
            return;
        }
        boolean isGuard = false;
        for (String noDropGroup : noDropGroups) {
            if (plugin.getPermissionsApi().playerInGroup(p, noDropGroup)) {
                isGuard = true;
            }
        }
        if (isGuard) {
            List<ItemStack> remove = new LinkedList<ItemStack>();
            for (ItemStack item : event.getDrops()) {
                if (shouldDropItem(item)) {
                    remove.add(item);
                }
            }
            for (ItemStack aRemove : remove) {
                event.getDrops().remove(aRemove);
            }
        }
    }

    @EventHandler
    public void onPlayerDropItem(PlayerDropItemEvent event) {
        Player p = event.getPlayer();
        boolean isGuard = false;
        for (String noDropGroup : noDropGroups) {
            if (plugin.getPermissionsApi().playerInGroup(p, noDropGroup)) {
                isGuard = true;
            }
        }
        if (isGuard) {
            if (shouldDropItem(event.getItemDrop().getItemStack())) {
                event.setCancelled(true);
            }
        }
    }

    private boolean shouldDropItem(ItemStack item) {
        return nonDroppedItems.contains(item.getTypeId());
    }
}
