package pw.yumc.Residence.listeners;

import java.util.HashMap;
import java.util.Map;

import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;

import pw.yumc.Residence.gui.SetFlag;

public class ResidenceGUIListener implements Listener {
    public static Map<String, SetFlag> GUI = new HashMap<>();

    @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
    public void onFlagGuiClick(final InventoryClickEvent event) {
        final String player = event.getWhoClicked().getName();
        if (GUI.size() == 0 || !GUI.containsKey(player)) {
            return;
        }
        event.setCancelled(true);
        final int slot = event.getRawSlot();
        if ((slot > 53) || (slot < 0)) {
            return;
        }
        final SetFlag setFlag = GUI.get(player);
        setFlag.toggleFlag(event);
    }

    @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
    public void onFlagGuiClose(final InventoryCloseEvent event) {
        final String player = event.getPlayer().getName();
        if (GUI.size() != 0 & GUI.containsKey(player)) {
            GUI.remove(player);
        }
    }
}
