package nl.sbdeveloper.themeparkplus.listeners;

import nl.iobyte.themepark.ThemePark;
import nl.iobyte.themepark.api.attraction.objects.Attraction;
import nl.sbdeveloper.themeparkplus.ThemeParkPlus;
import nl.sbdeveloper.themeparkplus.gui.MalfunctionGUI;
import nl.sbdeveloper.themeparkplus.util.ConfigUtil;
import org.bukkit.ChatColor;
import org.bukkit.block.Sign;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;

public class MalfunctionListener implements Listener {
    @EventHandler
    public void onClickSign(PlayerInteractEvent e) {
        if (e.getClickedBlock() == null || e.getAction() != Action.RIGHT_CLICK_BLOCK || !(e.getClickedBlock().getState() instanceof Sign) || e.getHand() != EquipmentSlot.HAND) return;

        String mrLineOne = ChatColor.stripColor(ConfigUtil.__(ThemeParkPlus.getSConfig().getFile().getString("Malfunction.Sign.Row1")));
        String mrLineTwo = ChatColor.stripColor(ConfigUtil.__(ThemeParkPlus.getSConfig().getFile().getString("Malfunction.Sign.Row2")));

        Sign sign = (Sign) e.getClickedBlock().getState();
        if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase(mrLineOne) && ChatColor.stripColor(sign.getLine(1)).equalsIgnoreCase(mrLineTwo)) {
            String attID = sign.getLine(2);
            if (!ThemePark.getInstance().getAPI().getAttractionService().hasAttraction(attID)) return;
            Attraction att = ThemePark.getInstance().getAPI().getAttractionService().getAttraction(attID);

            new MalfunctionGUI(att, e.getPlayer());
        }
    }
}
