package works.chatterbox.chatterbox.pipeline.stages.impl.channel;

import org.jetbrains.annotations.NotNull;
import works.chatterbox.chatterbox.Chatterbox;
import works.chatterbox.chatterbox.channels.Channel;
import works.chatterbox.chatterbox.messages.Message;
import works.chatterbox.chatterbox.pipeline.PipelineContext;
import works.chatterbox.chatterbox.pipeline.stages.Stage;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TagStage implements Stage {

    private final Chatterbox chatterbox;
    private final Pattern tagPattern = Pattern.compile("^@([^\\s]+)");

    public TagStage(final Chatterbox chatterbox) {
        this.chatterbox = chatterbox;
    }

    /**
     * This processes @-tags. If a message starts with {@code @tag} where {@code tag} is the tag of a channel, this will
     * reroute the message to that channel and remove the tag from the message.
     * <p>Intended effect: Reroute the message to the tagged channel
     */
    @Override
    public void process(@NotNull final Message message, @NotNull final PipelineContext context) {
        if (message.isCancelled()) return;
        final String content = message.getMessage();
        if (!content.startsWith("@")) return;
        final Matcher m = tagPattern.matcher(content);
        if (!m.find()) return;
        final String tag = m.group(1);
        final Channel channel = this.chatterbox.getAPI().getChannelAPI().getChannelByTag(tag);
        // TODO: Should this cancel the message and send an error or just ignore the @-tag?
        if (channel == null || !message.getSender().getChannels().contains(channel)) return;
        message.setMessage(m.replaceFirst("").trim());
        message.setChannel(channel);
    }
}

@@ -60,16 +60,45 @@
 annel;%0A%0A
+import org.bukkit.ChatColor;%0A
 import o
@@ -1492,123 +1492,261 @@
     
-// TODO: Should this cancel the message and send an error or just ignore the @-tag?%0A        if (channel == null %7C%7C 
+if (channel == null) %7B%0A            message.getSender().ifOnline(player -%3E player.sendMessage(ChatColor.RED + this.chatterbox.getLanguage().getString(%22NO_SUCH_CHANNEL%22)));%0A            message.setCancelled(true);%0A            return;%0A        %7D%0A        if (
 !mes
@@ -1795,23 +1795,234 @@
 annel)) 
-return;
+%7B%0A            message.getSender().ifOnline(player -%3E player.sendMessage(ChatColor.RED + this.chatterbox.getLanguage().getString(%22NOT_IN_CHANNEL%22)));%0A            message.setCancelled(true);%0A            return;%0A        %7D
 %0A       

Send error messages