package cat.nyaa.nyaautils.signedit;

import cat.nyaa.nyaacore.CommandReceiver;
import cat.nyaa.nyaacore.LanguageRepository;
import cat.nyaa.nyaautils.I18n;
import cat.nyaa.nyaautils.NyaaUtils;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.Set;

public class SignEditCommands extends CommandReceiver {
    private final NyaaUtils plugin;

    public SignEditCommands(Object plugin, LanguageRepository i18n) {
        super((NyaaUtils) plugin, i18n);
        this.plugin = (NyaaUtils) plugin;
    }

    public static void printSignContent(Player player, SignContent content) {
        player.sendMessage(I18n.format("user.signedit.content"));
        for (int i = 0; i < 4; i++) {
            player.sendMessage(I18n.format("user.signedit.content_line", i, content.getLine(i)));
        }
    }

    @Override
    public String getHelpPrefix() {
        return "se";
    }

    @SubCommand(value = "edit", permission = "nu.se.admin")
    public void commandEdit(CommandSender sender, Arguments args) {
        Player player = asPlayer(sender);
        int line = args.nextInt();
        if (line >= 0 && line < 4) {
            Block block = player.getTargetBlock((Set<Material>)null, 5);
            if (block != null && block.getState() instanceof Sign) {
                String text = args.nextString();
                //checkFormatCodes(text);
                text = ChatColor.translateAlternateColorCodes('&', text);
                if (ChatColor.stripColor(text).length() > plugin.cfg.signedit_max_length) {
                    throw new BadCommandException("user.signedit.too_long", plugin.cfg.signedit_max_length);
                }
                if ("CLEAR".equalsIgnoreCase(text)) {
                    text = "";
                }
                Sign sign = (Sign) block.getState();
                sign.setLine(line, text);
                sign.update();
            } else {
                msg(sender, "user.signedit.not_sign");
            }
        } else {
            msg(sender, "user.signedit.invalid_line");
        }
    }

    @SubCommand(value = "sign", permission = "nu.se.player")
    public void commandSign(CommandSender sender, Arguments args) {
        Player player = asPlayer(sender);
        ItemStack item = getItemInHand(sender).clone();
        if (item.getType() != Material.SIGN) {
            msg(sender, "user.signedit.need_sign");
            return;
        }
        if (args.length() == 4) {
            int line = args.nextInt();
            String text = args.nextString();
            checkFormatCodes(text);
            text = ChatColor.translateAlternateColorCodes('&', text);
            if (ChatColor.stripColor(text).length() > plugin.cfg.signedit_max_length) {
                throw new BadCommandException("user.signedit.too_long", plugin.cfg.signedit_max_length);
            }
            if (line >= 0 && line < 4) {
                SignContent signContent = SignContent.fromItemStack(item);
                if ("CLEAR".equalsIgnoreCase(text)) {
                    text = "";
                }
                signContent.setLine(line, text);
                printSignContent(player, signContent);
                player.getInventory().setItemInMainHand(signContent.toItemStack(item));
            } else {
                msg(sender, "user.signedit.invalid_line");
            }
        } else {
            printSignContent(player, SignContent.fromItemStack(item));
        }
    }

    public void checkFormatCodes(String text) {
        for (String k : plugin.cfg.signedit_disabledFormattingCodes) {
            if (text.toUpperCase().contains("&" + k.toUpperCase())) {
                throw new BadCommandException("user.warn.blocked_format_codes", "&" + k);
            }
        }
    }
}

@@ -118,16 +118,96 @@
 sitory;%0A
+import cat.nyaa.nyaacore.Message;%0Aimport cat.nyaa.nyaacore.utils.RayTraceUtils;%0A
 import c
@@ -263,24 +263,68 @@
 .NyaaUtils;%0A
+import net.md_5.bungee.api.chat.ClickEvent;%0A
 import org.b
@@ -340,16 +340,41 @@
 tColor;%0A
+import org.bukkit.Color;%0A
 import o
@@ -1034,35 +1034,42 @@
             
-player.send
+Message msg = new 
 Message(I18n
@@ -1127,24 +1127,225 @@
 tLine(i)));%0A
+            msg.inner.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND,%22/nu se sign %22+i+%22 %22+content.getLine(i).replace('%C2%A7','&')));%0A            msg.send(player, Message.MessageType.CHAT);%0A
         %7D%0A  
@@ -1699,54 +1699,198 @@
 k = 
-player.getTargetBlock((Set%3CMaterial%3E)null, 5);
+null;%0A            try %7B%0A                block = RayTraceUtils.rayTraceBlock(player);%0A            %7D catch (ReflectiveOperationException e) %7B%0A                e.printStackTrace();%0A            %7D
 %0A   
@@ -2008,50 +2008,8 @@
 ();%0A
-                //checkFormatCodes(text);%0A
     

improve signedit