package net.buycraft.plugin.bukkit.signs;

import lombok.RequiredArgsConstructor;
import net.buycraft.plugin.bukkit.BuycraftPlugin;
import net.buycraft.plugin.bukkit.tasks.SignUpdateApplication;
import net.buycraft.plugin.bukkit.tasks.SignUpdater;
import net.buycraft.plugin.bukkit.util.SerializedBlockLocation;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.BlockFace;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.SignChangeEvent;

@RequiredArgsConstructor
public class SignListener implements Listener {
    private final BuycraftPlugin plugin;

    @EventHandler
    public void onSignChange(SignChangeEvent event) {
        boolean ourSign;
        try {
            ourSign = event.getLine(0).equalsIgnoreCase("[buycraft_rp]");
        } catch (IndexOutOfBoundsException e) {
            return;
        }

        if (!ourSign)
            return;

        int pos;
        try {
            pos = Integer.parseInt(event.getLine(1));
        } catch (NumberFormatException | IndexOutOfBoundsException e) {
            event.getPlayer().sendMessage(ChatColor.RED + "The second line must be a number.");
            return;
        }

        if (!event.getPlayer().hasPermission("buycraft.admin")) {
            event.getPlayer().sendMessage(ChatColor.RED + "You can't create Buycraft signs.");
            return;
        }

        plugin.getSignStorage().addSign(new PurchaseSignPosition(SerializedBlockLocation.fromBukkitLocation(
                event.getBlock().getLocation()), pos));
        event.getPlayer().sendMessage(ChatColor.GREEN + "Added new recent purchase sign!");

        for (int i = 0; i < 4; i++) {
            event.setLine(i, "");
        }

        Bukkit.getScheduler().runTaskAsynchronously(plugin, new SignUpdater(plugin));
    }

    @EventHandler
    public void onBlockBreak(BlockBreakEvent event) {
        if (event.getBlock().getType() == Material.WALL_SIGN || event.getBlock().getType() == Material.SIGN_POST) {
            if (plugin.getSignStorage().removeSign(event.getBlock().getLocation())) {
                event.getPlayer().sendMessage(ChatColor.RED + "Removed recent purchase sign!");
                Bukkit.getScheduler().runTaskAsynchronously(plugin, new SignUpdater(plugin));
            }
            return;
        }

        for (BlockFace face : SignUpdateApplication.FACES) {
            if (plugin.getSignStorage().removeSign(event.getBlock().getRelative(face).getLocation())) {
                event.getPlayer().sendMessage(ChatColor.RED + "Removed recent purchase sign!");
                Bukkit.getScheduler().runTaskAsynchronously(plugin, new SignUpdater(plugin));
                return;
            }
        }
    }
}

@@ -1008,24 +1008,216 @@
    return;%0A%0A
+        if (!event.getPlayer().hasPermission(%22buycraft.admin%22)) %7B%0A            event.getPlayer().sendMessage(ChatColor.RED + %22You can't create Buycraft signs.%22);%0A            return;%0A        %7D%0A%0A
         int 
@@ -1221,16 +1221,16 @@
 nt pos;%0A
-
         
@@ -1504,58 +1504,17 @@
 if (
-!event.getPlayer().hasPermission(%22buycraft.admin%22)
+pos %3E 100
 ) %7B%0A
@@ -1582,38 +1582,52 @@
 u can't 
-create Buycraft signs.
+show more than 100 recent purchases!
 %22);%0A    

add some checks here