package moe.oko.opennaw.listener;

import moe.oko.opennaw.OpenNAW;
import moe.oko.opennaw.util.WorldGuardHelper;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;

public class GateListener implements Listener {

    @EventHandler
    public void onBlockInteract(PlayerInteractEvent event) {
        if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
            if(event.getClickedBlock().getType().name().contains("_SIGN")) {
                //its a sign!!!
                if(OpenNAW.getInstance().getGateHandler().getGateBySign(event.getClickedBlock()) != null) {
                    if(WorldGuardHelper.fetchCityRegionViaLoc(event.getClickedBlock().getLocation()) != null) {
                        //in a city, start perm check.
                        var rgr = WorldGuardHelper.fetchCityRegionViaLoc(event.getClickedBlock().getLocation());
                        if(!(WorldGuardHelper.isPlayerPermitted(rgr, event.getPlayer()))) {
                            event.getPlayer().sendMessage(ChatColor.RED + "" + ChatColor.ITALIC + "The gate is locked...");
                            return;
                        }
                    }
                    var gate = OpenNAW.getInstance().getGateHandler().getGateBySign(event.getClickedBlock());
                    gate.toggle(); //toggle gate
                    //TODO PERMS CHECK!!!!
                }
            }
        }
    }
}
